New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

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


Post Reply New Thread Subscribe

 
Thread Tools
omr_treb22

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

omr_treb22 is offline
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;
}
QUOTE Thanks
laplace

 
Member Since: Oct 23, 2009
Location: Sweden
Posts: 1
laplace is on a distinguished road
Mac Specs: Mac Mini

laplace is offline
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);
QUOTE Thanks
omr_treb22

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

omr_treb22 is offline
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 !!!
QUOTE Thanks

Post Reply New Thread Subscribe


« Interface Builder Noob | how to open Midnight Commander(MC) menu? »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
Forum Jump

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

All times are GMT -4. The time now is 04:32 AM.

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

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?