1

The short version. When trying to open a USB device using the open() function in the fcntl.h header, the program freezes

Now the slightly longer version. I am trying to interface with a time of flight sensor (TeraRanger Evo 60m). I am working on a mac running Big Sur 11.2.1. Using ls -l /dev/tty.* i have located the device path to be /dev/tty.usbmodem00000000001A1.

I have taken my code and heavily simplified it to isolate the issue I'm currently facing

#include <fcntl.h> // open
#include <stdio.h>  // print

int main()
{
  // /dev/tty.usbmodem00000000001A1
  char *device = "/dev/tty.usbmodem00000000001A1";

  printf("Start\n");
  int fd = open(device, O_RDWR | O_NOCTTY | O_SYNC);
  if(fd < 0)
  {
    perror(device);
    return -1;
  }
  printf("Opened\n");
  return 1;
}

When trying to open the device, the program freezes. There is no error printed to help me debug either. I have successfully connected to and received messages from another USB device I own by only changing the device path. Yet, this sensor make the code freeze.

I may also be useful to know that using the screen /dev/tty.usbmedem00000000001A1 115200 terminal command shows the output expected from the sensor. This means the device is properly functioning and it can be connected to but i have messed up my code somehow. I would greatly appreciate any help y'all can provide. Thank you!

Kyle Roth
  • 29
  • 3
  • What if you try to read it from console? – Eugene Sh. Mar 03 '21 at 21:54
  • @EugeneSh. I have received output when using the screen command for the console – Kyle Roth Mar 03 '21 at 21:59
  • Sounds that this device is pretending to be a serial port. So you should be working with it as with serial port. For reference: https://stackoverflow.com/questions/6947413/how-to-open-read-and-write-from-serial-port-in-c – Eugene Sh. Mar 03 '21 at 22:06
  • 1
    Try to open `/dev/cu.usbmodem00000000001A1` instead of `/dev/tty.usbmodem00000000001A1`. – Codo Mar 03 '21 at 22:33
  • Wow @Codo that fixed it! thank you! Can you explain why this is? – Kyle Roth Mar 03 '21 at 22:40
  • 1
    You have to google the details on the internet. It's an old thing from the time when modems were used. */dev/tty* is for incoming calls. So *open* waits until a call arrives, while */dev/cu* is for outgoing calls and doesn't wait. – Codo Mar 03 '21 at 22:44

0 Answers0