WCHSoftGroup / ch343ser_linux

USB driver for USB to serial chip ch342, ch343, ch344, ch9101, ch9102, ch9103, etc
125 stars 46 forks source link

CH343:In Linux system, continuous data transmission, it will loss data #43

Open loverking-007 opened 2 months ago

loverking-007 commented 2 months ago

source code: static int set_uart_baudrate(int fd, int baudrate) { struct termios2 tio;

if (ioctl(fd, TCGETS2, &tio)) {
    printf("%s,%d: Error,TCGETS2.\n", __FUNCTION__, __LINE__);
    return -1;
}

tio.c_cflag &= ~CBAUD;
tio.c_cflag |= BOTHER;
tio.c_ispeed = baudrate;
tio.c_ospeed = baudrate;

if (ioctl(fd, TCSETS2, &tio)) {
    printf("%s,%d: Error,TCSETS2.\n", __FUNCTION__, __LINE__);
    return -1;
}

if (ioctl(fd, TCGETS2, &tio)) {
    printf("%s,%d: Error,TCGETS2.\n", __FUNCTION__, __LINE__);
    return -1;
}

return 0;

}

int set_uart_attributes(int fd, int baudrate) { struct termios tty;

// Clear serial port non-blocking mode
//fcntl(fd, F_SETFL, 0);

// Get the current serial port properties
if (tcgetattr(fd, &tty) != 0) {
    perror("tcgetattr");
    close(fd);
    return -1;
}

// Set input and output baud rate
//cfsetospeed(&tty, baudrate);
//cfsetispeed(&tty, baudrate);

// Control mode flag (CLOCAL, CREAD)
tty.c_cflag |= (CLOCAL | CREAD);

// Set the data bits to 8 bits
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;

// Disable Parity
tty.c_cflag &= ~PARENB;
tty.c_iflag &= ~INPCK;

// Set a stop bit
tty.c_cflag &= ~CSTOPB;

#if 1
// Disable hardware flow control
tty.c_cflag &= ~CRTSCTS;
#else
// Enable hardware flow control
tty.c_cflag |= CRTSCTS;
#endif

// Set to raw mode
//tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

// Disable software flow control
//tty.c_iflag &= ~(IXON | IXOFF | IXANY);

// Set the raw output mode
// tty.c_oflag &= ~OPOST;

// Setting the read timeout
tty.c_cc[VMIN] = 64;
tty.c_cc[VTIME] = 100;

// Setting new properties takes effect immediately
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
    perror("tcsetattr");
    close(fd);
    return -1;
}

set_uart_baudrate(fd, baudrate);

return 0;

}

void test_uart_send_speed(char *pDev,unsigned int baud) { int sendCnt = 0; char Buffer[65] = {0}; char tmpstr[16] = {0}; char c = '0'; int fdser = open(pDev, O_RDWR | O_NOCTTY); int fdusb = open("/media/sda1/usb2uarttest.log", DBG_FILE_MODE); system("echo -n "" > /media/sda1/usb2uarttest.log"); if((fdser!=-1)&&(fdusb!=-1)) { printf("%s,%d:Start Test %s!baud=%d\n", FUNCTION, LINE, pDev,baud); set_uart_attributes(fdser, baud); while(sendCnt<20000) { sendCnt++; memset(Buffer, c++, sizeof(Buffer)); if(c > 'z'){ c = '0'; } Buffer[sizeof(Buffer)-2] = '\n'; Buffer[sizeof(Buffer)-1] = 0; memset(tmpstr, 0, sizeof(tmpstr)); snprintf(tmpstr,sizeof(tmpstr),"<%06d> %d ",sendCnt,strlen(Buffer)); memcpy(Buffer, tmpstr, strlen(tmpstr)); write(fdser, Buffer, strlen(Buffer)); write(fdusb, Buffer, strlen(Buffer)); } close(fdser);
close(fdusb);
}

printf("%s,%d:Test Finished!\n", __FUNCTION__, __LINE__);
while(1)
{
    usleep(5000000);
}

}

char Dev343[] = "/dev/ttyCH343USB0"; char pDevice = Dev343; int main(int argc, charargv[]) { test_uart_send_speed(pDevice, 4000000); }

test result: The data was transmitted with a length of 64 bytes 20,000 times, and data loss occurred 5 times, one of which is shown in the attached picture. loss data as follows:

<009883> 64 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii <009884> 64 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj <009885> 64 kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk <009886> 64 lllllllllllllllllllllllllllllllllllllllllllllllllll zzzzzzzzyyy <009900> 64 zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz <009888> 64 nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn <009889> 64 ooooooooooooooooooooooooooooooooooooooooooooooooooo <009890> 64 ppppppppppppppppppppppppppppppppppppppppppppppppppp <009891> 64 qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzzzzzzzzzzzz <009901> 64 000000000000000000000000000000000000000000000000000 <009902> 64 111111111111111111111111111111111111111111111111111 <009903> 64 222222222222222222222222222222222222222222222222222 <009904> 64 333333333333333333333333333333333333333333333333333
WCHSoftGroup commented 1 week ago

please check your hardware if the uart uses TTL level or other level? the baudrate and data amount is not large for ch343, by the way, you can get serial state to check if the chip occurs hardflow when received large data.