On Fri, 5 Mar 2010, tsao.terence wrote:
> 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);
^^^^^^^^^^^^^^^^^^
read returns a ssize_t type. unsigned char allows a value of from 0 to
511 - so there is the possibility of truncation is read read a large number
of characters. Try correcting this and see what happens.
> 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/
|