Linux串列埠除錯

2021-05-22 22:50:10 字數 4289 閱讀 7263

在第一次除錯linux串列埠驅動的時候,一定要保證與linux串列埠通訊的器件是沒有問題可以使用的,然後我們再進行串列埠操作的學習,否則也許可能碰到問 題的時候不知如何處理了。

好了,在保證硬體已經沒有問題的情況下,我們開始學習串列埠驅動模組的使用。

pc上的串列埠不比嵌入式,你可以在了解了暫存器之後操作暫存器就可以實現串列埠的功能了。

pc都是基於作業系統的,作業系統在使用者模式是不能隨便操作硬體,只有使用者將核心模組載入,讓核心模組去操作硬體,在linux下就是使用這樣的方 式操作模組的。

在linux下目錄/dev/ 下就是一些硬體的驅動模組,對應串列埠的是名稱為ttys(數字)的模組,比如ttys0就是串列埠1(com1),ttys1就是串列埠2,以此類推。

linux下對硬體的操作就像是對檔案的操作一樣,唯一的區別就是檔案不是流操作,可以在檔案內部來回移動,串列埠以及其他硬體都是流操作,讀取過了 的資料是沒有辦法恢復的。

在了解了這些之後,我們開始學習串列埠的訪問函式。

首先我們從頭檔案開始:

#include //標準輸入輸出定義

#include //字串功能定義

#include //unix標準函式定義

#include //檔案控制定義

#include //錯誤碼定義

#include //posix終端控制定義

#include //exit函式

#include //時間函式

#include //pid_t 多執行緒

#include //ioctl

以上就是我們串列埠除錯所用到的所有標頭檔案,當然很多標頭檔案應該用不上,但是我們一次性包含進來,用的時候就不用那樣的找了。

下面是串列埠的開啟操作:

串列埠開啟用 open 函式,具體為:

fd = open("/dev/ttys0", o_rdwr | o_noctty | o_ndelay);

這個函式返回-1就表示開啟錯誤,有可能是被占用。後門的標誌位o_rdwr 表示讀寫,o_noctty表示改程式不會成為此程式的控制終端,與o_ndelay,表示不等的dcd訊號,一直處於等待資料接收狀態。

在開啟成功後,在最後新增函式 fcntl(fd, f_setfl, 0);表示執行read命令的時候,如果沒有資料會阻塞程序。

串列埠關閉操作,

串列埠關閉使用close函式,

close(fd); 即可關閉,沒有返回值。

串列埠寫資料使用write函式

write(fd,w_buf,n)

串列埠讀操作使用read函式

len=read(fd,w_buf,n)

如果串列埠設定fcntl(fd,f_setfl,fndelay);

read函式將不會阻塞,如果沒有資料將會直接返回0

在我們知道如何操作串列埠之後,我們只能操作乙個適合我們使用的串列埠,

串列埠屬性的設定就是我們首先要解決的問題了。

1 波特率

2400        b2400

4800        b4800

9600        b9600

19200      b19200

38400      b38400

57600       b57600

115200    b115200

其中9600是預設波特率,用的比較多,arm晶元使用的波特率比較高,是115200。

設定波特率使用函式cfsetispped和cfsetospeed設定輸入波特率和輸出波特率,一般情況下我們使用的輸入和輸出都是同乙個波特 率。

2 設定控制模式

設定流控制:

不使用流控制:termios_new.c_cflag &= ~crtscts

使用硬體流控:termios_new.c_cflag |= crtscts

使用軟體流控:termios_new.c_iflag |= ixon | ixoff | ixany

設定字元大小:

termios_new.c_cflag &= ~csize;

termios_new.c_cflag |= cs8;

termios_new.c_cflag |= cs7;

termios_new.c_cflag |= cs6;

termios_new.c_cflag |= c5;

設定奇偶校驗:

無校驗: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;

以上就是串列埠通訊的一些基礎知識。

下面開始看程式:

mycom.h

typedef struct

portinfo_t;

typedef portinfo_t *pportinfo_t;

int portopen(pportinfo_t pportinfo);

int portset(int fdcom,const pportinfo_t pportinfo);

void portclose(int fdcom);

int portsend(int fdcom, char *data, int datalen);

int portrecv(int fdcom, char *data, int datalen, int baudrate);

mycom.c

/*mycom.c

*/#include

#include

#include

#include

#include

#include

#include

#include

#include

#include "mycom.h"

#define tty_dev "/dev/ttys"

#define timeout_sec(buflen,baud) (buflen*20/baud+2)

#define timeout_usec 0

char *get_ptty(pportinfo_t pportinfo)

break;

case '1':

break;

case '2':

break;

}return ptty;

}int convbaud(unsigned long int baudrate)

}int portset(int fdcom, const pportinfo_t pportinfo)

termios_new.c_cflag &= ~csize;

databit = pportinfo->databit;

switch(databit)

parity = pportinfo->parity;

switch(parity)

stopbit = pportinfo->stopbit;

if(stopbit == '2')

else

termios_new.c_oflag &= ~opost;

termios_new.c_cc[vmin] = 1;

termios_new.c_cc[vtime] = 1;

tcflush(fdcom, tciflush);

tmp = tcsetattr(fdcom, tcsanow, &termios_new);

tcgetattr(fdcom, &termios_old);

return tmp;

}int portopen(pportinfo_t pportinfo)

void portclose(int fdcom)

int portsend(int fdcom, char *data, int datalen)

else

}int portrecv(int fdcom, char *data, int datalen, int baudrate)

else

}主函式:

int main(int argc, char *argv)

; if(argc != 2)

fdcom = portopen(&portinfo);

if(fdcom<0)

portset(fdcom, &portinfo);

if(atoi(argv[1])==0)

else

sleep(1);

}portclose(fdcom);

}else

linux串列埠除錯

在第一次除錯linux串列埠驅動的時候,一定要保證與linux串列埠通訊的器件是沒有問題可以使用的,然後我們再進行串列埠操作的學習,否則也許可能碰到問題的時候不知如何處理了。好了,在保證硬體已經沒有問題的情況下,我們開始學習串列埠驅動模組的使用。pc上的串列埠不比嵌入式,你可以在了解了暫存器之後操作...

linux下串列埠除錯工具

之前公司訂購了幾台裝置,剛開始接觸串列埠的時候,對串列埠了解太少,以下是在使用過程中用到的串列埠除錯命令和方法 1.除錯串列埠首先要用串列埠命令測試串列埠指示燈亮不亮,在linux下使用的命令如下 stty f dev ttys0 a 說明哈 dev ttys0 是你要除錯的那個串列埠的裝置名稱有很...

linux串列埠命令列除錯

測試傳送 echo 123456 dev ttys1 測試接收 busybox microcom s 115200 dev ttys1 引數 s 波特率 t 超時退出時間 不加 t就是一直等待接收 例如 1.將arm機器的串列埠1接電腦 電腦開個串列埠工具設定波特率115200 2.busybox ...