linux串口樣本

來源:互聯網
上載者:User

最近看了下linux的串口,發現還是蠻容易的

做一些總結和記錄

【這篇文章也重在備份和記錄,代碼都是套用別人的 ,所以基本只是羅列了些代碼,但保證代碼可用】

其實串口操作也就那麼幾步

1.開啟串口

2.設定參數

3.發送接收

4.按需關閉

而根據函式提供的形式,

一般設定參數分兩步進行【其實就是那麼配置下,分幾步都行,只是配合後面的代碼了】

[1]設定傳輸速率

[2]設定資料格式

下面還是羅列一些代碼

開啟串口

int OpenDev(char *Dev)

{

int fd = open( Dev, O_RDWR );

if (-1 == fd)

{

perror("Can't Open Serial Port");

return -1;

}

else

return fd;

}

O_RDWR就是可讀寫的意思設定傳輸速率

Code

int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,

B38400, B19200, B9600, B4800, B2400, B1200, B300, };

int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,

19200, 9600, 4800, 2400, 1200, 300, };

void set_speed(int fd, int speed){

int i;

int status;

struct termios Opt;

tcgetattr(fd, &Opt);

for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {

if (speed == name_arr[i]) {

tcflush(fd, TCIOFLUSH);

cfsetispeed(&Opt, speed_arr[i]);

cfsetospeed(&Opt, speed_arr[i]);

status = tcsetattr(fd, TCSANOW, &Opt);

if (status != 0) {

perror("tcsetattr fd1");

return;

}

tcflush(fd,TCIOFLUSH);

}

}

}

設定參數是用到了一個專用的結構體struct termios其實也沒啥說的,就是通過它去配置串口參數罷了

注意tcflush,他是清空buffer用的,關於buffer這東西,裡面其實挺饒,這裡不多說明,只是要注意它清空的buffer並不是printf那類函式中所謂的緩衝。

另外那兩個數組,其實可以弄的簡單些,只不過懶得改了

有點像畫刷的使用,舊的一般都愛儲存起來,最後還要還原。

設定資料格式

int set_Parity(int fd,int databits,int stopbits,int parity)

{

struct termios options;

if ( tcgetattr( fd,&options) != 0) {

perror("SetupSerial 0");

return -1;

}

options.c_cflag &= ~CSIZE;

switch (databits)

{

case 7:

options.c_cflag |= CS7;

break;

case 8:

options.c_cflag |= CS8;

break;

default:

fprintf(stderr,"Unsupported data size\n");

return -1;

}

switch (parity)

{

case 'n':

case 'N':

options.c_cflag &= ~PARENB;

options.c_iflag &= ~INPCK;

break;

case 'o':

case 'O':

options.c_cflag |= (PARODD | PARENB);

options.c_iflag |= INPCK;

break;

case 'e':

case 'E':

options.c_cflag |= PARENB;

options.c_cflag &= ~PARODD;

options.c_iflag |= INPCK;

break;

case 'S':

case 's':

options.c_cflag &= ~PARENB;

options.c_cflag &= ~CSTOPB;break;

default:

fprintf(stderr,"Unsupported parity\n");

return -1;

}

switch (stopbits)

{

case 1:

options.c_cflag &= ~CSTOPB;

break;

case 2:

options.c_cflag |= CSTOPB;

break;

default:

fprintf(stderr,"Unsupported stop bits\n");

return -1;

}

if (parity != 'n')

options.c_iflag |= INPCK;

tcflush(fd,TCIFLUSH);

options.c_cc[VTIME] = 150;

options.c_cc[VMIN] = 0;

if (tcsetattr(fd,TCSANOW,&options) != 0)

{

perror("SetupSerial 3");

return -1;

}

return 0;

}

和前面的函式不同這裡直接對struct termios進行操作,進而配置了資料位元長度,校正位,停止位,逾時等資訊

最後整合下,列出一個測試案例

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <termios.h>

#include <errno.h>

int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,

B38400, B19200, B9600, B4800, B2400, B1200, B300, };

int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,

19200, 9600, 4800, 2400, 1200, 300, };

void set_speed(int fd, int speed){

int i;

int status;

struct termios Opt;

tcgetattr(fd, &Opt);

for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {

if (speed == name_arr[i]) {

tcflush(fd, TCIOFLUSH);

cfsetispeed(&Opt, speed_arr[i]);

cfsetospeed(&Opt, speed_arr[i]);

status = tcsetattr(fd, TCSANOW, &Opt);

if (status != 0) {

perror("tcsetattr fdl");

return;

}

tcflush(fd,TCIOFLUSH);

}

}

}

int set_Parity(int fd,int databits,int stopbits,int parity)

{

struct termios options;

if ( tcgetattr( fd,&options) != 0) {

perror("SetupSerial 1");

return -1;

}

options.c_cflag &= ~CSIZE;

switch (databits)

{

case 7:

options.c_cflag |= CS7;

break;

case 8:

options.c_cflag |= CS8;

break;

default:

fprintf(stderr,"Unsupported data size\n"); return 0;

}

switch (parity)

{

case 'n':

case 'N':

options.c_cflag &= ~PARENB;

options.c_iflag &= ~INPCK;

break;

case 'o':

case 'O':

options.c_cflag |= (PARODD | PARENB);

options.c_iflag |= INPCK;

break;

case 'e':

case 'E':

options.c_cflag |= PARENB;

options.c_cflag &= ~PARODD;

options.c_iflag |= INPCK;

break;

case 'S':

case 's':

options.c_cflag &= ~PARENB;

options.c_cflag &= ~CSTOPB;break;

default:

fprintf(stderr,"Unsupported parity\n");

return -1;

}

switch (stopbits)

{

case 1:

options.c_cflag &= ~CSTOPB;

break;

case 2:

options.c_cflag |= CSTOPB;

break;

default:

fprintf(stderr,"Unsupported stop bits\n");

return -1;

}

if (parity != 'n')

options.c_iflag |= INPCK;

tcflush(fd,TCIFLUSH);

options.c_cc[VTIME] = 150;

options.c_cc[VMIN] = 0;

if (tcsetattr(fd,TCSANOW,&options) != 0)

{

perror("SetupSerial 3");

return -1;

}

return 0;

}

int OpenDev(char *Dev)

{

int fd = open( Dev, O_RDWR );

if (-1 == fd)

{

perror("Can't Open Serial Port");

return -1;

}

else

return fd;

}

int main(int argc, char **argv){

int fd;

int nread;

char buff[6];

char *dev = "/dev/ttyS0";

fd = OpenDev(dev);

set_speed(fd,9600);

if (set_Parity(fd,8,1,'N') == FALSE) {

printf("Set Parity Error\n");

exit (0);

}

while (1) {

write(fd,"hello",5);

nread = read(fd, buff, 5);

tcflush(fd,TCIOFLUSH);

buff[nread+1] = '\0';

printf( "%s", buff);

}

//close(fd);

// exit (0);

}

這個測試將發送hello字元並接受,主要是為了測試自發自收用的如果有串口的話,可以使用跳線帽將2,3腳端接來完成此測試可以看到終端中不斷出現hellohello……就對了!我的是現代化的筆記本。。。。。XD怎會有串口的存在所以整了張卡,據說比usb轉接線穩定,目前還沒發現穩定到哪裡去

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.