LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-22-2013, 04:05 AM   #1
ynmly
LQ Newbie
 
Registered: Jan 2013
Posts: 2

Rep: Reputation: Disabled
serial communication through rs232 in linux c


hi all,
i am new in linux, i am facing problem in serial communication
actually i have two computer on one hyper terminal is running and on another i am writing c program to communicate with hyper terminal.both PC are connected through rs232.
i have written some code and i am able to send data to hyper terminal.but i am not getting how i can read data from hyper terminal and write in linux PC.
any suggestion?
thanks in advance.

Below are my code

int main(int argc, char **argv)
{
struct termios options;
int fd;

fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1)
{
printf("Could not open port /dev/ttyS0\n");
return 1;
}

tcgetattr(fd, &options); //get port options

cfsetispeed(&options, B9600); //set input baud rate
cfsetospeed(&options, B9600); //set output baud rate

options.c_cflag |= (CLOCAL | CREAD); //enable receiver and set local mode

options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag &= ~CRTSCTS; //disable hardware flow control
options.c_cflag &= ~(ICANON | ECHO | ISIG); //set raw input

options.c_cflag |= IXOFF; //disable software flow control

tcsetattr(fd, TCSANOW, &options); //set new port options

sleep(1);
int rc,count;
int size = 8;
unsigned char buf[10];
FILE *fp = NULL;
char ch;
int i=0;
printf("enter the data you want to send");

while((ch=getchar())!='\n')
{
write(fd,&ch,1);
}
close(fd);

printf("Finished.\n");

return 0;
}
 
Old 01-22-2013, 10:02 PM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
You have nothing there to read from the terminal.

Note - if you expect asynchronous input/output, you have to either use separate threads, or separate processes.

If you can write to the device as you say, your setup is done. All you need is a read(fd,...) loop instead of a write.
 
Old 01-22-2013, 10:15 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
It's not simultaneous I/O, but I implemented this setup not too long ago for a system that would send commands to a device, then read the response to check for an "OK" or "ER" response:
Code:
  fstep = (2.0/ssclk)*swpsp/1e6;
  sprintf(buffw,"fd %14.12lf\n",fstep);
  write(sfd,buffw,18);
  read(sfd,buffr,20);
  if (buffr[0] != 'O' && buffr[1] != 'K') {
    fprintf(stderr,"Communication error with synthesizer\n");
    fprintf(stderr,"Response to fd command was %c%c\n",buffr[0],buffr[1]);
    exit(0);
  }
  sleep(1);

and so on to the next command
buffw and buffr are of course two character buffers, and sfd is the file descriptor for the serial device.

Note that I am in NO way shape or form a decent C programmer. I'm an IDL/Fortran guy who can fudge my way through C to get something done, so there is most likely a much better way to implement some of what I was doing. However, it may give you some insight into a possible way to read from the serial device. If you spawn the read in a separate thread from your write, you should be able to get more-or-less simultaneous read/write capability.
 
Old 01-23-2013, 03:17 AM   #4
ynmly
LQ Newbie
 
Registered: Jan 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jpollard View Post
You have nothing there to read from the terminal.

Note - if you expect asynchronous input/output, you have to either use separate threads, or separate processes.

If you can write to the device as you say, your setup is done. All you need is a read(fd,...) loop instead of a write.
thanks jpollard for your response ....that what i have given you is able to do communication with hyper terminal. i.e one way communication.when i run this code i am able to send data from linux box to hyper terminal...i am facing problem how to get data when i send data from hyper terminal to linux PC
can you plz suggest me with sample code?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
rs232 communication speeds over Embedded linux ARM gigin Linux - Mobile 5 11-11-2010 02:21 AM
[SOLVED] Serial RS232 Communication to Embedded Device on Laptop - No DB9 or DB25 idoneous Linux - General 4 09-06-2010 05:29 PM
USB-RS232 communication problem (I think) damip Linux - Hardware 1 10-31-2007 09:57 AM
Serial (RS232) communication is one-way anroy Linux - Hardware 5 08-25-2007 02:35 PM


All times are GMT -5. The time now is 01:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration