當前位置:ag真人国际官网-ag旗舰厅官方网站 » 編程軟體 » linux串口編程

linux串口編程-ag真人国际官网

發布時間: 2022-01-08 01:36:19

linux下的串口編程

這有個友善的串口常式,參考下吧,用gcc編譯可以在linux下用
# include
# include
# include
# include
# include
# include
# include
# include
# include

int commfd, ttyfd;

static void error(const char *msg)
{
fprintf (stderr, "%s\n", msg);
fprintf (stderr, "strerror() is %s\n", strerror(errno));
exit(1);
}
static void warning(const char *msg)
{
fprintf (stderr, "warning: %s\n", msg);
}

static int serialspeed(const char *speedstring)
{
int speednumber = atoi(speedstring);
# define testspeed(speed) if (speednumber == speed) return b##speed
testspeed(1200);
testspeed(2400);
testspeed(4800);
testspeed(9600);
testspeed(19200);
testspeed(38400);
testspeed(57600);
testspeed(115200);
testspeed(230400);
error("bad speed");
return -1;
}

static void printusage(void)
{

fprintf(stderr, "comtest - interactive program of comm port\n");
fprintf(stderr, "press [esc] 3 times to quit\n\n");

fprintf(stderr, "usage: comtest [-d device] [-t tty] [-s speed] [-7] [-c] [-x] [-o] [-h]\n");
fprintf(stderr, " -7 7 bit\n");
fprintf(stderr, " -x hex mode\n");
fprintf(stderr, " -o output to stdout too\n");
fprintf(stderr, " -c stdout output use color\n");
fprintf(stderr, " -h print this help\n");
exit(-1);
}

static inline void waitfdwriteable(int fd)
{
fd_set writesetfd;
fd_zero(&writesetfd);
fd_set(fd, &writesetfd);
if (select(fd 1, null, &writesetfd, null, null) < 0) {
error(strerror(errno));
}

}

int senduart(char c)
{
waitfdwriteable(commfd);
return write(commfd, &c, 1);
}

char recuart()
{
char c='\0';
if (fd_isset(commfd, &readsetfd))
{
if(read(commfd, &c, 1) == 1) return c;
else printf("no data to receive.\n");
}
}

int main(int argc, char **argv)
{
struct termios ttyattr;
struct termios backupttyattr;

int devicespeed = b115200;
int ttyspeed = b115200;
int bytebits = cs8;
const char *devicename = "/dev/ttys0";
const char *ttyname = "/dev/tty";
int outputhex = 0;
int outputtostdout = 0;
int usecolor = 0;

printf("now we start.\n");
opterr = 0;
for (;;) {
int c = getopt(argc, argv, "d:s:t:7xoch");
if (c == -1)
break;
switch(c) {
case 'd':
devicename = optarg;
break;
case 't':
ttyname = optarg;
break;
case 's':
if (optarg[0] == 'd') {
devicespeed = serialspeed(optarg 1);
} else if (optarg[0] == 't') {
ttyspeed = serialspeed(optarg 1);
} else
ttyspeed = devicespeed = serialspeed(optarg);
break;
case 'o':
outputtostdout = 1;
break;
case '7':
bytebits = cs7;
break;
case 'x':
outputhex = 1;
break;
case 'c':
usecolor = 1;
break;
case '?':
case 'h':
default:
printusage();
}
}
if (optind != argc)
printusage();

commfd = open(devicename, o_rdwr, 0);
if (commfd < 0)
error("unable to open device");
if (fcntl(commfd, f_setfl, o_nonblock) < 0)
error("unable set to nonblock mode");

memset(&ttyattr, 0, sizeof(struct termios));
ttyattr.c_iflag = ignpar;
ttyattr.c_cflag = devicespeed | hupcl | bytebits | cread | clocal;
ttyattr.c_cc[vmin] = 1;

if (tcsetattr(commfd, tcsanow, &ttyattr) < 0)
warning("unable to set comm port");

ttyfd = open(ttyname, o_rdwr | o_ndelay, 0);
if (ttyfd < 0)
error("unable to open tty");

ttyattr.c_cflag = ttyspeed | hupcl | bytebits | cread | clocal;
if (tcgetattr(ttyfd, &backupttyattr) < 0)
error("unable to get tty");

if (tcsetattr(ttyfd, tcsanow, &ttyattr) < 0)
error("unable to set tty");

for (;;) {
unsigned char char = 0;
fd_set readsetfd;

void outputstdchar(file *file) {
char buffer[10];
int len = sprintf(buffer, outputhex ? "%.2x " : "%c", char);
fwrite(buffer, 1, len, file);
}

fd_zero(&readsetfd);

fd_set(commfd, &readsetfd);
fd_set( ttyfd, &readsetfd);
# define max(x,y) ( ((x) >= (y)) ? (x) : (y) )
if (select(max(commfd, ttyfd) 1, &readsetfd, null, null, null) < 0) {
error(strerror(errno));
}
# undef max

if (fd_isset(commfd, &readsetfd)) {
while (read(commfd, &char, 1) == 1) {

waitfdwriteable(ttyfd);
if (write(ttyfd, &char, 1) < 0) {
error(strerror(errno));
}
if (outputtostdout) {
if (usecolor)
fwrite("\x1b[01;34m", 1, 8, stdout);
outputstdchar(stdout);
if (usecolor)
fwrite("\x1b[00m", 1, 8, stdout);
fflush(stdout);
}
}
}

if (fd_isset(ttyfd, &readsetfd)) {
while (read(ttyfd, &char, 1) == 1) {
static int esckeycount = 0;
waitfdwriteable(commfd);
if (write(commfd, &char, 1) < 0) {
error(strerror(errno));
}
if (outputtostdout) {
if (usecolor)
fwrite("\x1b[01;31m", 1, 8, stderr);
outputstdchar(stderr);
if (usecolor)
fwrite("\x1b[00m", 1, 8, stderr);
fflush(stderr);
}

if (char == '\x1b') {
esckeycount ;
if (esckeycount >= 3)
goto exitlabel;
} else
esckeycount = 0;
}
}

}

exitlabel:
if (tcsetattr(ttyfd, tcsanow, &backupttyattr) < 0)
error("unable to set tty");

return 0;
}

⑵ linux rs485串口編程

對於編程來說,沒什麼區別,通過控制485的使能端該程序完全可以使用。唯一的區別就是你在發送的時候通過程序把485的控制腳拉高,接收的時候把他拉低就可以了。至於電氣方面的區別:rs232是全雙工,可以同時收發,rs485是半雙工,不能同時收發,還有電平信號不一樣,這個編程你就不要理了。

⑶ linux文件編程和串口編程的基本概念是什麼

簡單說幾句吧,linux下的設備都是文件,流程也無非是open, read/write, close等當然,串口你得設置各種屬性才行對不對,比如在win下的超級終端就設置了波特率啊,停止位啊,奇偶校驗啊什麼的,這些屬性都通過 int tcgetattr(int fd, struct termios *termios_p); int tcsetattr(int fd, int optional_actions, const struct termios *termios_p);函數來設置。完整代碼嗎自己去google,一把一把的,其實最重要的是設置好屬性,剩下的就是read,write的問題咯。希望對你有用對了,了解終端函數的詳情請在linux命令行終端獲取: man termios

⑷ 如何實現linux下的串口中斷編程

#include
#include
#include
#include
#include
#include

#define baudrate b38400
#define modemdevice "/dev/ttys1"
#define _posix_source 1 /* posix 系統相容 */
#define false 0
#define true 1

volatile int stop=false;

void signal_handler_io (int status); /* 定義訊號處理程序 */
int wait_flag=true; /* 沒收到訊號的話就會是 true */

main()
{
int fd,c, res;
struct termios oldtio,newtio;
struct sigaction saio; /* definition of signal action */
char buf[255];

/* 開啟裝置為 non-blocking (讀取功能會馬上結束返回) */
fd = open(modemdevice, o_rdwr | o_noctty | o_nonblock);
if (fd <0) {perror(modemdevice); exit(-1); }

/* 在使裝置非同步化前, 安裝訊號處理程序 */
saio.sa_handler = signal_handler_io;
saio.sa_mask = 0;
saio.sa_flags = 0;
saio.sa_restorer = null;
sigaction(sigio,&saio,null);

/* 允許行程去接收 sigio 訊號*/
fcntl(fd, f_setown, getpid());
/* 使檔案ake the file descriptor 非同步 (使用手冊上說只有 o_append 及
o_nonblock, 而 f_setfl 也可以用...) */
fcntl(fd, f_setfl, fasync);

tcgetattr(fd,&oldtio); /* 儲存目前的序列埠設定值 */
/* 設定新的序列埠為標准輸入程序 */
newtio.c_cflag = baudrate | crtscts | cs8 | clocal | cread;
newtio.c_iflag = ignpar | icrnl;
newtio.c_oflag = 0;
newtio.c_lflag = icanon;
newtio.c_cc[vmin]=1;
newtio.c_cc[vtime]=0;
tcflush(fd, tciflush);
tcsetattr(fd,tcsanow,&newtio);

/* 等待輸入訊號的迴圈. 很多有用的事我們將在這做 */
while (stop==false) {
printf(".\n");usleep(100000);
/* 在收到 sigio 後, wait_flag = false, 輸入訊號存在則可以被讀取 */
if (wait_flag==false) {
res = read(fd,buf,255);
buf[res]=0;
printf(":%s:%d\n", buf, res);
if (res==1) stop=true; /* 如果只輸入 cr 則停止迴圈 */
wait_flag = true; /* 等待新的輸入訊號 */
}
}
/* 回存舊的序列埠設定值 */
tcsetattr(fd,tcsanow,&oldtio);
}

⑸ linux 串口編程亂碼

關於串口設備,最好聯系廠家詢問是否提供api介面。

關於api介面一般是一個可供調用的dll文件。

如果有可以直接在c#中引用,作為類庫來操作設備。

具體調用方式需要詢問廠家或參閱api的文檔說明。

不提供軟體介面的設備是無法進行開發的。

我大概搜了下這個設備,設備是提供配套軟體的,那麼這款設備是有相關通訊介面類庫的,也就是可以用c#進行開發。
如果找不到api文檔,
請嘗試用串口調試工具,跟蹤配套軟體的每一步操作,獲取串口通訊報文,用c#模擬操作報文自己封裝通訊類後進行開發。

⑹ 如何在linux下編寫一個c語言的串口程序

1、參考這個:posix操作系統串口編程指南和 unix環境高級編程。
2、簡單介紹一下:
《posix操作系統的串口編程指南》是在unix環境或pc上對串口進行編程的教程,每一章提供的常式都使用posix(portable standard for unix)終端控制函數,只需極少的修改就可運行在irix 、hp-ux、 sunos、 solaris、 digital unix、 linux等大多數類unix操作系統。

⑺ 很簡單的linux串口編程問題:fd = open("/dev/ttysn",|xxxxxxx)。其中哪個ttysn具體是多少

沒做過linux下的,提供個建議,看成不成。
pc端的串口必須配置正確。 要確定 與 板子的 波特率 要一致。
此外, pc端作為串口總控端, com埠配置只針對於pc端自己。 比如你使用了com1口,那麼定義的時候,(ttysn 應該是 ttysn1 --- 沒用過linux下的不知道是不是在這配置,你要查)

板子端的com口配置也是只針對於自己,如果你使用板子的com1和com2, 那麼程序中初始化的時候需要同時把com1/2都初始化,那麼pc端就可以連接任意的板子埠。

pc(com1) ---- 板子(com1) 或 pc(com1) ---- 板子(com2)

在確保板子硬體沒有問題的情況下,且pc端程序無誤, 如果pc端無法接收到數據, 嘗試
在pc端編寫程序時,在 接收數據之前 加上時間延遲。 也就是說,pc發出數據後需要等待
一段時間才能接收到板子 返回的數據。 具體時間測試來看。

⑻ 關於linux下多串口編程

或許 是 硬體資源 的 問題
串口 相關的 中斷、io 地址等等

⑼ linux下怎樣對串口編程

使用串口協議登錄linux終端控制台,通過zmodem文件傳輸協議接收一個外部文件。 命令:rz -y 會彈出文件瀏覽窗口,選擇要上傳的文件即可。 -y 表示若文件已存在,則覆蓋。

熱點內容
仙境傳說手游腳本 發布:2024-07-17 16:09:24 瀏覽:690
matlab命令窗口和新建腳本 發布:2024-07-17 15:51:26 瀏覽:374
建ftp文件夾 發布:2024-07-17 15:51:26 瀏覽:954
魔獸撿物腳本 發布:2024-07-17 15:27:56 瀏覽:129
開發ip伺服器 發布:2024-07-17 15:24:42 瀏覽:387
安卓系統視頻製作哪個好用 發布:2024-07-17 15:10:47 瀏覽:210
androidapk結構 發布:2024-07-17 15:10:43 瀏覽:945
c語言指針的例子 發布:2024-07-17 15:08:01 瀏覽:768
linuxzcat 發布:2024-07-17 15:02:09 瀏覽:901
賓士編程嗎 發布:2024-07-17 14:57:08 瀏覽:853
网站地图