appbox之: linux串口測試程式

來源:互聯網
上載者:User

標籤:linux   串口   

平台:從網上買的X86平台(baytrail -D (cerelon J1900))

Baytrail平台內建兩個串口,基本上就是低端台式機的配置

串口也是同台式機一樣


問題:這裡將這個X86平台(baytrail cerelon J1900)當成從機裝置,

將其接到PC的串口上,一直無輸出


解決:串口線兩端都是母頭,也就是說是直連的,那麼這樣,baytrail的tx直接與PC的tx相接

所以調了很久都沒有任何輸出,問題就在於BYT的TX與PC的TX相接,導致PC上看不到BYT裝置的輸出

所以,調串口裝置,一定要注意需要使用交叉線還是直連線,或者說要注意收和發是否對應


另外,上串口測試代碼,這也是appbox的一個常式:

#include <stdio.h>#include <ctype.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <fcntl.h>#include <time.h>#include <dirent.h>#include <assert.h>#include <linux/unistd.h>#include <pthread.h>#include <semaphore.h>#include <signal.h>#include <termios.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/ioctl.h>#include <linux/ioctl.h>#include <sys/times.h>#include <sys/time.h>#include <sys/sysinfo.h>#include <net/if.h>#include <net/if_arp.h>#include <net/route.h>#include <linux/if_ether.h>#include <arpa/inet.h>#include <linux/sockios.h>#include <sys/mman.h>/*平台:從網上買的X86平台(baytrail -D (cerelon J1900))Baytrail平台內建兩個串口,基本上就是低端台式機的配置串口也是同台式機一樣問題:這裡將這個X86平台(baytrail cerelon J1900)當成從機裝置,將其接到PC的串口上,一直無輸出解決:串口線兩端都是母頭,也就是說是直連的,那麼這樣,baytrail的tx直接與PC的tx相接所以調了很久都沒有任何輸出,問題就在於BYT的TX與PC的TX相接,導致PC上看不到BYT裝置的輸出另外,上串口測試代碼,這也是appbox的一個常式:*/typedef unsigned int            DWORD;typedef unsigned char           BYTE;#define LINE_MAX_LENGTH128#define NAME_MAX_LENGTH 32#define MAX_PATH_LENGTH 128/// 串口屬性結構typedef struct COMM_ATTR {DWORDbaudrate;///< 實際的傳輸速率值。BYTEdatabits;///< 實際的資料位元數。BYTEparity;///< 同位選項,取comm_parity_t類型的枚舉值。BYTEstopbits;///< 停止位元,取comm_stopbits_t類型的枚舉值。BYTEreserved;///< 保留} COMM_ATTR;/// 串口停止位類型enum comm_stopbits_t {COMM_ONESTOPBIT,///< 1 stop bitCOMM_ONE5STOPBITS,///< 1.5 stop bitCOMM_TWOSTOPBITS///< 2 stop bit};/// 串口校正位類型enum comm_parity_t {COMM_NOPARITY,///< No parityCOMM_ODDPARITY,///< Odd parityCOMM_EVENPARITY,///< Even parityCOMM_MARK,///< COMM_SPACE///< };typedef enum CommType {commBus_485 = 0x0001,commBus_422 = 0x0002, commBus_232 = 0x0004, } CommType;/// 串口操作中斷類型typedef enum CommPurgeFlags{commPurgeTxAbort = 0x0001,///< 中止寫操作commPurgeRxAbort = 0x0002,///< 中止讀操作commPurgeTxClear = 0x0004,///< 清空輸出緩衝commPurgeRxClear = 0x0008 ///< 清空輸入緩衝} CommPurgeFlags;/// 串口停止位類型typedef enum CommStopBit{commOneStopBit = 0,///< 1 stop bitcommOne5StopBits,///< 1.5 stop bitcommTwoStopBits//< 2 stop bit} CommStopBit;/// 串口校正位類型typedef enum CommParityType {commNoParity = 0,///< No paritycommOddParity,///< Odd paritycommEvenParity,///< Even paritycommMarkParity,///< Mark paritycommSpaceParity///< Space parity} CommParityType;/// 特殊串口標誌typedef enum CommSpecialFlag{commNormal = 0,commRedApple} CommSpecialFlag;/// 串口模式typedef enum CommMode{commFullDuplex = 0,///< 全雙工系統commSemiDuplex,///< 半雙工} CommMode;int uartfd = -1;int FrontboardSetAttr(COMM_ATTR *ParmaAttribute_p){struct termios Option;COMM_ATTR *Attribute_p = ParmaAttribute_p;memset(&Option, 0, sizeof(struct termios));tcgetattr(uartfd, &Option);if(uartfd < 0){printf("Index < 0 || UartSetAttributeEx.\n");return -1;}cfmakeraw(&Option);switch(Attribute_p->baudrate){case 50:cfsetispeed(&Option, B50);cfsetospeed(&Option, B50);break;case 75:cfsetispeed(&Option, B75);cfsetospeed(&Option, B75);break;case 110:cfsetispeed(&Option, B110);cfsetospeed(&Option, B110);break;case 134:cfsetispeed(&Option, B134);cfsetospeed(&Option, B134);break;case 150:cfsetispeed(&Option, B150);cfsetospeed(&Option, B150);break;case 200:cfsetispeed(&Option, B200);cfsetospeed(&Option, B200);break;case 300:cfsetispeed(&Option, B300);cfsetospeed(&Option, B300);break;case 600:cfsetispeed(&Option, B600);cfsetospeed(&Option, B600);break;case 1200:cfsetispeed(&Option, B1200);cfsetospeed(&Option, B1200);break;case 1800:cfsetispeed(&Option, B1800);cfsetospeed(&Option, B1800);break;case 2400:cfsetispeed(&Option, B2400);cfsetospeed(&Option, B2400);break;case 4800:cfsetispeed(&Option, B4800);cfsetospeed(&Option, B4800);break;case 9600:cfsetispeed(&Option, B9600);cfsetospeed(&Option, B9600);break;case 19200:cfsetispeed(&Option, B19200);cfsetospeed(&Option, B19200);break;case 38400:cfsetispeed(&Option, B38400);cfsetospeed(&Option, B38400);break;case 57600:cfsetispeed(&Option, B57600);cfsetospeed(&Option, B57600);break;case 115200:cfsetispeed(&Option, B115200);cfsetospeed(&Option, B115200);break;default:printf("Unsupported baudrate %d\n", Attribute_p->baudrate);return -1;}switch(Attribute_p->parity){case commNoParity:// noneOption.c_cflag &= ~PARENB;// disable parityOption.c_iflag &= ~INPCK;// disable parity checkbreak;case commOddParity:// oddOption.c_cflag |= PARENB;// enable parityOption.c_cflag |= PARODD;// oddOption.c_iflag |= INPCK;// enable parity checkbreak;case commEvenParity:// evenOption.c_cflag |= PARENB;// enable parityOption.c_cflag &= ~PARODD;// evenOption.c_iflag |= INPCK;// enable parity checkbreak;case commMarkParity:Option.c_cflag |= PARENB;/* enable parity*/Option.c_cflag |= PARODD;/* parity bit is always 1*/break;case commSpaceParity:Option.c_cflag |= PARENB;/* enable parity*/break;default:printf("Unsupported parity %d\n", Attribute_p->parity);return -1;}Option.c_cflag &= ~CSIZE;switch (Attribute_p->databits){case 5:Option.c_cflag |= CS5;break;case 6:Option.c_cflag |= CS6;break;case 7:Option.c_cflag |= CS7;break;case 8:Option.c_cflag |= CS8;break;default:printf("Unsupported data bits %d\n", Attribute_p->databits);return -1;}Option.c_cflag &= ~CSTOPB;switch (Attribute_p->stopbits){case commOneStopBit:Option.c_cflag &= ~CSTOPB;break;case commOne5StopBits:break;case commTwoStopBits:Option.c_cflag |= CSTOPB;break;default:printf("Unsupported stop bits %d\n", Attribute_p->stopbits);return -1;}Option.c_cc[VTIME] = 0;Option.c_cc[VMIN]  = 1;tcflush(uartfd, TCIOFLUSH);if(tcsetattr(uartfd, TCSANOW, &Option) < 0){printf("tcsetattr Failed.\n");return -1;}return 0;}int FrontboardCreate(void){char DeviceName[NAME_MAX_LENGTH] = {0};int iFbUartIndex;COMM_ATTR UartAttribute;memset(&UartAttribute, 0, sizeof(UartAttribute));if(uartfd > 0){return 0;}iFbUartIndex = 0;snprintf(DeviceName, sizeof(DeviceName),"%s%d", "/dev/ttyS", iFbUartIndex);printf("DeviceName:%s\n", DeviceName);uartfd = open(DeviceName, O_RDWR);if(uartfd < 0){printf("Open %s failed.\n", DeviceName);return -1;}UartAttribute.baudrate = 9600;UartAttribute.databits = 8;UartAttribute.parity   = commNoParity;UartAttribute.stopbits = commOneStopBit;if(FrontboardSetAttr(&UartAttribute) < 0){printf("UartSetAttributeEx Failed. \n");return -1;}return 0;}int FrontboardDestory(void){if(uartfd > 0){close(uartfd);uartfd = -1;}return 0;}int FrontboardRead(void *pData, DWORD nBytes){int ReadLength = 0;int ReadPosition = 0;if(NULL == pData || nBytes < 0){printf("Invalid Params, NULL == pData or nBytes < 0.\n");return -1;}memset(pData, 0, nBytes);while(0 != nBytes){ReadLength = read(uartfd, pData + ReadPosition, nBytes);if(ReadLength < 0){printf("read Failed, ReadLength < 0.\n");return -1;}ReadPosition += ReadLength;nBytes -= ReadLength;}return ReadPosition;}int FrontboardWrite(void *pData, DWORD nBytes){int WriteLength = 0;int WritePosition = 0;if(NULL == pData || nBytes < 0){printf("Invalid Params, NULL == pData || nBytes < 0.\n");return -1;}while(0 != nBytes){WriteLength = write(uartfd, pData + WritePosition, nBytes);if(WriteLength < 0){printf("write failed, nBytes:%d, WriteLength:%d\n", nBytes, WriteLength);return -1;}WritePosition += WriteLength;nBytes -= WriteLength;}return WritePosition;}int main(int argc, char *argv[]){unsigned char pData[8] = {0};pData[0] = 'a';pData[1] = 'b';pData[2] = 'c';pData[3] = 'd';pData[4] = '\n';FrontboardCreate();while(1){printf("frontboardwrite:\n");FrontboardWrite((void*)pData, 8);sleep(1);}return 0;}


appbox之: linux串口測試程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.