USB - Generic Serial device : Unable to read more than 4095 bytes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi ,

I have a trouble with using usb serial generic device.
 I am using USB - Generic Serial driver for communicating with my usb
device and my embedded device.
My usb device sending data at a rate of 409600 bytes/sec, and in host
side application I tried to read 16384 bytes in one read. But the read
size returning is always  4095
I don't know whether I  missed any settings or this is limited in driver.
Could you please tell me is it possible to read large size(16384
bytes) using usb serial generic driver.

I pasted my sample code for reading from usb device

#######################################################

/*
 * usb_logger_test.c
 *
 *  Created on: Aug 3, 2015
 *      Author:
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>
#include <poll.h>

#define USB_DEVICE "/dev/ttyUSB0"
#define DATA_SIZE (16384)
//#define DATA_SIZE (1000)

struct pollfd  fds[1];
nfds_t  nfds = 1;
int dev_fd;

void set_blocking (int should_block)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (dev_fd, &tty) != 0) {
printf("error %d from tggetattr", errno);
return;
}
tty.c_cc[VMIN]  = should_block ? 1 : 0;
//tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

if (tcsetattr (dev_fd, TCSANOW, &tty) != 0) {
printf("error %d setting term attributes", errno);
}
}

int hal_uart_init()
{

int parity =0;
int speed = B115200;
struct termios tty;

memset ((void *)&tty, 0, sizeof tty);
if (tcgetattr (dev_fd, &tty) != 0) {
printf("error %d from tcgetattr", errno);
return -1;
}
cfsetospeed (&tty, speed);
cfsetispeed (&tty, speed);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
// disable IGNBRK for mismatched speed tests; otherwise receive break
// as \000 chars
tty.c_iflag &= ~IGNBRK;         // disable break processing
tty.c_lflag = 0;                // no signaling chars, no echo,
// no canonical processing
tty.c_oflag = 0;                // no remapping, no delays
tty.c_cc[VMIN]  = 0;            // read doesn't block
tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
tty.c_cflag |= parity;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
tty.c_iflag &= ~(ICRNL | IGNCR );
if (tcsetattr (dev_fd, TCSANOW, &tty) != 0) {
printf("error %d from tcsetattr", errno);
return -1;
}
set_blocking (1);

return 0;
}

int hal_uart_open()
{
dev_fd = open (USB_DEVICE,O_RDWR | O_NOCTTY | O_SYNC);
if (dev_fd < 0)  {
printf ("error %d opening %s: %s", errno, USB_DEVICE, strerror (errno));
return -1;
}
fds[0].fd = dev_fd; /* open fd for each filter,increment nfds */
fds[0].events = POLLIN;
fds[0].revents = 0;
nfds = 1;

return 0;
}

int hal_uart_read(unsigned char *buf, unsigned int size, unsigned int *rcv_len)
{
int ret;
unsigned int timeout = 1000;
int read_size;

ret = poll(&fds[0], nfds, timeout);
if(ret <= 0) {
if(ret < 0) {
printf( "poll err !!!   errno %d : %s !!! \n", errno, strerror(errno));
return -1;
}else {
printf( "poll timeout !!! ,  timeout %d \n",  timeout);
return -1;
}
}
if(fds[0].revents & POLLIN) {
read_size = read(fds[0].fd, buf, DATA_SIZE);
if(read_size <= 0) {
printf( "read err !!!   read_size %d fd %d   errno %d : %s !!! \n",
read_size, fds[0].fd, timeout, errno, strerror(errno));
return -1;
}
printf("read succc read_size %d \n", read_size);
{
FILE *fp = fopen("usb_dump", "ab" );
if(fp != NULL) {
fwrite(buf, 1, read_size, fp);
fflush(fp);
fclose(fp);
}
}
}else {
printf(  "No data read  \n");
}

return read_size;
}

void main()
{

unsigned char buf[DATA_SIZE];
unsigned int size;
unsigned int rcv_len;
int ret = 0;

ret = hal_uart_open();
if(ret < 0) {
printf("hal_uart_open fail  \n");
return;
}
ret = hal_uart_init();
if(ret < 0) {
printf("hal_uart_init fail  \n");
return;
}

while(1) {
hal_uart_read(&buf[0], size, &rcv_len);
//usleep(400000);
usleep(40000);
//sleep(10);
//sleep(11);
}
}

Regards,
Arun
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux