LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


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

Rep: Reputation: 0
Serial Communication Code.


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, 10:04 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
A quick checklist, before digging into your code:
1. Does the serial port work with other applications (minicom, C-Kermit, hyperterminal),
a. on any OS?
b. on this installed OS?
- check dmesg for report from serial port driver.
(is this a 'real' serial port, and not a USB-serial dongle?)
2. Does the serial port have appropriate access permissions?
3. What possible pre-existing settings may be in effect (stty -a -F /dev/ttyS0)
- Look for hardware flow control that may not be satisfied.

--- rod.
 
  


Reply



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 1 06-25-2009 07:25 AM
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
Help! Serial Communication Port programming in Assembly(Partial code inside) laksi Programming 4 10-12-2004 01:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:24 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