ubuntu如何测试串口
ubuntu测试串口的方法:
测试新机器的串口硬件的是否正常,可借用以下程序测试串口是否正常运行,代码如下:
#include #include #include #include #include #include #include #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);
}
}
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;
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 (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);
}
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);
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;
}
const char * program_name;
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);
}
int main(int argc, char *argv[])
{
int fd, next_option, havearg = 0;
char *device;
int i=0,j=0;
int nread;
char buff[512];
pid_t pid;
char *xmit = "1234567890";
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
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341