I am implementing a simple unix program that takes an RS232 input and saves it to a file.
I used these links:
http://en.wikibooks.org/wiki/Serial_Programming/Serial_Linux and
http://www.easysw.com/~mike/serial/serial.html
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
int main(int argc,char** argv)
{
struct termios tio;
struct termios stdio;
int tty_fd;
fd_set rdset;
FILE *file;
unsigned char c;
memset(&tio,0,sizeof(tio));
tio.c_iflag=0;
tio.c_oflag=0;
tio.c_cflag=CS8|CREAD|CLOCAL;
tio.c_lflag=0;
tio.c_cc[VMIN]=1;
tio.c_cc[VTIME]=5;
tty_fd=open("/dev/ttyS1", O_RDWR | O_NONBLOCK);
speed_t baudrate = 1843200;
cfsetospeed(&tio,baudrate);
cfsetispeed(&tio,baudrate);
tcsetattr(tty_fd,TCSANOW,&tio);
file = fopen("out.raw", "wb");
while (1)
{
if (read(tty_fd,&c,1)>0) {
fwrite(&c, 1, 1, file);
fflush(file);
}
}
}
I tried at 921'600 bps and at 1'843'200 bps and it works correctly. However, it does not work if I set a non-standard transmission rate, for example, 1'382'400 bit / s.
ie, this works:
cfsetospeed(&tio,1843200); cfsetispeed(&tio,1843200);
but this is not the case (it receives random data):
cfsetospeed(&tio,1382400); cfsetispeed(&tio,1382400);
What could be the problem?
I tried with WinXP (using the WIN32 CreateFile, SetCommState and ReadFile functions) and it works correctly (with 1'843'200 bps, as well as with non-standard 1'382'400 bps)
ps: , , - , .
,