- 积分
- 3
贡献14
飞刀0 FD
注册时间2015-2-27
在线时间0 小时
|
楼主 |
发表于 2015-2-27 17:50:28
|
显示全部楼层
- #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>
- main()
- {
- int fd;
- int i;
- int len;
- int n = 0;
- int m=0;
- unsigned char read_buf[256];
- unsigned char write_buf[256];
- struct termios opt;
- write_buf[0]=0xFF;
- fd = open("/dev/ttySAC2", O_RDWR | O_NOCTTY); //默认为阻塞读方式
- if(fd == -1)
- {
- perror("open serial 0\n");
- exit(0);
- }
- tcgetattr(fd, &opt);
- cfsetispeed(&opt, B9600);
- cfsetospeed(&opt, B9600);
- if(tcsetattr(fd, TCSANOW, &opt) != 0 )
- {
- perror("tcsetattr error");
- return -1;
- }
- opt.c_cflag &= ~CSIZE;
- opt.c_cflag |= CS8;
- opt.c_cflag &= ~CSTOPB;
- opt.c_cflag &= ~PARENB;
- //opt.c_cflag &= ~INPCK;
- opt.c_cflag |= (CLOCAL | CREAD);
- /*
- opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
- opt.c_oflag &= ~OPOST;
- opt.c_oflag &= ~(ONLCR | OCRNL); //添加的
- //ISTRIP
- opt.c_iflag &= ~(ICRNL | INLCR);
- opt.c_iflag &= ~(IXON | IXOFF | IXANY); //添加的
- */
-
- opt.c_cc[VTIME] = 0;
- opt.c_cc[VMIN] = 0;
- tcflush(fd, TCIOFLUSH);
- printf("configure complete\n");
- if(tcsetattr(fd, TCSANOW, &opt) != 0)
- {
- perror("serial error");
- return -1;
- }
- printf("start send and receive data\n");
- while(1)
- {
- n = 0;
- len = 0;
-
- m = write(fd, write_buf, 1);
- printf("write %02X chars\n",write_buf[0]);
- while( (n = read(fd, read_buf, 1)) > 0 )
- {
- printf("read len is %d \n ",n);
- for(i = len; i < (len + n); i++)
- {
- write_buf[i] = read_buf[i - len];
- }
- len += n;
- printf("recive char is :%02X \n", read_buf[0]);
- }
-
- sleep(2);
- }
- }
复制代码 将串口扩展板,的串口2 的 2 和3 短接,自发自收即
- [root@FORLINX6410]# ./acom
- configure complete
- start send and receive data
- write FF chars
- write FF chars
- read len is 1
- recive char is :7F
- write 7F chars
- read len is 1
- recive char is :7F
- ^C
复制代码 结果就是,每个字节的最高位,自动给过滤了
代码中,变量是 无符号的,,应该没啥问题了,,硬件啥有啥特别注意的?
还是其他地方有啥没注意到呢? |
|