Solved

Serial port program help

Posted on 2004-04-15
7
3,102 Views
Last Modified: 2013-11-15
Heres the deal.
Ive got two boxes, one running Windows and the other running Linux.
Ive got the two boxes connected with a serial cable on COM1.
Ive got a Teraterm Pro session open on the Windows box and my program running on my Linux box.
When i type a key e.g. A on the Teraterm window, i wnt it to be displayed on the Linux box screen.

Im baffled why it wont work, but then again a complete newbie!

Heres my code, so if anyone can point me in the right direction id really appreciate it.

#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 n, fd; /* File descriptor for the port */
struct termios options;
char inBuffer[100];

      fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
      if(fd == 0)
      {
          perror("open_port: Unable to open    /dev/ttyS0 -");
                 return;
      }
      else
             {
         fcntl(fd, F_SETFL, 0);
      }

      /*Configire port*/
      /*Get the current options for the port*/

      tcgetattr(fd, &options);

      /*Set the Baud rates to 19200*/

      cfsetispeed(&options, B19200);
      cfsetospeed(&options, B19200);

      /*Enable received and set local mode*/

      options.c_cflag |= (CLOCAL | CREAD);

      /*Set new options for port*/

      tcsetattr(fd, TCSANOW, &options);

      /*Set data bits*/

      options.c_cflag &= ~CSIZE;    /*Mask the character size bits*/
      options.c_cflag |= CS8;       /*Select 8 data bits*/

      /*Set RAW input*/

      options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

      /*Set Raw output*/

      options.c_oflag &= ~OPOST;

      /*Set timeout to 1 sec*/

      options.c_cc[VMIN] = 0;
      options.c_cc{VTIME] = 10;

            /* read characters from port */

             n = read(fd, inBuffer, 1);                      /* read 1 byte*/

             /* check if we got anything */
             if (n)
             {
         /* examine the bytes and do something with them */

         /* then reply to sender if needed */
                write(fd, "OK", 2);
             }


      close(fd);
}

Thanks
0
Comment
Question by:aaronmcl
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Expert Comment

by:booki
ID: 10835583
aaronmcl,

Is your serial cable nulled (null modem)? The transmit and recieve pins have to be switched.. you can't use a regular serial cable (with or without gender changer).

b.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 10835586
Hi aaronmcl,

You might actually be reading the data, but the return is getting buffered so that you don't see it.

Write a message to stderr to show that the character was received.


             n = read(fd, inBuffer, 1);                      /* read 1 byte*/

             /* check if we got anything */
             if (n)
             {
        /* examine the bytes and do something with them */

                fprintf (stderr, "Got %c\n", n > 0x20 ? n : ' ');

        /* then reply to sender if needed */
                write(fd, "OK", 2);
             }

 


Good Luck,
Kent
0
 

Author Comment

by:aaronmcl
ID: 10835739
Kent

Tried what you suggested but no difference.

0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 45

Assisted Solution

by:Kdo
Kdo earned 70 total points
ID: 10835781

I also misspoke/mistyped.  sigh....

You want to test what was read, not the count.

               fprintf (stderr, "Got %c\n", *inBuffer > 0x20 ? *inBuffer : ' ');


Does the program exit immediately or wait until a key is pressed on the other system?

When the key is pressed does the linux program exit immediately?

Following booki's lead, how are the two systems cabled together?  If you're using "standard" ethernet cables you'll need a hub/switch/router in between the systems to manage the send/receive signals.  Without the additional electronics you'll need a cross-over cable.


Kent
0
 
LVL 4

Accepted Solution

by:
booki earned 55 total points
ID: 10837646
aaronmcl,

Aside from a mistyped curly brace your code works fine.

>>    options.c_cc{VTIME] = 10;

         options.c_cc[VTIME] = 10;

I tested by sending from winxp (first from a little prog I wrote then also with hyperterminal) and recieving on a linux box.  Baud rate 9600 and 19200 worked fine.

You're probably not using a null modem/crossover cable.

A null cable is laid out as follows, if you want to make your own:
1&6     4
4           1&6
2           3
3           2
5           5
7           8
8           7

*source: idt,  Q_20935708

b.
0
 
LVL 4

Expert Comment

by:booki
ID: 10837707
aaronmcl,

One more thing... All changes you make to your termios struct "options" AFTER calling tcsetattr() have no effect.  Therefore, as you may have noticed, your call to read() blocks despite having set VMIN of 0.  It looks like a default of 1 is used.

b.
0
 

Author Comment

by:aaronmcl
ID: 10840142
Sorry for the delay in replying.
Ill give your ammended code a try Kent.
booki im using a crossover cable so the cable is fine and i had previously corrected the curly brace, just a typo.
Ill try recompiling when i get home but if youve tried it already booki then im confident it'll work.

Thanks for the help guys appreciated!

0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If your app took Google’s lash recently, here are the 5 most likely reasons.
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Using Adobe Premiere Pro, the viewer will learn how to set up a sequence with proper settings, importing pictures, rendering, and exporting the finished product.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question