linux串口操作函數(shù)
1.打開(kāi)串口: fd = open("/dev/ttyf1", O_RDWR | O_NOCTTY | O_NDELAY); fcntl(fd, F_SETFL, 0); O_NOCTTY 選項(xiàng)防止程序受鍵盤控制中止操作鍵等影響. O_NDELAY 告訴 UNIX 不必另一端端口是否啟用.(檢測(cè) DCD信號(hào)線狀態(tài))
2.往串口發(fā)送數(shù)據(jù)n = write(fd, "ATZ\r", 4);
3.從串口讀取數(shù)據(jù)當(dāng)以原始數(shù)據(jù)模式(raw data mode)打開(kāi)串口時(shí),read 系統(tǒng)調(diào)用將不管串口輸入緩存里有多少字符可讀都返回.若沒(méi)有數(shù)據(jù),則阻塞直至有字符到來(lái),或定時(shí)器超時(shí).串口設(shè)置這個(gè)選項(xiàng)后,read 調(diào)用都是立即返回.沒(méi)有數(shù)據(jù)可讀時(shí),read 返回 0 fcntl(fd, F_SETFL, FNDELAY);
解除這個(gè)功能是
fcntl(fd, F_SETFL, 0); 4.關(guān)閉串口
close(fd);
二.標(biāo)準(zhǔn)的 POSIX 配置串口參數(shù)串口收發(fā)數(shù)據(jù)主要是要做好端口配置工作,需要包含
POSIX控制函數(shù)
termios結(jié)構(gòu)
Table 3 - Termios Structure Members
Member Description
c_cflag Control options
c_lflag Line options
c_iflag Input options
c_oflag Output options
c_cc Control characters
c_ispeed Input baud (new interface)
c_ospeed Output baud (new interface)
struct termios termios_old,termios_new;
1)獲取串口屬性
tcgetattr(fdcom, &termios_old);
2)配置輸入速率
cfsetispeed(&termios_new, baudrate); cfsetospeed(&termios_new, baudrate); 3) 控制模式,保證程序不會(huì)成為端口的占有者termios_new.c_cflag |= CLOCAL;控制模式,使能端口讀取輸入的數(shù)據(jù)termios_new.c_cflag |= CREAD; 4) 控制模式,屏蔽字符大小位,設(shè)置串口傳輸數(shù)據(jù)所用的位數(shù)termios_new.c_cflag &= ~CSIZE; termios_new.c_cflag |= CS5;//CS6,CS7,CS8
5)奇偶校驗(yàn)parity check
//無(wú)奇偶校驗(yàn)
termios_new.c_cflag &= ~PARENB ;
//偶校驗(yàn)
termios_new.c_cflag |= PARENB; termios_new.c_cflag &= PARODD;
//奇校驗(yàn)
termios_new.c_cflag |= PARENB; termios_new.c_cflag |= PARODD;
6)設(shè)置停止位
termios_new.c_cflag |= CSTOPB; //2stop bits termios_new.c_cflag &= ~CSTOPB;//1 stop bits.
7)其他屬性配置
termios_new.c_oflag &= ~OPOST; //輸出模式,原始數(shù)據(jù)輸出termios_new.c_cc[VMIN] = 1; //控制字符,所要讀取字符的最小數(shù)量termios_new.c_cc[VTIME] = 1;//控制字符,讀取第一個(gè)字符的等待時(shí)間,以 0.1 妙為單
位
8)設(shè)置新屬性
tcsetattr(fdcom, TCSANOW, &termios_new);
// TCSANOW:所由改變立即生效
//TCSADRAIN:等待所有東西都被發(fā)送出去后設(shè)置
//TCSAFLUSH:將輸入輸出buffer全部溢出后設(shè)置
采用 select 系統(tǒng)調(diào)用讀取串口數(shù)據(jù)跟其他 socket,設(shè)備數(shù)據(jù)
#p#示例:
假定我們要從一個(gè)串口和一個(gè) socket 讀取數(shù)據(jù).需要判斷每個(gè)文件描述符的輸入數(shù)據(jù)情況,但 10 妙內(nèi)無(wú)數(shù)據(jù)的話,需要通知用戶沒(méi)有數(shù)據(jù)可讀.
/* Initialize the input set */
FD_ZERO(input);
FD_SET(fd, input); FD_SET(socket, input);
max_fd = (socket > fd ? socket : fd) + 1;
/* Initialize the timeout structure */
timeout.tv_sec = 10; timeout.tv_usec = 0;
/* Do the select */
n = select(max_fd,NULL, NULL, ;
/* See if there was an error */
if (n 0)
perror("select failed");
else if (n == 0)
puts("TIMEOUT");
else
{
/* We have input */
if (FD_ISSET(fd, input))
process_fd();
if (FD_ISSET(socket, input))
process_socket();
}
三.unix/linux下,采用 ioctl函數(shù)來(lái)實(shí)現(xiàn)串口配置功能int ioctl(int fd, int request, ...); fd 是串口描述符,request參數(shù)是定義在
Table 10 - IOCTL Requests for Serial Ports
Request Description POSIX Function
TCGETS
Gets the current serial
port settings.
tcgetattr
TCSETS
Sets the serial port
settings immediately. tcsetattr(fd, TCSANOW, &options)
TCSETSF
Sets the serial port
settings after flushing the
input and output buffers. tcsetattr(fd, TCSAFLUSH, &options)
TCSETSW
Sets the serial port
settings after allowing
the input and output
buffers to drain/empty. tcsetattr(fd, TCSADRAIN, &options)
TCSBRK
Sends a break for the
given time. tcsendbreak, tcdrain
TCXONC
Controls software flow
control.
tcflow
TCFLSH
Flushes the input and/or
output queue.
tcflush
TIOCMGET
Returns the state of the
"MODEM" bits.
None
TIOCMSET
Sets the state of the
"MODEM" bits.
None
FIONREAD
Returns the number of
bytes in the input buffer.
None
為獲取狀態(tài)位,調(diào)用 ioctl函數(shù),用一個(gè)整數(shù)來(lái)存放位指針.
Listing 5 - Getting the MODEM status bits. #include
int fd;
int status;
ioctl(fd, TIOCMGET, &status);
Listing 6 - Dropping DTR with the TIOCMSET ioctl. #include
int fd;
int status;
ioctl(fd, TIOCMGET, &status);
status &= ~TIOCM_DTR;
ioctl(fd, TIOCMSET, status);
【編輯推薦】