473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Problem with fopen, linux, and RS-232, possibly

I have a temperature sensor device we use to shutdown our servers. It
relys on fopen() blocking when the serial port device isn't ready.
Unfortunately that's not what's happening. The fopen() returns
immediately without an error. What have I got wrong?

Basically when it gets too hot the device connects the 8 and 6 lines
to line 20. That should, as I understand it, tell the fopen() that
the file is available and cause the rest of the program to execute.

The code is below. I compile it with "gcc -o minmon minmon.c" and
invoke it as "minmon /dev/tyyS0" on SuSE9 with gcc 2.95.3

#include <stdio.h>
#include <signal.h>

int await_event(char* port)
{
FILE *file;
int fd;

if ((file = fopen(port, "r")) != NULL) /*blocks*/
fclose(file); /*no longer need port*/
else
{
fprintf(stderr, "ERROR opening port %s", port);
return -1;
}
return 0;
}

main (int argc, char* argv[])
{
int pid, getuid();
char* port, *script;

if (argc != 2 )
{
fprintf(stderr, "Usage:");
fprintf(stderr, "\t%s device\n", argv[0]);
exit(3);
}
else /*run in diagnostic mode*/
{
port = argv[1];
printf("watching on port: %s\n", port);
if (await_event(port) == 0) /*blocks*/
{
printf("%s: diagnostic power out\n", argv[0]);
/*insert shutdown code here*/
}
else
printf("%s: ERROR while waiting for event\n",
argv[0]);
exit(0);
}
}
Nov 14 '05 #1
3 4723
alp
Hi Gregory,

Are those pins you specified the DCD signal?
I am not sure how fopen() is calling open(), but you want to make sure
open() is called WITHOUT the O_NDELAY flag.
Also, you may want the O_NOCTTY flag to be present.

Try writing the program using open() instead, and specify the exact
flags.
You can also pass fcntl()s to the device.

If this is not DCD signal, and rather some obscure pins than the driver
may be unaware of its "readiness", then any open() call may not block.
Whether the device is actually capable of sending data or not. I
haven't checked the serial pinout to verify.

Here is some reading that may be of assistance.
http://www.easysw.com/~mike/serial/serial.html#2_5
Hope this helps.

Art Perry
Linux Systems/Software Engineer

Nov 14 '05 #2
On Wed, 8 Dec 2004, Gregory Graham wrote:

Hi Gregory,

Are those pins you specified the DCD signal?
I am not sure how fopen() is calling open() (I didn't bother checking),
but you want to make sure that open() is called WITHOUT the O_NDELAY flag.
Also, you may want the O_NOCTTY flag to be present.

Try writing the program using open() instead, and specify the exact flags.
You can also pass fcntl()s to the device.

If these pins are not DCD signal, and rather some obscure pins then the
driver may be unaware of its "readiness". Then any open() call may not
block, whether the device is actually capable of sending data or not. I
haven't checked the serial pinout to verify.

Here is some reading that may be of assistance.
http://www.easysw.com/~mike/serial/serial.html#2_5

Hope this helps.

Art Perry
Linux Systems/Software Engineer

I have a temperature sensor device we use to shutdown our servers. It
relys on fopen() blocking when the serial port device isn't ready.
Unfortunately that's not what's happening. The fopen() returns
immediately without an error. What have I got wrong?

Basically when it gets too hot the device connects the 8 and 6 lines
to line 20. That should, as I understand it, tell the fopen() that
the file is available and cause the rest of the program to execute.

The code is below. I compile it with "gcc -o minmon minmon.c" and
invoke it as "minmon /dev/tyyS0" on SuSE9 with gcc 2.95.3

#include <stdio.h>
#include <signal.h>

int await_event(char* port)
{
FILE *file;
int fd;

if ((file = fopen(port, "r")) != NULL) /*blocks*/
fclose(file); /*no longer need port*/
else
{
fprintf(stderr, "ERROR opening port %s", port);
return -1;
}
return 0;
}

main (int argc, char* argv[])
{
int pid, getuid();
char* port, *script;

if (argc != 2 )
{
fprintf(stderr, "Usage:");
fprintf(stderr, "\t%s device\n", argv[0]);
exit(3);
}
else /*run in diagnostic mode*/
{
port = argv[1];
printf("watching on port: %s\n", port);
if (await_event(port) == 0) /*blocks*/
{
printf("%s: diagnostic power out\n", argv[0]);
/*insert shutdown code here*/
}
else
printf("%s: ERROR while waiting for event\n",
argv[0]);
exit(0);
}
}

Nov 14 '05 #3
>I have a temperature sensor device we use to shutdown our servers. It
relys on fopen() blocking when the serial port device isn't ready.
Unfortunately that's not what's happening. The fopen() returns
immediately without an error. What have I got wrong?
Sometimes serial ports have several associated devices.
Some of them wait for carrier detect ("dial-in") and some
of them don't ("dial-out").
Basically when it gets too hot the device connects the 8 and 6 lines
to line 20.
DTR -> DSR and DTR -> DCD . This is typical of a quick-and-dirty
way for a UPS to signal a power failure when batteries are
near to exhausted.

That should, as I understand it, tell the fopen() that
the file is available and cause the rest of the program to execute.


Gordon L. Burditt
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: kaiwing18 | last post by:
Hi , Please see this code: ResultSet rs=course.showStudent(); while(rs.next()) { out.println(rs.getString("name")); }
5
by: richard | last post by:
I have a simple test to pass information from a client to a server using named pipe. what I really want is: when I type a line on the client, the server will output the line immediately. but to my...
14
by: Aaron Couts | last post by:
I have a program that writes to a log file. It's compiled on RH Linux 7.3 (Kernel 2.4.18-18.7). It's using fopen in append mode. When the file reaches 51200000 bytes in size, the program will no...
7
by: git_cs | last post by:
Hey, guys and gals Somedays ago, I had asked for the DES algorithm in C language. Although I have written the algorthim in C myself, I am facing a peculiar problem, which I hope some of u guys and...
3
by: Michael | last post by:
I'm having trouble opening a file for writing under Red Hat Fedora Linux. Here is the statement: $fp = fopen("/var/www/html/test01.txt","w"); It generates the following error msg: ...
11
by: aldrin | last post by:
I'm trying to run this code under windows xp sp2 using codeblocks v1.0 compiler with great difficulty.There is no problem with running this under KDevelop in linux. Any help would be greatly...
5
by: xieliwei | last post by:
I have a freshly installed openSuSe 10.2 with PHP4 from http://download.opensuse.org/repositories/home:/michal-m:/php4/openSUSE_10.2/ (openSuSe abandoned PHP4 since version 10, but I have customers...
3
by: IamtheEvster | last post by:
Hi there, I'm using fopen for the first time and I know I'm running into a permissions problem, but I can't seem to resolve it and any help would be greatly appreciated. I'm running PHP5 and...
14
by: anchitgood | last post by:
Hey, I have developed the following code and it is executing well on windows. But I don't have LINUX installed on my system. I would be grateful to you guys if you check whether this code is...
31
by: Bill Cunningham | last post by:
Is there a real difference in r+ and w+ ? I read in my man pages that r+ opened a file for reading and writing while w+ opened a file for reading and writing and if the file didn't exist it would...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.