Hi Alexey,
Thanks for the help, I like the code which you provided. But I am still
getting the same result. After more testing, and printing out the packet that
I received, I found out for a 520 bytes packet, I am only receiving 264 bytes
from it. I lost 256 bytes from the packet.
The lost bytes problem only occurs if the ttyUSB0 baudrate is higher than 38400
bps
for example:
9600baud, result: sent 520, successfully received 520
36400baud, result: sent 520, successfully received 520
57600baud, result: sent 520, but only received 256
115200baud, result sent 520, but only received 256
500000baud, result sent 520, but only received 256
I don't know why the problem is so consistent, has someone had experiences like
this before?
Here is my receiving packet code:
unsigned char receivePacket(int fd, unsigned char *buffer)
{
printf("Receive Packet\n");
unsigned int i=0;
unsigned int left_to_read=520;
while(left_to_read>0)
{
unsigned char temp = read(fd, buffer+i, left_to_read);
usleep(10);
i+=temp;
left_to_read-=temp;
}
for (i=0; i<520; i++)
{
printf("%c", buffer[i]);
}
return 0;
}
Here is how I open the port:
int openUSB()
{
//system("stty -F /dev/ttyUSB0 57600 cs8 -cstopb -parity -icanon min 1 time
int fd = open ("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY | O_FSYNC);
if (fd==-1)
{
fprintf(stderr, "open_port: Unable to open /dev/ttyUSB0 -%s \n",
strerror(errno));
}
struct termios options;
/*Configure port reading*/
fcntl (fd, F_SETFL, FNDELAY);
/*Get the current options for the port*/
tcgetattr(fd, &options);
/*Set baudrate to 500000Mb*/
cfsetispeed(&options, B57600);
cfsetospeed(&options, B57600);
/*Mark the character size to 8 bits, no parity*/
options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;
options.c_iflag &= ~(IXON | ICRNL);
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 0;
tcsetattr(fd, TCSANOW, &options);
return fd;
}
--- In Alexey Vdovin <> wrote:
>
> Tsao,
>
> In function receivePacket try read more then 1 byte at once:
>
> unsigned char temp = read(fd, buffer+i, left_to_read);
> i += temp;
> left_to_read -= temp;
>
> To avoid hangs add read timeout to this function.
> Keep in mind, some date bytes may be lost or corrupted during
> transmission, you need error checking mechanism.
>
> ---
> Best Regards
> Alexey Vdovin
>
> -----Original Message-----
> From: "tsao.terence" <>
> To:
> Date: Thu, 04 Mar 2010 08:44:03 -0000
> Subject: [ts-7000] TS7260-Reading from /dev/ttyUSB0
>
> > Dear Linux masters,
> >
> > I am communicating TS7260 with my sensor device using a USB to serial
> > FTDI converter. The sensor uses the uart interface, and the TS7260 uses
> > the USB interface. The sensor device will send TS7260 a packet of data
> > which contains 512 bytes, aka each packet is 512 bytes.
> >
> >
> > I had success reading from ttyUSB0 with 9600 and 38400 baudrate, but when I
> > jump to 57600 baud, the program just hangs. I think there might be
> > something wrong as I open the USB port, or my receive function has too much
> > software overhead. Anyone here had success reading from the ttyUSB0 port?
> >
> > Here is my open USB function:
> > int openUSB()
> > {
> > int fd = open ("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY | O_FSYNC);
> > if (fd==-1)
> > {
> > fprintf(stderr, "open_port: Unable to open /dev/ttyUSB0 -%s \n",
> > strerror(errno));
> > }
> >
> > struct termios options;
> >
> > /*Configure port reading*/
> > fcntl (fd, F_SETFL, FNDELAY);
> > /*Get the current options for the port*/
> > tcgetattr(fd, &options);
> >
> > /*Set baudrate to 500000Mb*/
> > cfsetispeed(&options, B57600);
> > cfsetospeed(&options, B57600);
> >
> > /*Mark the character size to 8 bits, no parity*/
> > options.c_cflag |= (CLOCAL | CREAD);
> > options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
> > options.c_oflag &= ~OPOST;
> > options.c_iflag &= ~(IXON | ICRNL);
> > options.c_cc[VMIN] = 0;
> > options.c_cc[VTIME] = 0;
> > tcsetattr(fd, TCSANOW, &options);
> > return fd;
> > }
> >
> > Here is my read function:.
> > unsigned char receivePacket(int fd, unsigned char *buffer)
> > {
> >
> > unsigned int i=0;
> > unsigned int left_to_read=520;
> > unsigned char byte;
> > while(left_to_read>0)
> > {
> > nsigned char temp = read(fd, &byte, 1);
> > if(temp>0)
> > {
> > left_to_read--;
> > buffer[i]=byte;
> > printf("%.2x",byte);
> > i++;
> > }
> > }
> > return 0;
> > }
> >
>
------------------------------------
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/ts-7000/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/ts-7000/join
(Yahoo! ID required)
<*> To change settings via email:
<*> To unsubscribe from this group, send an email to:
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|