自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

linux串口操作函數(shù)

系統(tǒng) Linux
打開(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))

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ù)主要是要做好端口配置工作,需要包含,定義終端控制結(jié)構(gòu)以及

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ù)是定義在的常量,一般是下表中的一個(gè)

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 #include

int fd;

int status;

ioctl(fd, TIOCMGET, &status);

Listing 6 - Dropping DTR with the TIOCMSET ioctl. #include #include

int fd;

int status;

ioctl(fd, TIOCMGET, &status);

status &= ~TIOCM_DTR;

ioctl(fd, TIOCMSET, status);

【編輯推薦】

  1. linux下面串口工具C-kermit
  2. linux大掃盲:linux之Tar命令常用參數(shù)
  3. Linux系統(tǒng)巧用NMAP來(lái)收集主機(jī)信息
責(zé)任編輯:趙寧寧 來(lái)源: chinaitlab
相關(guān)推薦

2011-06-22 17:49:35

Linux Qt 串口

2020-10-08 10:10:51

Linux系統(tǒng)編程信號(hào)集

2009-08-25 15:59:28

C#串口操作

2010-04-22 16:01:48

Aix操作系統(tǒng)串口

2009-08-25 17:02:20

C#串口操作

2010-06-11 11:35:55

Linux串口測(cè)試工具

2017-03-13 16:46:11

Linuxminicomusb串口

2009-09-23 10:26:12

linux串口工具C-kermit

2023-03-08 15:55:53

Linux驅(qū)動(dòng)鴻蒙

2009-12-25 16:53:06

Linux操作系統(tǒng)

2012-09-20 09:43:33

Linux服務(wù)器串口Linux

2011-06-22 17:36:50

QT Linux 串口

2010-06-07 14:05:38

Linux串口測(cè)試工具

2010-06-13 17:12:10

Linux串口測(cè)試工具

2014-08-14 09:25:31

Linux串口

2009-12-14 17:46:40

Linux桌面操作系統(tǒng)

2011-01-11 14:56:51

Linux基本操作

2010-05-28 10:53:07

Linux串口測(cè)試工具

2009-04-11 15:12:24

VxWorks串口例子

2020-12-29 16:39:01

Linux代碼命令
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)