View Full Version : Problem of serial programming

05-09-2005, 11:11 AM
I am trying to use an Imada DPS-1 Force Digital Force Guage under Debian Linux system via RS-232 serial port.
I checked "the Serial Programming Guide for POSIX Operating System" and have my own code as shown in the end.
This is just a testing code to see whether I can communicate well with the force guage. I tried to write 2 ASCII characters "Q" and CR to the port. As suggested by the force guage manual, this is to turn off the power of the force guage.
However, though no "Unable to open /dev/ttyS0!" or "Writing failed" message showed up, the command "Q\r" doesn't turn off the power at all.
I also added more code to this simple one to set the attributes of the serial port, like the baudrate, whether it is a canonical process or not, etc, though I think this is only necessary for reading the port. And it still couldn't work.
I used to use the MSComm class to do the same thing under MS Windows, and it works perfectly (which means the product manual can't be wrong). But it just can't work when I switch to Linux.

Could anyone help me out of this problem? It's really killing me.

Here's the code:

#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */

int fd;
int n;

if ((fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY)) == -1)
perror("Unable to open /dev/ttyS0!\n");
else printf("/dev/ttyS0 succesfully opened, fd=%i.\n",fd);

fcntl(fd, F_SETFL, O_RDWR);

n = write(fd, "Q\r",2);
if (n<0) printf("Writing failed!\n");
else printf("%i bytes written to /dev/ttyS0.\n",n);

Thank you very much.

05-09-2005, 11:20 AM
> if ((fd = open("./test.dat", O_RDWR | O_NOCTTY | O_NDELAY)) == -1)
You appear to be opening a file called test.dat in the current directory, not a device.

05-09-2005, 12:32 PM
That's a stupid mistake. I posted the trial version of my code. I did use \dev\ttyS0 in my real code.

05-10-2005, 10:41 AM
If you call perror() when you have an error, it should print out some additional information (in addition to the string you specify).
What did you get?

05-11-2005, 10:54 AM
The problem is I didn't get any error message when I run the program. So the perror() has not been called at all. However, the annoying force guage just didn't follow my command.
Thank you for your reply.

05-11-2005, 11:05 AM
Sorry - my mis-read.
I thought your original post was reporting errors, where you said there were no errors.

Have a rummage in the termios part of the manual to see how to read/write the terminal line characteristics (baud rate, start bits, stop bits, parity).

From what you were saying about windows working, you should be able to figure out what setting should be applied. Indeed, you could use hyperterm to work it out directly.