Solved

Read/Write to Linux serial port using C or C++

Posted on 1997-06-24
2
953 Views
Last Modified: 2013-12-26
OS Version: Red Hat Linux release 3.0.3
Language: GNU gcc 2.7.2-2 or g++ 2.7.2-2

I need an example of how to write and read characters to the /dev/cua0 device (COM1) in Linux.  I need to communicate at a baud rate of 1200, 8 data bits, 1 stop bit, and no parity.  (1200,N,8,1).

I have tried the following code fragement:
--------------------
 if ((serial_fd = open("/dev/cua0",O_RDWR))==-1) {
    printf("Error opening /dev/cua0\n");
    return -1;
  }

  buf[0] = (char)1;
  buf[1] = (char)24;
  buf[2] = (char)230;
  i = write(serial_fd,buf,3);
  printf("wrote %d bytes\n", i);

  i = read(serial_fd, buf, 4);
  printf("read %d bytes, errno=%d\n", i,errno);      
-----------------

It writes the characters out.  I put a tester (on the port) with LEDs that flickers when the program sends characters to the port.  However I don't know how to set the port to no parity, 8 data bits, and 1 stop bit.  I was able to use the setserial command to set the baud rate.  

Does anyone have an example on how to do this?
It can be in either C or C++.
0
Comment
Question by:mag062397
  • 2
2 Comments
 
LVL 3

Accepted Solution

by:
dhm earned 100 total points
ID: 1293713
Check the termios(3) functions.  The ones you want are tcgetattr() [get current settings] and tcsetattr() [change settings].  Your code would look like this:
------------------------------------------------------
struct termios old_settings;
struct termios new_settings;

tcgetattr( serial_fd, &old_settings );
new_settings = old_settings;
cfsetispeed( &new_settings, B1200 );
cfsetospeed( &new_settings, B1200 );
new_settings.c_cflag &= ~(PARENB | CSTOPB);
tcsetattr( serial_fd, &new_settings );
-----------------------------------------------
Cfset[io]speed() are convenience functions that set the appropriate bits in the terminal control structure; you could use bitwise ANDs and ORs to accomplish the same thing.  In the next to last line, I clear the Parity-Enable and 2-Stop-Bits flags.  Incidentally, the reason I have two termios structures (old_settings and new_settings) is that it's polite to restore the terminal to the state you found it when your program exits.
0
 
LVL 3

Expert Comment

by:dhm
ID: 1293714
Whoops, forgot to set 8-bit mode...add these lines before the tcsetattr():

new_settings.c_cflag &= ~CSIZE;
new_settings.c_cflag |= CS8;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error on moodle after upgrade 3 123
sum67 challenge 35 93
canBalance challenge 34 70
firstChar challenge 13 110
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

920 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

16 Experts available now in Live!

Get 1:1 Help Now