LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   serial port programming (https://www.linuxquestions.org/questions/programming-9/serial-port-programming-570586/)

sundaresh 07-19-2007 12:57 AM

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.

Wim Sturkenboom 07-19-2007 02:38 AM

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).

sundaresh 07-19-2007 04:10 AM

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.

sundaresh 07-19-2007 04:22 AM

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;
}

sundaresh 07-19-2007 05:00 AM

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.

Wim Sturkenboom 07-19-2007 08:26 AM

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.

XavierP 07-19-2007 08:32 AM

As requested, moved to Programming


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