LinuxQuestions.org
Visit Jeremy's Blog.
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 12-01-2009, 12:50 PM   #1
rkjnsn
LQ Newbie
 
Registered: Jun 2009
Posts: 4

Rep: Reputation: 0
Trouble reading from serial port.


I am trying to write a very simple test program that communicates with a serial device. What I want to accomplish is very simple:
1. Wait for a keypress
2. Send a 4 byte command to the device
3. Wait for it to send a byte back
4. Go to step 1

The program successfully sends the command to the device (the device carries out the action), but it never receives the reply byte back, instead blocking on the read forever. I have verified that the device does indeed send the response byte by sending it commands using gtkterm, and the response character is displayed every time.

Frankly, I am quite stumped, as I have looked at many examples and tried several different things. My current code is as follows:
Code:
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#define BAUDRATE B9600
#define DEVICE "/dev/ttyUSB0"

main ()
{
	int fd;
	struct termios oldtio, newtio;

	fd = open(DEVICE, O_RDWR | O_NOCTTY);

	tcgetattr(fd, &oldtio);

	memcpy(&newtio, &oldtio, sizeof(struct termios));
	newtio.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
		| INLCR | IGNCR | ICRNL | IXON);
	newtio.c_oflag &= ~OPOST;
	newtio.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
	newtio.c_cflag &= ~(CSIZE | PARENB);
	newtio.c_cflag |= CS8;
	newtio.c_cc[VMIN]=1;
	newtio.c_cc[VTIME]=0;
	cfsetispeed(&newtio, B9600);
	cfsetospeed(&newtio, B9600);
	tcsetattr(fd, TCSAFLUSH, &newtio);
	
	while(1)
	{
		char response;
		if (getchar() == 'q')
			break;
		write(fd, "\xA5" "\x5A" "\x0A" "\xF5", 4);
		read(fd, &response, 1)
	}

	tcsetattr(fd, TCSANOW, &oldtio);
	close(fd);

	return 0;
}
P.S.
The settings that work with gtkterm are:
Speed: 9600
Parity: none
Bits: 8
Stopbits: 1
Flow control: none

Last edited by rkjnsn; 12-01-2009 at 12:52 PM.
 
Old 12-02-2009, 06:07 AM   #2
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Code:
#define DEVICE "/dev/ttyUSB0"
shouldn't it be something like "/dev/ttyS0" with respect to serial port ?
 
Old 12-02-2009, 07:44 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
/dev/ttyUSBx devices are valid and assigned to USB serial port adapters. You should use non-canonical input processing i.e newtio.c_lflag = 0 or asynchronous with a signal handler. Canonical input waits until a line terminating character is received.

http://tldp.org/HOWTO/Serial-Program...OWTO/x115.html
 
1 members found this post helpful.
Old 12-02-2009, 06:00 PM   #4
echo36
LQ Newbie
 
Registered: Nov 2009
Posts: 6

Rep: Reputation: 1
Code:
newtio.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
It looks like OP is using non-canonical processing.
 
1 members found this post helpful.
Old 12-02-2009, 06:18 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Your correct I misread that line.... I do see a mistake.
Quote:
read(fd, &response, 1)
No need for the &.
 
1 members found this post helpful.
Old 12-03-2009, 10:20 AM   #6
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
Quote:
Originally Posted by michaelk View Post
Your correct I misread that line.... I do see a mistake.


No need for the &.
That code looks fine to me. The response buffer is a char scalar, 1 byte deep. read() requires a pointer to it. Ordinarily, one would define the buffer as an array, but here, that is not the case.

With respect to the original problem, I think the cooked mode being used is the problem. Non-canonical processing is probably the solution.
--- rod.

Last edited by theNbomr; 12-03-2009 at 10:22 AM.
 
Old 12-03-2009, 10:26 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Quote:
Originally Posted by theNbomr View Post
That code looks fine to me. The response buffer is a char scalar, 1 byte deep. read() requires a pointer to it. Ordinarily, one would define the buffer as an array, but here, that is not the case.
Oops.. Your correct... I stand corrected again.... I shall clean my glasses and look again.
 
Old 12-03-2009, 06:44 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Not sure what the problem is anymore other then you might not be sending the correct characters and the getchar() requires the enter key to be pressed i.e if you are not actually using the enter key as your keypress.
 
Old 12-04-2009, 09:30 AM   #9
rkjnsn
LQ Newbie
 
Registered: Jun 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by michaelk View Post
Not sure what the problem is anymore other then you might not be sending the correct characters and the getchar() requires the enter key to be pressed i.e if you are not actually using the enter key as your keypress.
Well, I am indeed using the enter key as my keypress, and I have verified that it is indeed hanging on the read.

Thank you everyone for all of the replies so far.
 
Old 12-04-2009, 09:48 AM   #10
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
How do you know that the response is not received? Your code does nothing with any received characters.
Sorry for having misread the code with respect to the use of non-canonical processing.
--- rod.

Last edited by theNbomr; 12-04-2009 at 10:55 AM.
 
Old 12-04-2009, 09:53 AM   #11
rkjnsn
LQ Newbie
 
Registered: Jun 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by theNbomr View Post
How do you know that the response is not received? Your code does nothing with any received characters.
Sorry for having misread to code with respect to the use of non-canonical processing.
--- rod.
Well, the main purpose is to just wait until the byte is received, and I don't really care what it is (it's actually always 'R'). But I have indeed tried putting a printf after the read statement, and it never got executed. The key issue is that my program hangs on the read command forever, even though I know that a byte has been sent back.
 
Old 12-04-2009, 10:58 AM   #12
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
Have you tested the return codes from open(), tcsetattr(), write(), and read()? Permissions on the device? If you set VTIME to a non-zero value, does the read() call get any data?
--- rod.

Last edited by theNbomr; 12-04-2009 at 11:00 AM.
 
Old 12-04-2009, 11:11 AM   #13
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
I've tested your code using two laptops with the device being simulated via hyperterminal. Obviously the laptop does not care what characters are being sent to it and in fact I changed them to regular ASCII text. I also added some additional debug print statements but basically the program works for me.
 
Old 12-04-2009, 11:57 PM   #14
rkjnsn
LQ Newbie
 
Registered: Jun 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Okay, so some more weirdness. Just for grins I tried rewriting the program in python, and it works. I am at a loss as to why it doesn't work in C (since it does work for you).

Code:
import sys
import serial

ser = serial.Serial('/dev/ttyUSB0', 9600)

while 1:
	char = sys.stdin.read(1)
	if char == 'q':
		break;
	ser.write("\xA5\x5A\x0A\xF5")
	ser.read(1)

ser.close()
 
  


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 port reading jayadhanesh Linux - Newbie 3 05-19-2009 06:54 PM
Problem reading serial port linux_chris Linux - Software 2 02-04-2009 07:55 PM
Reading and Writing to a Serial Port without SU Steef223 Programming 2 06-16-2008 09:16 AM
reading from serial port IC009562 Linux - Software 2 11-08-2007 11:25 PM
Having trouble reading from serial port glo Linux - Newbie 0 02-09-2007 10:42 AM

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

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