Solved

Serial port program help

Posted on 2004-04-15
7
3,095 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
Comment Utility
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
Comment Utility
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
Comment Utility
Kent

Tried what you suggested but no difference.

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 45

Assisted Solution

by:Kdo
Kdo earned 70 total points
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Today companies are subjected to more-and-more data, and it won't stop any time soon.  But there are obvious opportunities for reducing data, particularly data duplicated among companies.
Let’s list some of the technologies that enable smooth teleworking. 
The viewer will learn how to create multiple layers to apply various filters and how to delete areas from each layer’s filter.
Viewers will learn how to use the Hootsuite Dashboard.

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now