Thread: Problem of serial programming

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    3

    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.
    Last edited by fingerling54; 05-09-2005 at 12:31 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    3

    Sorry

    That's a stupid mistake. I posted the trial version of my code. I did use \dev\ttyS0 in my real code.
    Last edited by fingerling54; 05-09-2005 at 12:57 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    3

    Unhappy

    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.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. serial port to poll on request
    By infineonintern in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 06:52 AM
  2. Serial Port Questions
    By valaris in forum Tech Board
    Replies: 2
    Last Post: 05-22-2009, 08:26 AM
  3. sending data over UDP from serial port
    By forumguy in forum Linux Programming
    Replies: 0
    Last Post: 04-25-2009, 02:10 PM
  4. PC104 Serial Port Programming Problem
    By outerspace in forum C Programming
    Replies: 6
    Last Post: 11-09-2005, 07:07 PM
  5. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM