LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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


Closed Thread
  Search this Thread
Old 06-25-2009, 04:10 AM   #1
Suraj Swami
LQ Newbie
 
Registered: Jun 2009
Posts: 7

Rep: Reputation: 0
Serial Communication


Hi,

I am having problems in writing a simple serial communication code in linux. I want to send single charector on the serial port which my hardware will receive and execute.
Here is the code.

**************************************************************************************************** ********
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>


#define BAUDRATE B9600
#define MODEMDEVICE "/dev/ttyS0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;

main()
{
int fd,c, res;
struct termios oldtio,newtio;
char buf[255];

fd = open(MODEMDEVICE, O_RDWR | O_NDELAY );
if (fd <0) {perror(MODEMDEVICE); exit(-1); }

tcgetattr(fd,&oldtio); /* save current port settings */

bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = OPOST;

/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;

cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);

newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
newtio.c_cc[VMIN] = 0; /* blocking read until 5 chars received */

tcsetattr(fd,TCSANOW,&newtio);

printf("1\n"); // This is to test where the code has reached.

tcflow(fd, TCOON );
tcflush(fd, TCIFLUSH);
tcflush(fd, TCOFLUSH);

printf("2\n");

strcpy(buf,"82345\n") ;

printf("3\n");


write(fd,buf,1);
tcdrain(fd);



printf("Work done\n");

// I never got to see "work done" in the output. The terminal would hang . If I removed write(fd,buf,1) then work done would print but then my main purpose of the program is lost. It even worked for write(1,buf,1) . So I guess the problem is with write(fd,buf,1)

tcsetattr(fd,TCSANOW,&oldtio);
}

**************************************************************************************************** ***

I had one more observation. When I tried to open proc folder it would not open.

Please suggest. Am I missing in some configuration for the output.

I am using ret hat ver 5.0 its a pentium processor. The hyper-terminal is working in windows very well.


Regards,
Suraj Swami,
suraj.g.swami@gmail.com
 
Old 06-25-2009, 07:25 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.
 
  


Closed Thread



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
Serial Communication Suraj Swami Linux - Newbie 3 06-23-2009 11:22 AM
Serial Communication help mitchell2345 Linux - Software 2 03-20-2009 08:41 PM
Serial Communication freeindy Programming 2 04-04-2007 08:24 AM
ip communication through serial connection pcsaji Linux - Networking 3 12-14-2006 01:50 AM
serial port communication prems Linux - Newbie 1 04-17-2005 02:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:47 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
Open Source Consulting | Domain Registration