適用於所有arm開發板的串列埠程式設計示例 Linux

2021-09-30 09:23:59 字數 3074 閱讀 1092

本程式摘自友善之臂最新linux示例(光碟中的/linux/exapmples.tgz解壓出來可得此檔案)

說明:comtest

程式是友善之臂早期開發的乙個串列埠測試程式,它其實是乙個十分簡易的串列埠終端程式,類似於

linux

中的minicom

,該程式與硬體無關,因此相同的**不僅適用於任何

arm-linux

開發板平台,也可以在

pc linux

上執行使用,方法都是完全一樣的。通過該程式你可以了解串列埠程式設計的一些常見關鍵設定,對於

linux

# include

# include

# include

# include

# include

# include

# include

# include

# include

static void error(const char *msg)

static void warning(const char *msg)

static int serialspeed(const char *speedstring)

static void printusage(void)

static inline void waitfdwriteable(int fd)

} int main(int argc, char **ar**)

else if (optarg[0] == 't') 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 (;;)

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)

# undef max

if (fd_isset(commfd, &readsetfd))

if (outputtostdout) }

}if (fd_isset(ttyfd, &readsetfd))

if (outputtostdout)

if (char == '/x1b') else

esckeycount = 0; } 

} }exitlabel:

if (tcsetattr(ttyfd, tcsanow, &backupttyattr) < 0)

error("unable to set tty");

return 0; }

編譯指令碼:

在arm平台上:

本指令碼適用於最新編譯器arm-linux-gcc-4.3.2 with  eabi

#!/bin/bash

arm-linux-gcc -wall -o3 -o armcomtest com_test.c

arm-linux-strip --strip-all armcomtest

編譯可得到armcomtest執行程式(所有友善的開發板均已經包含此執行程式)

使用其他編譯器,可以這樣:

arm-linux-gcc -o armcomtest com_test.c

在x86平台:

gcc -o comtest com_test.c

可以編譯出comtest程式,它相當於乙個微型的minicom

該程式執行後的退出方式:連續按三次"esc"按鍵(就是你的pc鍵盤)

適用於所有arm開發板的串列埠程式設計示例 Linux

本程式摘自友善之臂最新linux示例 光碟中的 linux exapmples.tgz解壓出來可得此檔案 說明 comtest 程式是友善之臂早期開發的乙個串列埠測試程式,它其實是乙個十分簡易的串列埠終端程式,類似於 linux 中的minicom 該程式與硬體無關,因此相同的 不僅適用於任何 ar...

適用於所有arm開發板的串列埠程式設計示例 Linux

本程式摘自友善之臂最新linux示例 光碟中的 linux exapmples.tgz解壓出來可得此檔案 說明 comtest 程式是友善之臂早期開發的乙個串列埠測試程式,它其實是乙個十分簡易的串列埠終端程式,類似於 linux 中的minicom 該程式與硬體無關,因此相同的 不僅適用於任何 ar...

嵌入式環境的搭建(用於Arm開發板)

1,搭建開發環境 a,對於新系統,安裝一些依賴軟體。sudo apt get install build essential git libusb 1.0 0 dev u boot tools ccache zlib1g dev uuid dev 如果是64位的linux還要執行如下的安裝 sudo...