Solved

Serial port program help

Posted on 2004-04-15
7
3,108 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Kent Olsen
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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
This video shows how use content aware, what it’s used for, and when to use it over other tools.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 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