?? serial.c
字號(hào):
/** @file Serial.c* @brief 串口部分API程序, 該部分封裝底層驅(qū)動(dòng), 為用戶(hù)提供接口* @Notice 串口號(hào)定義為, CPU內(nèi)部為0 1 擴(kuò)展串口依次后面加, 如有問(wèn)題請(qǐng)聯(lián)系作者* @Author Mars.zhu@hotmail.com 2007-9-22 8:01*/#include "option.h"#include "tcuart.h"#include "Sermem.h"#include "Serial.h"static S_TermiosUart_dev[UART_CNT]; /** 定義串口結(jié)構(gòu), 該結(jié)構(gòu)中保存參數(shù)*//*********************************************************************************************************** 內(nèi)部函數(shù)**********************************************************************************************************/static voidcfsetispeed(S_Termios *tios_p, speed_t speed){ tios_p->c_baud = speed;}#define cfsetospeed(...)static D_Resulttcsetattr(uint8 fd){ if (fd >= UART_CNT) return D_ERR; switch (fd) { case 0: case 1: { // CPU內(nèi)部串口設(shè)置 UARTn_Init(fd, &Uart_dev[fd]); break; } default: break; } return D_OK;}/*********************************************************************************************************** 用戶(hù)初始化API, 類(lèi)UNIX接口**********************************************************************************************************//** @func open_port* @brief 打開(kāi)串口* @Retval D_OK 成功* D_ERR 失敗*/D_Resultopen_port(uint8 fd, uint8 comport){ fd = fd; if (Uart_dev[comport].c_open_fg == 1) return D_ERR; Uart_dev[comport].c_open_fg = 1; UartInitBuf(fd); return D_OK;}/** @func close_port* @brief 關(guān)閉串口* @Retval D_OK 成功* D_ERR 失敗*/D_Resultclose_port(uint8 fd, uint8 comport){ fd = fd; if (Uart_dev[comport].c_open_fg == 0) return D_ERR; Uart_dev[comport].c_open_fg = 0; return D_OK;}/** @func set_opt* @brief 設(shè)置串口屬性函數(shù)* @Retval D_OK 成功* D_ERR 失敗*/D_Resultset_opt(uint8 fd, speed_t nSpeed, uint8 nBits, char nEvent, uint8 nStop){ /** 設(shè)置波特率*/ switch (nSpeed) { case 2400: cfsetispeed(&Uart_dev[fd], B2400); cfsetospeed(&Uart_dev[fd], B2400); break; case 8400: cfsetispeed(&Uart_dev[fd], B4800); cfsetospeed(&Uart_dev[fd], B4800); break; case 9600: cfsetispeed(&Uart_dev[fd], B9600); cfsetospeed(&Uart_dev[fd], B9600); break; case 57600: cfsetispeed(&Uart_dev[fd], B57600); cfsetospeed(&Uart_dev[fd], B57600); break; case 115200: cfsetispeed(&Uart_dev[fd], B115200); cfsetospeed(&Uart_dev[fd], B115200); break; case 230400: cfsetispeed(&Uart_dev[fd], B230400); cfsetospeed(&Uart_dev[fd], B230400); break; case 460800: cfsetispeed(&Uart_dev[fd], B460800); cfsetospeed(&Uart_dev[fd], B460800); break; default: cfsetispeed(&Uart_dev[fd], B9600); cfsetospeed(&Uart_dev[fd], B9600); break; } /** 設(shè)置數(shù)據(jù)位*/ switch (nBits) { case 5: { Uart_dev[fd].c_cflag |= CS5; break; } case 6: { Uart_dev[fd].c_cflag |= CS6; break; } case 7: { Uart_dev[fd].c_cflag |= CS7; break; } case 8: { Uart_dev[fd].c_cflag |= CS8; break; } default: break; } /** 設(shè)置奇偶校驗(yàn)位*/ switch (nEvent) { case 'O': { // 奇數(shù) Uart_dev[fd].c_cflag |= PARENB; Uart_dev[fd].c_cflag |= PARODD; break; } case 'E': { // 偶數(shù) Uart_dev[fd].c_cflag |= PARENB; Uart_dev[fd].c_cflag &= ~PARODD; break; } case 'N': { // 無(wú)奇偶校驗(yàn)位 Uart_dev[fd].c_cflag &= ~PARENB; break; } default: break; } /** 設(shè)置停止位*/ if (nStop == 1) Uart_dev[fd].c_cflag &= ~CSTOPB; else if (nStop == 2) Uart_dev[fd].c_cflag |= CSTOPB; /** FIFO 使能*/ Uart_dev[fd].c_FIFO_data = 1; return tcsetattr(fd);}/** @func uart_write* @brief 向串口寫(xiě)入數(shù)據(jù)* @param fd 打開(kāi)的串口描述符* data_buf 發(fā)送數(shù)據(jù)緩沖區(qū)* NByte 發(fā)送字節(jié)數(shù)* @Retval writen 成功發(fā)送字節(jié)數(shù)*/uint16uart_write(uint8 fd, const uint8 *data_buf, uint16 NByte){ uint16 writen; if (Uart_dev[fd].c_open_fg != 1) return 0; for (writen=0; writen<NByte; writen++) { if (UsrPutc(fd, data_buf[writen]) != 0) break; } UartFlush(fd); return writen;}/** @func uart_read* @brief 讀取串口數(shù)據(jù)* @param fd 打開(kāi)的串口描述符* data_buf 接收數(shù)據(jù)緩沖區(qū)* NByte 接收字節(jié)數(shù)* @Retval 實(shí)際接收字節(jié)數(shù)*/uint16uart_read(uint8 fd, uint8 *data_buf, uint16 NByte){ uint8 dat; uint16 readn; if (Uart_dev[fd].c_open_fg != 1) return 0; for (readn=0; readn<NByte; readn++) { if (UsrGetc(fd, &dat) == 0) data_buf[readn] = dat; else break; } return readn;}/** @func uart_putch* @brief 向串口緩沖區(qū)寫(xiě)1字節(jié)數(shù)據(jù)* @param fd 打開(kāi)的串口描述符* ch 寫(xiě)入的字符* @Retval 0 成功*/D_Resultuart_putch(uint8 fd, uint8 ch){ return UsrPutc(fd, ch);}/** @func uart_getch* @brief 從串口緩沖區(qū)讀1字節(jié)數(shù)據(jù)* @param fd 打開(kāi)的串口描述符* ch 讀取的字符* @Retval 0 成功*/D_Resultuart_getch(uint8 fd, uint8 *ch){ return UsrGetc(fd, ch);}/** @func uart_print* @brief 輸出字符串* @param fd 打開(kāi)的串口描述符* str 字符串指針, 字符串只輸出MaxLenStr以?xún)?nèi)部分* @Retval void*/voiduart_print(uint8 fd, char *str){ uint8 i; char ch; for (i=0; i<MaxLenStr; i++) { if ((ch=str[i]) == '\0') { break; } else if (ch == '\n') { while (UsrPutc(fd, 0x0D)); while (UsrPutc(fd, 0x0A)); } else { while (UsrPutc(fd, ch)); } } UartFlush(fd);}voiduart_print_hex(uint8 fd, uint8 hex){ uint8 c; c = hex >> 4; if (c<=9) c = c + '0'; else c = c + 'A' - 10; UsrPutc(fd, c); c = hex & 0x0F; if (c<=9) c = c + '0'; else c = c + 'A' - 10; UsrPutc(fd, c); UartFlush(fd);}voiduart_print_dec(uint8 fd, uint8 dec){ uint8 c; if (dec > 99) { c = dec / 100; c = c + '0'; UsrPutc(fd, c); dec %= 100; c = dec / 10; c = c + '0'; UsrPutc(fd, c); c = dec % 10; c = c + '0'; UsrPutc(fd, c); } else if (dec > 9) { dec %= 100; c = dec / 10; c = c + '0'; UsrPutc(fd, c); c = dec % 10; c = c + '0'; UsrPutc(fd, c); } else { c = dec % 10; c = c + '0'; UsrPutc(fd, c); } UartFlush(fd);}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -