linux 下串口的配置需要注意
9.1設(shè)置規(guī)范模式
規(guī)范模式是面向行的輸入方式,輸入字符被放入用于和用戶交互可以編輯的緩沖區(qū)內(nèi),直接到讀入回車或者換行符號時才結(jié)束。
可以通過如下方式來設(shè)置
option.c_lflag |= (ICANON | ECHO | ECHOE);
9.2設(shè)置原始輸入模式
原始輸入模式是沒有處理過的,當(dāng)接收數(shù)據(jù)時,輸入的字符在它們被接收后立即被傳送,使用原始輸入模式時候,一般可以選擇取消ICANON,ECHO,ECHOE和ISIG選項。
例如:
option.c_lflag &= ~(ICANON | ECHO | ECHOE);
=========================================================================
總結(jié):上面的問題應(yīng)該比較清楚了,就是在輸入模式的設(shè)置上出了問題,默認(rèn)的可能是行緩沖的,導(dǎo)致在接收串口的時候,串口會自行把數(shù)據(jù)存到緩沖里面,只有接收到回車、換行等結(jié)束標(biāo)志后,才把數(shù)據(jù)一起送到內(nèi)存,這樣串口的read函數(shù)才能讀到數(shù)據(jù),也就會觸發(fā)相應(yīng)的標(biāo)志(SIGIO)。所以,需要在對串口設(shè)置的時候,要設(shè)置為原始輸入模式。 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
#include<stdio.h> ?
#include<stdlib.h> ?
#include<unistd.h> ?
#include<sys/types.h> ?
#include<sys/stat.h> ?
#include<fcntl.h> ?
#include<termios.h> ?
#include<errno.h> ?
#include<string.h> ?
#define FALSE -1 ?
#define TRUE 0 ?
??
int bit_recv=0;
int uart_fd;
int speed_arr[] = { B57600, B38400, B19200, B9600, B4800, B2400, B1200, B300,B38400, B19200, B9600, B4800, B2400, B1200, B300, }; ?
int name_arr[] = {57600,38400, ?19200, ?9600, ?4800, ?2400, ?1200, ?300, 38400, 19200, ?9600, 4800, 2400, 1200, ?300, }; ?
void set_speed(int fd, int speed){ ?
? int ? i; ??
? int ? status; ??
? struct termios ? Opt; ?
? tcgetattr(fd, &Opt); ??
? for ( i= 0; ?i < sizeof(speed_arr) / sizeof(int); ?i++) { ??
? ? if ?(speed == name_arr[i]) { ? ? ??
? ? ? tcflush(fd, TCIOFLUSH); ? ? ??
? ? ? cfsetispeed(&Opt, speed_arr[i]); ? ?
? ? ? cfsetospeed(&Opt, speed_arr[i]); ? ??
? ? ? status = tcsetattr(fd, TCSANOW, &Opt); ? ?
? ? ? if ?(status != 0) { ? ? ? ? ?
? ? ? ? perror("tcsetattr fd1"); ? ?
? ? ? ? return; ? ? ??
? ? ? } ? ? ?
? ? ? tcflush(fd,TCIOFLUSH); ? ??
? ? } ? ?
? } ?
} ?
??
int set_Parity(int fd,int databits,int stopbits,int parity) ?
{ ??
? ? struct termios options; ??
? ? if ?( tcgetattr( fd,&options) ?!= ?0) { ??
? ? ? ? perror("SetupSerial 1"); ? ? ??
? ? ? ? return(FALSE); ? ?
? ? } ?
? ? options.c_cflag &= ~CSIZE; ??
? ? switch (databits) ??
? ? { ? ??
? ? case 7: ? ? ??
? ? ? ? options.c_cflag |= CS7; ??
? ? ? ? break; ?
? ? case 8: ? ? ??
? ? ? ? options.c_cflag |= CS8; ?
? ? ? ? break; ? ??
? ? default: ? ? ?
? ? ? ? fprintf(stderr,"Unsupported data size\n"); return (FALSE); ? ?
? ? } ?
? ? switch (parity) ??
? ? { ? ??
? ? ? ? case 'n': ?
? ? ? ? case 'N': ? ? ?
// ? ? ? ? ? ?options.c_cflag &= ~PARENB; ? /* Clear parity enable */ ?
?// ? ? ? ? ? options.c_iflag &= ~INPCK; ? ? /* Enable parity checking */ ??
? ? ? ? ? ? options.c_lflag &= ~(ICANON|ECHO|ISIG); ? /* Clear parity enable */ ?
? ? ? ? ? ? options.c_oflag &= ~OPOST; ? ? /* Enable parity checking */ ??
? ? ? ? ? ? break; ? ?
? ? ? ? case 'o': ? ??
? ? ? ? case 'O': ? ? ??
? ? ? ? ? ? options.c_cflag |= (PARODD | PARENB); ??
? ? ? ? ? ? options.c_iflag |= INPCK; ? ? ? ? ? ? /* Disnable parity checking */ ??
? ? ? ? ? ? break; ? ?
? ? ? ? case 'e': ? ?
? ? ? ? case 'E': ? ??
? ? ? ? ? ? options.c_cflag |= PARENB; ? ? /* Enable parity */ ? ? ?
? ? ? ? ? ? options.c_cflag &= ~PARODD; ? ? ?
? ? ? ? ? ? options.c_iflag |= INPCK; ? ? ? /* Disnable parity checking */ ?
? ? ? ? ? ? break; ?
? ? ? ? case 'S': ??
? ? ? ? case 's': ?/*as no parity*/ ? ??
? ? ? ? ? ? options.c_cflag &= ~PARENB; ?
? ? ? ? ? ? options.c_cflag &= ~CSTOPB;break; ? ?
? ? ? ? default: ? ??
? ? ? ? ? ? fprintf(stderr,"Unsupported parity\n"); ? ? ?
? ? ? ? ? ? return (FALSE); ? ?
? ? ? ? } ? ?
? ? ??
? ? switch (stopbits) ?
? ? { ? ??
? ? ? ? case 1: ? ? ?
? ? ? ? ? ? options.c_cflag &= ~CSTOPB; ? ?
? ? ? ? ? ? break; ? ?
? ? ? ? case 2: ? ? ?
? ? ? ? ? ? options.c_cflag |= CSTOPB; ? ?
? ? ? ? ? ?break; ?
? ? ? ? default: ? ? ?
? ? ? ? ? ? ?fprintf(stderr,"Unsupported stop bits\n"); ? ?
? ? ? ? ? ? ?return (FALSE); ??
? ? } ??
? ? /* Set input parity option */ ??
? ? if (parity != 'n') ? ??
? ? ? ? options.c_iflag |= INPCK; ??
? ? tcflush(fd,TCIFLUSH); ?
// ? ?options.c_cc[VTIME] = 150; ??
?// ? options.c_cc[VMIN] = 0; /* Update the options and do it NOW */ ?
? ?
?if (tcsetattr(fd,TCSANOW,&options) != 0) ? ??
? ? { ??
? ? ? ? perror("SetupSerial 3"); ? ??
? ? ? ? return (FALSE); ? ?
? ? } ??
? ? return (TRUE); ? ?
} ?
int main(int argc, char *argv[])
{
int ret;
unsigned char Flag,ReadBuf[15];
/*鎵撳紑騫跺垵濮嬪寲涓插彛*/
uart_fd = open("/dev/ttyUSB4",O_RDWR);?
if(uart_fd<0)
{
printf("error\n");
}
set_speed(uart_fd,57600); ?
? ?if (set_Parity(uart_fd,8,1,'N') == FALSE) ?{ ?
? ? ? ?printf("Set Parity Error\n"); ?
? ? ? ?exit (0); ?
? ?} ?
? ??
? ??
tcflush(uart_fd, TCIFLUSH) ;
SendDspCmd(Get_Image,uart_fd);
while(1)
總結(jié)
以上是生活随笔為你收集整理的linux 下串口的配置需要注意的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: su sudo
- 下一篇: POSIX标准总体分析