Results 1 to 3 of 3
  1. #1

    Member Since
    Oct 20, 2009
    iMac 17
    problem on open(/dev/tty.usbserial"....)
    Hello all,

    to control a solar installation, I've made an electronic board based on a PIC which outputs data (temperatures, ...) on a RS232 serial link @9600bps. To log data, I've bought an USB-serial convertor that I connected to my iMac. I've tested this link with a software like "coolTerm", everything is fine !

    Now, to store all this data and to provide it on a web site, I want to write a C program on the MAC to read data from USB-serial link and generate some kinds of HTML pages.

    My first C lines on MAC (my frist try after several years under Linux) start hardly : this easy program crashes on open function (it seems definitely locked...) ??? I've tried to add O_NONBLOCK attribute to open function, then it returns a correct fd, but read function returns "resource temporarily unavailable".

    Does someone has an idea about this problem ?

    Thanks for help !

    int main(int argc, char **argv)
    	int fd;
    	int ret;
    	char c;
    	printf("open /dev/tty.usbserial...\n");
    	fd = open("/dev/tty.usbserial", O_RDONLY);
    	printf("...Serial port open - Return code : %d\n", fd);
    	if (fd < 0)
    		printf("Erreur a l'ouverture de /dev/tty.usbserial - Code=%d\n", fd);
    	printf("Serial port opened...\n");
    	while (1)
    		ret = read(fd, &c, 1);
    		if (ret == 1)
    			printf("%c", c);
    			perror("Error read ");
    			return 1;
    	printf("close /dev/tty.usbserial\n");
    	return 0;

  2. #2

    Member Since
    Oct 23, 2009
    Mac Mini
    Re: problem on open

    You can try the device /dev/cu.usbserial instead (check with ls /dev if it exists).
    I think you must open using O_RDWR.
    Do you need to write a command to the port before reading?
    You can also try to set the speed to 9600.
    If that doesn't work try looping until read returns 1.

    I hope this helps.


    strcpy(bsdPath, "/dev/cu.usbserial");
    fileDescriptor = open(bsdPath, O_RDWR);
    if (-1 == fileDescriptor)
    return EX_IOERR;

    struct termios theTermios;
    memset(&theTermios, 0, sizeof(struct termios));
    cfsetspeed(&theTermios, 9600);
    theTermios.c_cflag = CREAD | CLOCAL; // turn on READ and ignore modem control lines
    theTermios.c_cflag |= CS8;
    theTermios.c_cc[VMIN] = 0;
    theTermios.c_cc[VTIME] = 10; // 1 sec timeout
    int ret = ioctl(fileDescriptor, TIOCSETA, &theTermios);

    ret = read(fileDescriptor, &c, 1);

  3. #3

    Member Since
    Oct 20, 2009
    iMac 17
    YEAH ! It works !

    Thank you very very much for your help : it was effectively necessary to change /dev/tty.usbserial to /dev/cu.usbserial and also to define speed of the line.

    I can now start real work from these inputs.

    Once again, thanks !!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. "New Thread" Button in Dev Forums??
    By markgrant in forum macOS - Apps and Games
    Replies: 2
    Last Post: 07-29-2015, 03:40 PM
  2. USB to Serial tty.usbserial Problem HELP
    By ubila in forum macOS - Operating System
    Replies: 1
    Last Post: 09-19-2011, 05:30 PM
  3. VMWare - Operation on file "dev/rdiskOs3" failed
    By mschmir in forum Running Windows (or anything else) on your Mac
    Replies: 2
    Last Post: 01-24-2011, 11:16 AM
  4. problem opening /dev/tty.iap
    By jacksmax in forum macOS - Development and Darwin
    Replies: 0
    Last Post: 12-08-2009, 06:56 PM
  5. How to create a link to a device like /dev/
    By Philipina in forum macOS - Development and Darwin
    Replies: 4
    Last Post: 06-22-2006, 07:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts