Enable operation on systems that have a blocking open
Introduction
On some systems (certainly on Slackware 7.1.0), a call to open for a serial port will not return unless one of the hardware handshaking lines is set (DCD I think). This means that with the standard serial cable all of the *2300 commands hang. It is probably possible to fix this by changing the configuration of the serial port, but since linux versions seem to vary on the best way to do this from distro to distro, I made changes to the open code in linux2300.c.
Description of Patch
The code in open_weatherstation has been changed to use non blocking calls. I also changed the lock code so that the code exits if the port is locked. This works for me but may not be correct for general use.
Installation of Patch
Below are the source code changes
WEATHERSTATION open_weatherstation(char *device)
{
WEATHERSTATION ws2300;
struct termios adtio;
int portstatus, fdflags;
//Setup serial port
// Changed by AS to use non blocking open
if ((ws2300 = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0)
{
printf("\nUnable to open serial device %s\n", device);
exit(EXIT_FAILURE);
}
if ( flock(ws2300, LOCK_EX|LOCK_NB) < 0 ) {
perror("Serial device is locked by other program");
exit(EXIT_FAILURE);
}
if ((fdflags = fcntl(ws2300, F_GETFL)) == -1
|| fcntl(ws2300, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
{
perror("couldn't reset non-blocking mode");
exit(EXIT_FAILURE);
}
Change History of Patch
Kenneth I hope this is the correct place to post this change and that posting the source code changes rather than a patch file is ok.
Thanks for your patch.
Patches as code segments, real diff files, entire source files. Whatever it takes. As long as you share your improvements, the open source community is thankful.
I am not sure there is a huge difference between the code in the distributions. I think the initial settings of the serial bus varies and also depends on what other programs that have previously setup the port. It could even be an unused modem driver.
--
KennethLavrsen - 31 Dec 2005
Using O_NOCTTY on a Macintosh G4 with a USB/Serial converter causes the program to hang as it waits for the signal forever. Applying the patch as
if ((ws2300 = open(device, O_RDWR | O_NONBLOCK)) < 0)
works.
--
PeterTattersall - 26 Jan 2006
I have integrated this patch in my 1.11 sources.
--
KennethLavrsen - 19 Jul 2006
Thanks for the work. I had the same problem.
Unfrotunately, this fix does not work for me :
- old version : if I run two programs, say fetch2300, from two different terminals simultaneously (i.e. start the second before the first one has returned), then both block.
- new version : when I run the second program, I returns immediately with the message telling me the port is locked. Fine. But the first call gets blocked by the second attempting an access...
This is important for me, as I intend to run the interactive web page constantly (or at least frequently), and have histlog2300 run once a day. I'll be damn lucky if they don't interact some day or other.
Thanks all the same for the work, and thanks to all for this great software, that help to justify my choice to jump to the ubuntu bankwagon, comming from win thingy.
JP
--
JohnBerry - 04 May 2015