成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

linux串口操作函數

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

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 配置串口參數串口收發數據主要是要做好端口配置工作,需要包含,定義終端控制結構以及

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,設備數據

#p#

示例:

假定我們要從一個串口和一個 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參數是定義在的常量,一般是下表中的一個

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 #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命令常用參數
  3. Linux系統巧用NMAP來收集主機信息
責任編輯:趙寧寧 來源: chinaitlab
相關推薦

2011-06-22 17:49:35

Linux Qt 串口

2020-10-08 10:10:51

Linux系統編程信號集

2009-08-25 15:59:28

C#串口操作

2009-08-25 17:02:20

C#串口操作

2010-04-22 16:01:48

Aix操作系統串口

2009-09-23 10:26:12

linux串口工具C-kermit

2023-03-08 15:55:53

Linux驅動鴻蒙

2010-06-11 11:35:55

Linux串口測試工具

2017-03-13 16:46:11

Linuxminicomusb串口

2009-12-25 16:53:06

Linux操作系統

2010-06-07 14:05:38

Linux串口測試工具

2012-09-20 09:43:33

Linux服務器串口Linux

2011-06-22 17:36:50

QT Linux 串口

2014-08-14 09:25:31

Linux串口

2010-06-13 17:12:10

Linux串口測試工具

2009-12-14 17:46:40

Linux桌面操作系統

2011-01-11 14:56:51

Linux基本操作

2010-05-28 10:53:07

Linux串口測試工具

2010-06-18 10:42:51

Linux Acces

2020-12-29 16:39:01

Linux代碼命令
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美激情精品久久久久久免费 | 91在线一区二区三区 | 久久久久国产精品 | 一区二区三区日韩精品 | 亚洲网站在线观看 | 日韩精品1区2区 | 国产精品成人一区二区三区 | 日本中文字幕视频 | 久久久无码精品亚洲日韩按摩 | 成人免费区一区二区三区 | 国产欧美精品在线 | 四虎影院免费在线 | 日韩精品一区二区三区中文字幕 | 久久久久国产视频 | 中文字幕一区二区三区四区五区 | 伦理午夜电影免费观看 | 一区二区三区在线免费观看 | 中文字幕日韩一区二区 | 伊人免费视频二 | 国产视频久久久 | 日韩欧美三区 | 中文字幕在线一区二区三区 | 国产精品视频久久久 | 久久精品小视频 | 九九福利 | h视频免费在线观看 | av一二三区 | 欧美亚洲网站 | 成人日韩 | 国产成人自拍av | 视频一区在线播放 | 成人在线中文字幕 | 中文一区 | 国产精品中文字幕在线播放 | 日韩一区av | 欧美电影免费观看 | 做a视频 | 最新中文字幕在线播放 | 夜夜爽99久久国产综合精品女不卡 | 日韩播放| 成人欧美一区二区三区黑人孕妇 |