歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux服務器 >> linux串口操作函數

linux串口操作函數

日期:2017/3/2 16:39:52   编辑:Linux服務器

1.打開串口: fd = open("/dev/ttyf1", O_RDWR | O_NOCTTY | O_NDELAY); fcntl(fd, F_SETFL, 0); O_NOCTTY 選項防止程序受鍵盤控制中止操作鍵等影響. O_NDELAY 告訴 UNIX 不必另一端端口是否啟用.(檢測 DCD信號線狀態)

2.往串口發送數據n = write(fd, "ATZ\r", 4);

3.從串口讀取數據當以原始數據模式(raw data mode)打開串口時,read 系統調用將不管串口輸入緩存裡有多少字符可讀都返回.若沒有數據,則阻塞直至有字符到來,或定時器超時.串口設置這個選項後,read 調用都是立即返回.沒有數據可讀時,read 返回 0 fcntl(fd, F_SETFL, FNDELAY);

解除這個功能是

fcntl(fd, F_SETFL, 0); 4.關閉串口

close(fd);

二.標准的 POSIX 配置串口參數串口收發數據主要是要做好端口配置工作,需要包含<termios.h>,定義終端控制結構以及

POSIX控制函數

termios結構

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) 控制模式,保證程序不會成為端口的占有者termios_new.c_cflag |= CLOCAL;控制模式,使能端口讀取輸入的數據termios_new.c_cflag |= CREAD; 4) 控制模式,屏蔽字符大小位,設置串口傳輸數據所用的位數termios_new.c_cflag &= ~CSIZE; termios_new.c_cflag |= CS5; //CS6,CS7,CS8

5)奇偶校驗parity check

//無奇偶校驗

termios_new.c_cflag &= ~PARENB ;

//偶校驗

termios_new.c_cflag |= PARENB; termios_new.c_cflag &= PARODD;

//奇校驗

termios_new.c_cflag |= PARENB; termios_new.c_cflag |= PARODD;

6)設置停止位

termios_new.c_cflag |= CSTOPB; //2stop bits termios_new.c_cflag &= ~CSTOPB; //1 stop bits.

7)其他屬性配置

termios_new.c_oflag &= ~OPOST; //輸出模式,原始數據輸出termios_new.c_cc[VMIN] = 1; //控制字符,所要讀取字符的最小數量termios_new.c_cc[VTIME] = 1;//控制字符,讀取第一個字符的等待時間,以 0.1 妙為單

8)設置新屬性

tcsetattr(fdcom, TCSANOW, &termios_new);

// TCSANOW:所由改變立即生效

//TCSADRAIN:等待所有東西都被發送出去後設置

//TCSAFLUSH:將輸入輸出buffer全部溢出後設置

采用 select 系統調用讀取串口數據跟其他 socket,設備數據

示例:

假定我們要從一個串口和一個 socket 讀取數據.需要判斷每個文件描述符的輸入數據情況,但 10 妙內無數據的話,需要通知用戶沒有數據可讀.

/* 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函數來實現串口配置功能int ioctl(int fd, int request, ...); fd 是串口描述符,request參數是定義在<termios.h>的常量,一般是下表中的一個

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

為獲取狀態位,調用 ioctl函數,用一個整數來存放位指針.

Listing 5 - Getting the MODEM status bits. #include <unistd.h> #include <termios.h>

int fd;

int status;

ioctl(fd, TIOCMGET, &status);

Listing 6 - Dropping DTR with the TIOCMSET ioctl. #include <unistd.h> #include <termios.h>

int fd;

int status;

ioctl(fd, TIOCMGET, &status);

status &= ~TIOCM_DTR;

ioctl(fd, TIOCMSET, status);

Copyright © Linux教程網 All Rights Reserved