Serial write timeout?

Hi,

I am trying to send data through the serial port. I'm not having any problems with that, but how can I detect when something goes wrong while writing data to it?

Currently I am writing data to it one byte at a time. If, during a large transmission, I disconnect the serial cable, my program just hangs on the write command. Is there any way to set up a timeout period for the write operation?

I am just using the standard write command with a file descriptor.

Cheers,
Dave
dwagnerAsked:
Who is Participating?
 
moduloConnect With a Mentor Commented:
PAQed, with points refunded (100)

modulo
Community Support Moderator
0
 
paulqnaCommented:
I guess this relates to your problem as well...?

http://www.linuxdoc.org/HOWTO/Serial-Programming-HOWTO-3.html

and look for:
while (STOP==FALSE) {     /* loop until we have a terminating condition */
0
 
dwagnerAuthor Commented:
Hi,

It is a little different in my case because I am writing rather than waiting for input. As far as I can see there is no way to automatically have a timeout value for writing to a resource.

However, I solved my own problem. I now access the serial port in non-blocking mode (using the O_NONBLOCK flag when opening it) instead of a blocking mode.

I then have a little bit of code that tries to write a byte to the resource for a specified period of time. If it fails because the file is busy it will try sending again. It gives up after an unexpected error or the time I want to retry for has been exceeded.

Kind of simple when I thought of it. I should have waited those 5 minutes longer before posting here :)

Thanks for your help though. I'll leave this question open in case anyone has a better solution.


Dave
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
mrn060900Commented:
What about using checksum, after you've sent the data, could you then retrieve it and compare the checksum?

This a just a wild stab in the dark.

Regards Mike
0
 
dwagnerAuthor Commented:
Hi,

That would spot corrupt data but wouldn't help the original problem of having a "write" just hang if transmission was interupted (for example by ripping the serial cable out of the port like I did :).

I'm not too worried if the machine at the other side of the serial cable receives the data OK. It can work that out from a CRC transmitted with the data and then it can decided if it wants to try getting the data again.

CRCs... I still don't fully understand the maths... Good job there is plenty of source code out there that I can use :)

Dave
0
 
jmcgOwnerCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

PAQ with points refunded

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.