Go Back  Mac-Forums.com  > Software > OS X - Darwin and Development > problem on open(/dev/tty.usbserial"....)

Reply
 
LinkBack Thread Tools
Old 10-20-2009, 04:25 PM   #1 (permalink)
omr_treb22

 
Member Since: Oct 20, 2009
Location: France
Posts: 2
omr_treb22 is on a distinguished road
Mac Specs: 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 !

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;
}
omr_treb22 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Stumble this post
Reply With Quote
Old 10-23-2009, 06:18 AM   #2 (permalink)
laplace

 
Member Since: Oct 23, 2009
Location: Sweden
Posts: 1
laplace is on a distinguished road
Mac 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);
laplace is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Stumble this post
Reply With Quote
Old 10-24-2009, 01:13 PM   #3 (permalink)
omr_treb22

 
Member Since: Oct 20, 2009
Location: France
Posts: 2
omr_treb22 is on a distinguished road
Mac Specs: iMac 17
Smile

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 !!!
omr_treb22 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Stumble this post
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
diagnosing kernel task/CPU problem on Macbook Air nick_harambee Apple Notebooks 6 11-14-2009 10:34 PM
Possible faulty CPU thermometer, common problem? dyaballikl Apple Notebooks 6 11-16-2008 01:21 AM
Microsoft Messenger Problem millenium_spike OS X - Applications and Games 4 09-16-2008 02:11 AM
Problem Darwin Server + VLC nanaki Web Design and Hosting 0 03-13-2008 08:27 AM
Problem With Keyboard...... mmcltd OS X - Operating System 1 06-14-2006 12:17 PM

 
 
 
     
Home Calendar Get New
     

Copyright ©2001-2009 Mac-Forums.com. All Rights Reserved. A division of iNET Interactive.

Other iNET Interactive Sites: Web Hosting Talk | Swish Talk | Hosting Catalog.com| Dev Papers| Deleted Domains| Hot Scripts

Powered by vBulletin
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.