LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 07-19-2007, 12:57 AM   #1
sundaresh
LQ Newbie
 
Registered: Nov 2005
Distribution: red hat
Posts: 5

Rep: Reputation: 0
serial port programming


Hello,

I am using the termios library to communicate with TI6000/65000
RFID reader and associated computer. Whereas I have been able to write
to the device, I have not been able to read the response from the
device.Any suggestions/help will be welcome.I have tried asynchronous
read using signals as well as direct read with ample timeout. Both
these methods do not read a response.
 
Old 07-19-2007, 02:38 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Sounds like a programming question. If os

I suggest that you report your post (there is a button) 'report' so it can be moved to the programming forum.

Further it will be usefull to see relevant source code (for setup of serial port as well as the read functions that you've written).
 
Old 07-19-2007, 04:10 AM   #3
sundaresh
LQ Newbie
 
Registered: Nov 2005
Distribution: red hat
Posts: 5

Original Poster
Rep: Reputation: 0
moving the post

Sorry for posting in the wrong forum. I kindly request the
administrators to move my question to the relevant programming
forum, and inform me. I shall respond with some source code.
 
Old 07-19-2007, 04:22 AM   #4
sundaresh
LQ Newbie
 
Registered: Nov 2005
Distribution: red hat
Posts: 5

Original Poster
Rep: Reputation: 0
source code

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

#define DEVICE_NAME ("/dev/ttyS1")

#define BAUD_RATE (B9600)

#define TIMEOUT (10000)

#define MAX_LEN (256)

int fd;

int timeout, i, len;

unsigned char buffer[MAX_LEN];

int reset() {
const unsigned char reset_command[8] = { '\x96', '\xFF', '\xFF', '\xFF' , '\xFF' , '\xFF' , '\xCC' , '\x97' };
struct termios new_settings, old_settings;
int fail;

tcflush(fd, TCOFLUSH);
if(! (fail = tcgetattr(fd, &old_settings))) {
new_settings = old_settings;
if(new_settings.c_cflag & PARENB) {
new_settings.c_cflag ^= PARENB;
}
if(new_settings.c_lflag & ICANON) {
new_settings.c_lflag ^= ICANON;
}
new_settings.c_cflag &= ~CSIZE;
new_settings.c_cflag |= CS8;
new_settings.c_iflag |= (IXON | IXOFF);
new_settings.c_cc[VSTART] = '\x96';
new_settings.c_cc[VSTOP] = '\x97';
if(! (fail = cfsetispeed(&new_settings, BAUD_RATE)) && ! (fail = cfsetospeed(&new_settings, BAUD_RATE)))
if(! (fail = tcsetattr(fd,TCSANOW, &new_settings)))
if(write(fd,reset_command,8) == 8) fail = 0;
else fail = -1;
else {
fail = -2;
tcsetattr(fd, TCSANOW, &old_settings);
}
else fail = -3;
}
else fail = -4;
return fail;
}

int main() {
fd = open(DEVICE_NAME, O_WRONLY | O_NOCTTY | O_NONBLOCK);
if(fd == -1) {
perror("Unable to open device");
exit(1);
}
if(reset()) {
perror("Unable to reset");
exit(2);
}
timeout = TIMEOUT;
while(read(fd, buffer, 1) <= 0 && --timeout >= 0);
if(timeout >= 0) {
len = read(fd, buffer + 1, MAX_LEN);
len++;
for(i = 0; i < len;i++) {
printf("%x ", (int)buffer[i]);
}
}
close(fd);
return 0;
}

Last edited by sundaresh; 07-19-2007 at 04:25 AM.
 
Old 07-19-2007, 05:00 AM   #5
sundaresh
LQ Newbie
 
Registered: Nov 2005
Distribution: red hat
Posts: 5

Original Poster
Rep: Reputation: 0
works

The above program does indeed work and does produce a valid response.
Thanks. The problem was the reader works in non-canonical mode,
but the existing default setting was for canonical mode.
 
Old 07-19-2007, 08:26 AM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by sundaresh
Sorry for posting in the wrong forum. I kindly request the
administrators to move my question to the relevant programming
forum, and inform me. I shall respond with some source code.
You can click the report button in one of your posts to request the move.

Glad it works. One other comment: in future use code tags around code. It maintains indentation and makes it easier to read.
 
Old 07-19-2007, 08:32 AM   #7
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Lubuntu
Posts: 19,176
Blog Entries: 4

Rep: Reputation: 430Reputation: 430Reputation: 430Reputation: 430Reputation: 430
As requested, moved to Programming
 
  


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
serial port programming!!! novice_din Programming 4 02-03-2005 08:50 PM
Serial port communication programming SeanatIL Programming 3 07-17-2004 08:26 PM
Serial Port Programming Question Darktyco Programming 8 03-23-2004 05:13 PM
Programming the Serial Port karans Linux - Networking 2 11-03-2003 01:54 AM
Modem programming and serial port Drickx Linux - Networking 2 04-24-2002 09:24 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration