ARM-qt 開發,串列埠配置
阿新 • • 發佈:2018-12-07
在使用終端開發使用串列埠時,配置串列埠的方式尤為重要
1、要使用串列埠就先開啟串列埠
isatty,函式名。主要功能是檢查裝置型別 , 判斷檔案描述詞是否是為終端機。int OpenUartPort(const char *UartPort) { int fd; fd = open(UartPort,O_RDWR|O_NONBLOCK); if(fd<0) { perror("open serial port"); return -1; } if(isatty(fd) == 0) { perror("this is not a terminal device"); return -1; } return fd; }
2、使用串列埠就要配置串列埠
tcflush函式是 Unix終端I/O函式。作用:清空終端未完成的輸入/輸出請求及資料。 tcgetattr是一個函式,用來獲取終端引數,成功返回零;失敗返回非零,發生失敗介面將設定 errno錯誤標識。 相關函式:tcsetattr用來設定終端引數。int SetUartConfig(int fd, int Baud) { struct termios new_cfg,old_cfg; if(tcgetattr(fd,&old_cfg) !=0 ) { perror("tcgetattr"); return -1; } new_cfg = old_cfg; new_cfg.c_cc[VTIME] = 0; new_cfg.c_cc[VMIN] = 0;//控制字元 new_cfg.c_iflag = 0;//輸入模式標誌 new_cfg.c_oflag = 0;//輸出模式標誌 new_cfg.c_lflag = 0;//本地模式標誌 switch(Baud) { case 9600:new_cfg.c_cflag = B9600 | CS8 | CLOCAL | CREAD;break;//控制模式標誌 case 19200:new_cfg.c_cflag = B19200 | CS8 | CLOCAL | CREAD;break; case 115200:new_cfg.c_cflag = B115200 | CS8 | CLOCAL | CREAD;break; } tcflush(fd,TCIOFLUSH); if(tcsetattr(fd,TCSANOW,&new_cfg) !=0) { perror("tcsettattr"); return -1; } return 0; }
3、從串列埠中讀取資料
int ReadUartPort(int fd, int Num, u_int8_t *Buff) { int res = 0; memset(Buff,0,Num); res = read(fd,Buff,Num); return res; }