Problem of serial programming
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:
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 */
main()
{
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);
close(fd);
}
Thank you very much.