資源簡介
本代碼使用C語言實現(xiàn)串口數(shù)據(jù)發(fā)送、接收,代碼帶詳細(xì)注解,便于初學(xué)者理解,只需要編譯就可直接使用。
代碼片段和文件信息
#include?????
#include?????
#include?????
#include???
#include?????
#include??????
#include????
#include?????
#include??
???
#define?UARTBUFF?512?
int?UartOpen(char*?port)??
{???
int?fd?=?-1;
fd?=?open(?port?O_RDWR|O_NOCTTY|O_NDELAY);??
????if?(fd?0)??
????{??
????????perror(“Can‘t?Open?Serial?Port“);??
????????return?-1;??
????}??
?????//恢復(fù)串口為阻塞狀態(tài)?????????????????????????????????
????if(fcntl(fd?F_SETFL?0)?0)??
????{??
????????printf(“fcntl?failed!\n“);??
????????return?-1;??
????}???????
????
//測試是否為終端設(shè)備??????
????if(0?==?isatty(fd))??
????{??
????????printf(“standard?input?is?not?a?terminal?device\n“);??
????????return(-1);??
????}??
?????????????
return?fd;??
}??
//關(guān)閉串口并返回串口設(shè)備文件描述?
void?UartClose(int?fd)??
{??
????close(fd);??
}??
???
int?UartSet(int?fdint?speedint?flow_ctrlint?databitsint?stopbitschar?parity)??
{??????
????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????8336??2018-01-24?13:37??uart.c
評論
共有 條評論