?? uart_test.c
字號:
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#define FALSE -1
#define TRUE 0
int speed_arr[] = { B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, //
B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300,
115200, 38400, 19200, 9600, 4800, 2400, 1200, 300, };
void set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt; //定義一個結(jié)構(gòu)
tcgetattr(fd, &Opt);//用來得到機(jī)器原端口的默認(rèn)設(shè)置
for(i = 0; i < sizeof(speed_arr) / sizeof(int); i++)
{
if(speed == name_arr[i])//判斷傳進(jìn)來是否相等
{
tcflush(fd, TCIOFLUSH);//刷新輸入輸出緩沖
//cfsetispeed(&Opt, speed_arr);//這里分別設(shè)置
//cfsetospeed(&Opt, speed_arr);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);//立刻把波特率設(shè)置真正寫到串口中去
if(status != 0)
{
perror("tcsetattr fd1");//設(shè)置錯誤
}
return;
}
tcflush(fd, TCIOFLUSH);//得到機(jī)器設(shè)置
}
}
int set_Parity(int fd, int databits, int stopbits, int parity)
{
struct termios options;
if(tcgetattr(fd, &options) != 0)//首先讀取系統(tǒng)默認(rèn)設(shè)置,必須
{
perror("SetupSerial 1");
return(FALSE);
}
options.c_cflag &= ~CSIZE;//設(shè)置c_cflag選項(xiàng)不按位數(shù)據(jù)位掩碼
switch(databits)//數(shù)據(jù)位
{
case 7:
options.c_cflag |= CS7; //設(shè)置c_cflag選項(xiàng)數(shù)據(jù)位為7位
break;
case 8:
options.c_cflag |= CS8;//8 bits
break;
default:
fprintf(stderr, "Unsupported data size\n");
return(FALSE);
}
switch(parity)//設(shè)置奇偶校驗(yàn),c_cflag c_iflag有效
{
case 'n':
case 'N'://no parity
options.c_cflag &= ~PARENB; //clear parity enable
options.c_iflag &= ~INPCK; //enable parity checking
break;
case 'o':
case 'O': //奇校驗(yàn),INPCK檢查校驗(yàn)
options.c_cflag |= (PARODD | PARENB);//設(shè)置奇校驗(yàn)
options.c_iflag | INPCK; //disnable parity checking
break;
case 'e':
case 'E'://偶校驗(yàn)
options.c_cflag |= PARENB;//enable parity
options.c_cflag &= ~PARODD;//轉(zhuǎn)換為偶校驗(yàn)
options.c_iflag |= INPCK;//disnable parity checking
break;
default:
fprintf(stderr, "Unsupported parity\n");
return(FALSE);
}
switch(stopbits)//設(shè)置停止位,影響表示位是c_cflag
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr, "Unsupported stop bits\n");
return(FALSE);
}
if(parity != 'n')//設(shè)置輸入是否進(jìn)行校驗(yàn)
options.c_iflag |= INPCK;
options.c_cc[VTIME] = 150;//15 seconds
options.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH);//刷新和立刻寫進(jìn)去
if(tcsetattr(fd, TCSANOW, &options) != 0)
{
perror("SetupSerial 3");
return(FALSE);
}
return(TRUE);
}
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;
}
int main(int arge, char **argv)
{
int fd;
int nread;
char buff[512];
int loopi;
int loopj;
//char *dev = "/dev/ttyS0";
char *dev="/dev/ttySAC1";
fd = OpenDev(dev);
if(fd >= 0)
{
set_speed(fd, 115200);
}
else
{
printf("Can't Open Serial Port!\n");
exit(0);
}
if (set_Parity(fd,8,1,'N')== FALSE) //設(shè)置8,1,n 注意,這里和上面要和下位機(jī)相符才可能通信
{
printf("Set Parity Error\n");
exit(1);
}
while(1)
{
while((nread = read(fd, buff, 512)) > 0)
{
printf("\nLen %d\n", nread);
buff[nread + 1] = '\0';
printf("\n%s",buff);
for(loopi = 0; loopi < 1000; loopi++)
for(loopj = 0; loopj < 10000; loopj++);
write(fd,buff,nread);
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -