problem on open(/dev/tty.usbserial"....)

Joined
Oct 20, 2009
Messages
2
Reaction score
0
Points
1
Location
France
Your Mac's Specs
iMac 17
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 !

Code:
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);
		exit(1);
		}
	
	printf("Serial port opened...\n");
	
	while (1)
	{
		ret = read(fd, &c, 1);
		if (ret == 1)
			printf("%c", c);
		else
		{
			perror("Error read ");
			close(fd);
			return 1;
		}
	}
	
	printf("close /dev/tty.usbserial\n");
	close(fd);
	return 0;
}
 
Joined
Oct 23, 2009
Messages
1
Reaction score
0
Points
1
Location
Sweden
Your Mac's Specs
Mac Mini
Re: problem on open

Hi,

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.

Regards,
Magnus

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));
cfmakeraw(&theTermios);
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);
 
OP
O
Joined
Oct 20, 2009
Messages
2
Reaction score
0
Points
1
Location
France
Your Mac's Specs
iMac 17
YEAH ! It works ! :D

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 !!!
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top