亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ubuntu串口怎么測試

發布時間:2022-11-21 16:35:01 來源:億速云 閱讀:212 作者:iii 欄目:服務器

本篇內容主要講解“ubuntu串口怎么測試”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“ubuntu串口怎么測試”吧!

測試新機器的串口硬件的是否正常,可借用以下程序測試串口是否正常運行,代碼如下:

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

#include

#include/*Unix標準函數定義*/

#include/**/

#include/**/

#include/*文件控制定義*/

#include/*PPSIX終端控制定義*/

#include/*錯誤號定義*/

#include

#include


#define FALSE 1

#define TRUE 0


char *recchr="We received:\"";


void print_usage();


int speed_arr[] = {

B921600, B460800, B230400, B115200, B57600, B38400, B19200,

B9600, B4800, B2400, B1200, B300,

};


int name_arr[] = {

921600, 460800, 230400, 115200, 57600, 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);

}


if (i == 12){

printf("\tSorry, please set the correct baud rate!\n\n");

print_usage(stderr, 1);

}

}

/*

*@brief 設置串口數據位,停止位和效驗位

*@param fd 類型 int 打開的串口文件句柄*

*@param databits 類型 int 數據位 取值 為 7 或者8*

*@param stopbits 類型 int 停止位 取值為 1 或者2*

*@param parity 類型 int 效驗類型 取值為N,E,O,,S

*/

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

{

struct termios options;

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

perror("SetupSerial 1");

return(FALSE);

}

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 (FALSE);

}


switch (parity) {

case 'n':

case 'N':

options.c_cflag &= ~PARENB; /* Clear parity enable */

options.c_iflag &= ~INPCK; /* Enable parity checking */

break;

case 'o':

case 'O':

options.c_cflag |= (PARODD | PARENB); /* 設置為奇效驗*/

options.c_iflag |= INPCK; /* Disnable parity checking */

break;

case 'e':

case 'E':

options.c_cflag |= PARENB; /* Enable parity */

options.c_cflag &= ~PARODD; /* 轉換為偶效驗*/

options.c_iflag |= INPCK; /* Disnable parity checking */

break;

case 'S':

case 's': /*as no parity*/

options.c_cflag &= ~PARENB;

options.c_cflag &= ~CSTOPB;

break;

default:

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

return (FALSE);

}

/* 設置停止位*/

switch (stopbits) {

case 1:

options.c_cflag &= ~CSTOPB;

break;

case 2:

options.c_cflag |= CSTOPB;

break;

default:

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

return (FALSE);

}

/* Set input parity option */

if (parity != 'n')

options.c_iflag |= INPCK;

options.c_cc[VTIME] = 150; // 15 seconds

options.c_cc[VMIN] = 0;


options.c_lflag &= ~(ECHO | ICANON);


tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */

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

perror("SetupSerial 3");

return (FALSE);

}

return (TRUE);

}


/**

*@breif 打開串口

*/

int OpenDev(char *Dev)

{

int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY

if (-1 == fd) { /*設置數據位數*/

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

return -1;

} else

return fd;

}



/* The name of this program */

const char * program_name;


/* Prints usage information for this program to STREAM (typically

* stdout or stderr), and exit the program with EXIT_CODE. Does not

* return.

*/


void print_usage (FILE *stream, int exit_code)

{

fprintf(stream, "Usage: %s option [ dev... ] \n", program_name);

fprintf(stream,

"\t-h --help Display this usage information.\n"

"\t-d --device The device ttyS[0-3] or ttySCMA[0-1]\n"

"\t-b --baudrate Set the baud rate you can select\n"

"\t [230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300]\n"

"\t-s --string Write the device data\n");

exit(exit_code);

}




/*

*@breif main()

*/

int main(int argc, char *argv[])

{

int fd, next_option, havearg = 0;

char *device;

int i=0,j=0;

int nread; /* Read the counts of data */

char buff[512]; /* Recvice data buffer */

pid_t pid;

char *xmit = "1234567890"; /* Default send data */

int speed ;

const char *const short_options = "hd:s:b:";


const struct option long_options[] = {

{ "help", 0, NULL, 'h'},

{ "device", 1, NULL, 'd'},

{ "string", 1, NULL, 's'},

{ "baudrate", 1, NULL, 'b'},

{ NULL, 0, NULL, 0 }

};


program_name = argv[0];


do {

next_option = getopt_long (argc, argv, short_options, long_options, NULL);

switch (next_option) {

case 'h':

print_usage (stdout, 0);

case 'd':

device = optarg;

havearg = 1;

break;

case 'b':

speed = atoi(optarg);

break;


case 's':

xmit = optarg;

havearg = 1;

break;

case -1:

if (havearg) break;

case '?':

print_usage (stderr, 1);

default:

abort ();

}

}while(next_option != -1);


sleep(1);

fd = OpenDev(device);


if (fd > 0) {

set_speed(fd, speed);

} else {

fprintf(stderr, "Error opening %s: %s\n", device, strerror(errno));

exit(1);

}


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

fprintf(stderr, "Set Parity Error\n");

close(fd);

exit(1);

}


pid = fork();


if (pid < 0) {

fprintf(stderr, "Error in fork!\n");

} else if (pid == 0){

while(1) {

printf("%s SEND: %s id %d\n",device, xmit,i);

write(fd, xmit, strlen(xmit));

sleep(1);

i++;

}

exit(0);

} else {

while(1) {

nread = read(fd, buff, sizeof(buff));

if (nread > 0) {

buff[nread] = '\0';

printf("%s RECV %d total\n", device, nread);

printf("%s RECV: %s\n", device, buff);

}

}

}

close(fd);

exit(0);

}


使用gcc命令進行編譯,然后執行生成的可執行文件

列入:./serialtest -d /dev/ttyS* -b 115200

到此,相信大家對“ubuntu串口怎么測試”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

麦盖提县| 讷河市| 竹山县| 舟山市| 巴林右旗| 乌审旗| 谢通门县| 丘北县| 乐清市| 蓬安县| 巢湖市| 华坪县| 应用必备| 湖南省| 岐山县| 宣武区| 交口县| 台前县| 资溪县| 连平县| 得荣县| 边坝县| 营山县| 白玉县| 绵阳市| 邵东县| 汝州市| 利辛县| 屏东县| 平武县| 宁晋县| 三原县| 瑞金市| 呼伦贝尔市| 仁寿县| 新昌县| 陇南市| 湘潭市| 宾川县| 巴彦淖尔市| 平塘县|