旭日X3派開(kāi)發(fā)板提供了40PIN標(biāo)準(zhǔn)接口,方便用戶擴(kuò)展外圍設(shè)備。其中物理引腳編號(hào)8和10為串口通信發(fā)送和接收使用(8-TXD,10-RXD)。
40PIN引腳功能和位置編號(hào)如下:
串口轉(zhuǎn)USB設(shè)備引腳和功能描述如下:
我們可以使用3根杜邦線 + 一個(gè)串口轉(zhuǎn)USB設(shè)備(旭日X3派開(kāi)發(fā)包附贈(zèng)) + 一臺(tái)筆記本電腦 + putty(串口工具)搭建串口通信開(kāi)發(fā)環(huán)境。
如下圖示:
1)6號(hào)管腳和串口轉(zhuǎn)USB GND連接
2)8號(hào)管腳和串口轉(zhuǎn)USB RXD連接
3)10號(hào)管腳和串口轉(zhuǎn)USB TXD連接
從40PIN管腳功能圖我們知道,8號(hào)和10號(hào)管腳為UART3,對(duì)應(yīng)的設(shè)備文件為 /dev/ttyS3。
環(huán)境搭建完成后,我們來(lái)編寫一個(gè)簡(jiǎn)單的程序庫(kù)操作串口。
//頭文件
#ifndef SERIAL_H#define SERIAL_Htypedef struct Serial Serial;struct Serial{ int (*Open)(Serial *pDevice, const char *sDevice, int baudrate); void (*Close)(Serial *pDevice); int (*Write)(Serial *pDevice, const char *buf, int len); int (*Read)(Serial *pDevice, char *buf, int len);};Serial* CreateSerialDevice();void DestroySerialDevice(Serial *pDevice);#endif //SERIAL
//實(shí)現(xiàn)文件
#include #include #include #include #include #include #include #include "serial.h"typedef struct SerialImpl{ Serial base; int handle; int baudrate; char sDevice[64];}SerialImpl;int speed_arr[] = {B1500000, B1152000, B1000000, B921600, B576000, B500000, B460800, B230400, B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1800, B1200, B600, B300, B200, B150, B134, B110, B75, B50, B0};int baudrate_arr[] = {1500000, 1152000, 1000000, 921600, 576000, 500000, 460800, 230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1800, 1200, 600, 300, 200, 150, 134, 110, 75, 50, 0};int serial_setting(Serial *pDevice, int baudrate);void serial_close(Serial *pDevice);int serial_comspeed_get(int baudrate){ for (int i = 0; i < sizeof(baudrate_arr)/ sizeof(baudrate_arr[0]); ++i) { if (baudrate_arr[i] == baudrate) { return speed_arr[i]; } } return -1;}int serial_open(Serial *pDevice, const char *sDevice, int baudrate){ int handle = -1; SerialImpl *impl = (SerialImpl*)pDevice; if (!impl) { return -1; } if (!sDevice || sDevice[0] == '\0') { return -2; } handle = open(sDevice, O_RDWR, 0); if (-1 == handle) { perror("open"); return -3; } impl->handle = handle; snprintf(impl->sDevice, sizeof(impl->sDevice), "%s", sDevice); if (0 != serial_setting(pDevice, baudrate)) { serial_close(pDevice); return -4; } return 0;}int serial_setting(Serial *pDevice, int baudrate){ int handle = -1; int speed = 0; struct termios options; SerialImpl *impl = (SerialImpl*)pDevice; if (!impl) { return -1; } handle = impl->handle; if (handle <= 0) { return -2; } speed = serial_comspeed_get(baudrate); if (speed < 0) { return -3; } impl->baudrate = baudrate; if (tcgetattr(handle, &options) != 0) { perror("tcgetattr"); return -4; } if (cfsetispeed(&options, speed) != 0 || cfsetispeed(&options, speed) != 0) { perror("cfsetispeed"); return -5; } //no partity options.c_cflag &= ~PARENB; options.c_iflag &= ~INPCK; //8bits options.c_cflag |= CS8; //stop 1bits options.c_cflag &= ~CSTOPB; //no flow control options.c_cflag &= ~CRTSCTS; options.c_cflag &= ~CSIZE; if (tcsetattr(handle, TCSANOW, &options) != 0) { perror("tcsetattr"); return -6; } return 0;}int serial_read(Serial *pDevice, char *buf, int len){ int handle = -1; int size = 0; SerialImpl *impl = (SerialImpl*)pDevice; if (!impl) { return -1; } handle = impl->handle; if (handle <= 0) { return -2; } if (!buf || len <= 0) { return -3; } size = read(handle, buf, len); if (-1 == size) { perror("read"); return -4; } return size;}int serial_write(Serial *pDevice, const char *buf, int len){ int handle = -1; int size = 0; SerialImpl *impl = (SerialImpl*)pDevice; if (!impl) { return -1; } handle = impl->handle; if (handle <= 0) { return -2; } if (!buf || len <= 0) { return -3; } size = write(handle, buf, len); if (-1 == size) { perror("write"); return -4; } return size;}void serial_close(Serial *pDevice){ int handle = -1; SerialImpl *impl = (SerialImpl*)pDevice; if (!impl) { return; } handle = impl->handle; if (handle != -1) { close(handle); }}Serial* CreateSerialDevice(){ SerialImpl *pDevice = (SerialImpl*)malloc(sizeof(SerialImpl)); if (pDevice) { pDevice->base.Open = serial_open; pDevice->base.Close = serial_close; pDevice->base.Write = serial_write; pDevice->base.Read = serial_read; pDevice->handle = -1; pDevice->baudrate = 0; pDevice->sDevice[0] = '\0'; } return (Serial*)pDevice;}void DestroySerialDevice(Serial *pDevice){ if (pDevice) { pDevice->Close(pDevice); free(pDevice); }}
//測(cè)試文件
#include #include #include #include #include "serial.h"int main(int argc, char *argv[]){ char sbuf[128]; int count; int size; int ret; Serial *port = CreateSerialDevice(); assert(port != NULL); ret = port->Open(port, "/dev/ttyS3", 921600); assert(ret == 0); while (1) { snprintf(sbuf, sizeof(sbuf), "%d", count++); size = port->Write(port, sbuf, strlen(sbuf)); assert(size != -1); usleep(1000*1000); } port->Close(port); DestroySerialDevice(port); return 0;}
//工程文件
cmake_minimum_required(VERSION 3.0)project(serial)set(CMAKE_BUILD_TYPE "Debug")add_library(serial SHARED serial.c)add_executable(echo echo.c)target_link_libraries(echo serial)
測(cè)試結(jié)果:
X3派側(cè)echo每隔1s發(fā)送一次遞增數(shù)據(jù),PC側(cè)putty接收并打印。
文中所有文件,均已打包在附件中。歡迎大家一起來(lái)玩轉(zhuǎn)串口通信吧
「地平線旭日X3派,開(kāi)啟你的嵌入式開(kāi)發(fā)之旅」,歡迎正在閱讀的你申請(qǐng)?jiān)囉?,一起交流開(kāi)發(fā)心得
本文轉(zhuǎn)自地平線開(kāi)發(fā)者社區(qū)
原作者:大道至簡(jiǎn)
原鏈接:https://developer.horizon.ai/forumDetail/98129540173361338
-
嵌入式
+關(guān)注
關(guān)注
5082文章
19126瀏覽量
305302 -
AI
+關(guān)注
關(guān)注
87文章
30898瀏覽量
269130 -
人工智能
+關(guān)注
關(guān)注
1791文章
47282瀏覽量
238534
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論