linux環境下串列埠通訊的程式設計
整理:ackarlix
串列埠裝置無論是在工控領域,還是在嵌入式裝置領域,應用都非常廣泛。而串列埠程式設計也就顯得必不可少。偶然的一次機會,需要使用串列埠,而且作業系統還要求是linux,因此,趁著這次機會,綜合別人的**,進行了一次整理和封裝。
具體的封裝格式為c**,這樣做是為了很好的移植性,使它可以在c和c++環境下,都可以編譯和使用。**的標頭檔案如下:
/////filename:stty.h
#ifndef __stty_h__
#define __stty_h__
#include
#include
#include
#include
#include
#include
#include
#include
#include
//// 串列埠裝置資訊結構
typedef struct tty_info_t
tty_info;
//// 串列埠操作函式
tty_info *readytty(int id);
int setttyspeed(tty_info *ptty, int speed);
int setttyparity(tty_info *ptty,int databits,int parity,int stopbits);
int cleantty(tty_info *ptty);
int sendntty(tty_info *ptty,char *pbuf,int size);
int recvntty(tty_info *ptty,char *pbuf,int size);
int locktty(tty_info *ptty);
int unlocktty(tty_info *ptty);
#endif
從頭檔案中的函式定義不難看出,函式的功能,使用過程如下:
(1) 開啟串列埠裝置,呼叫函式setttyspeed();
(2) 設定串列埠讀寫的波特率,呼叫函式setttyspeed();
(3) 設定串列埠的屬性,包括停止位、校驗位、資料位等,呼叫函式setttyparity();
(4) 向串列埠寫入資料,呼叫函式sendntty();
(5) 從串列埠讀出資料,呼叫函式recvntty();
(6) 操作完成後,需要呼叫函式cleantty()來釋放申請的串列埠資訊介面;
其中,locktty()和unlocktty()是為了能夠在多執行緒中使用。在讀寫操作的前後,需要鎖定和釋放串列埠資源。
具體的使用方法,在**實現的原檔案中,main()函式中進行了演示。下面就是源**檔案:
//stty.c
#include
#include
#include "stty.h"
///// 初始化串列埠裝置並進行原有設定的儲存
tty_info *readytty(int id)
//// 取得並且儲存原來的設定
tcgetattr(ptty->fd,&ptty->otm);
return ptty;
}///
// 清理串列埠裝置資源
int cleantty(tty_info *ptty)
return 0;
}///
// 設定串列埠通訊速率
// ptty 引數型別(tty_info *),已經初始化的串列埠裝置資訊結構指標
// speed 引數型別(int),用來設定串列埠的波特率
// return 返回值型別(int),函式執行成功返回零值,否則返回大於零的值
///int setttyspeed(tty_info *ptty, int speed)
ptty->ntm.c_iflag = ignpar;
ptty->ntm.c_oflag = 0;
////
tcflush(ptty->fd, tciflush);
tcsetattr(ptty->fd,tcsanow,&ptty->ntm);
////
return 0;}
///// 設定串列埠資料位,停止位和效驗位
// ptty 引數型別(tty_info *),已經初始化的串列埠裝置資訊結構指標
// databits 引數型別(int), 資料位,取值為7或者8
// stopbits 引數型別(int),停止位,取值為1或者2
// parity 引數型別(int),效驗型別 取值為n,e,o,,s
// return 返回值型別(int),函式執行成功返回零值,否則返回大於零的值
///int setttyparity(tty_info *ptty,int databits,int parity,int stopbits)
bzero(&ptty->ntm, sizeof(ptty->ntm));
ptty->ntm.c_cflag = cs8 | clocal | cread;
ptty->ntm.c_iflag = ignpar;
ptty->ntm.c_oflag = 0;
//// 設定串列埠的各種引數
ptty->ntm.c_cflag &= ~csize;
switch (databits)
////
switch (parity)
//// 設定停止位
switch (stopbits)
////
ptty->ntm.c_lflag = 0;
ptty->ntm.c_cc[vtime] = 0; // inter-character timer unused
ptty->ntm.c_cc[vmin] = 1; // blocking read until 1 chars received
tcflush(ptty->fd, tciflush);
if (tcsetattr(ptty->fd,tcsanow,&ptty->ntm) != 0)
return 0;
}int recvntty(tty_info *ptty,char *pbuf,int size)
pthread_mutex_unlock(&ptty->mt);
if(ret >0)
usleep(100);
}return size - left;
}int sendntty(tty_info *ptty,char *pbuf,int size)
//usleep(100);
}return size - nleft;}
int locktty(tty_info *ptty)
return flock(ptty->fd,lock_ex);}
int unlocktty(tty_info *ptty)
return flock(ptty->fd,lock_un);
}#ifdef leaf_tty_test
///// 介面測試
Linux 下串列埠通訊程式設計
int open com char device name return fd end of open com 一 串列埠程式需要的標頭檔案 include 標準輸入輸出定義 include 標準函式庫定義 include unix標準函式定義 include include include 檔案控...
zz Linux環境下串列埠通訊的程式設計
linux環境下串列埠通訊的程式設計 整理 ackarlix 串列埠裝置無論是在工控領域,還是在嵌入式裝置領域,應用都非常廣泛。而串列埠程式設計也就顯得必不可少。偶然的一次機會,需要使用串列埠,而且作業系統還要求是linux,因此,趁著這次機會,綜合別人的 進行了一次整理和封裝。具體的封裝格式為c ...
Linux 串列埠通訊程式設計
串列埠通過直接連線在兩台裝置間的線傳送和接收資料,兩台裝置通訊最少需要三根線 傳送資料 接收資料和接地 才可以通訊。以最常見的 rs232 串列埠為例,通訊距離較近時 12m 可以用電纜線直接連線標準 rs232 埠。如果傳輸距離遠,可以通過數據機 modem 傳輸。因為串列埠裝置工作頻率低且容易受...