Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


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

Rep: Reputation: 0
Serial Communication


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;

int fd,c, res;
struct termios oldtio,newtio;
char buf[255];

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 */


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

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


strcpy(buf,"82345\n") ;



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)


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

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.

Suraj Swami,
Old 06-25-2009, 08:25 AM   #2
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741
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

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

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

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