嵌入式爱好者

查看: 4084|回复: 1

6410 串口,没字节最高位自动给过滤了

[复制链接]

1

主题

2

帖子

3

积分

扫一扫,手机访问本帖
发表于 2015-2-27 17:49:24 | 显示全部楼层 |阅读模式
[code]

#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
回复

使用道具 举报

1

主题

2

帖子

3

积分

 楼主| 发表于 2015-2-27 17:50:28 | 显示全部楼层


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <termios.h>
  8. #include <errno.h>

  9. main()
  10. {
  11. int fd;
  12. int i;
  13. int len;
  14. int n = 0;
  15. int m=0;
  16. unsigned char read_buf[256];
  17. unsigned char write_buf[256];
  18. struct termios opt;

  19. write_buf[0]=0xFF;
  20. fd = open("/dev/ttySAC2", O_RDWR | O_NOCTTY); //默认为阻塞读方式
  21. if(fd == -1)
  22. {
  23. perror("open serial 0\n");
  24. exit(0);
  25. }

  26. tcgetattr(fd, &opt);
  27. cfsetispeed(&opt, B9600);
  28. cfsetospeed(&opt, B9600);

  29. if(tcsetattr(fd, TCSANOW, &opt) != 0 )
  30. {
  31. perror("tcsetattr error");
  32. return -1;
  33. }

  34. opt.c_cflag &= ~CSIZE;
  35. opt.c_cflag |= CS8;
  36. opt.c_cflag &= ~CSTOPB;
  37. opt.c_cflag &= ~PARENB;
  38. //opt.c_cflag &= ~INPCK;
  39. opt.c_cflag |= (CLOCAL | CREAD);
  40. /*
  41. opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  42. opt.c_oflag &= ~OPOST;
  43. opt.c_oflag &= ~(ONLCR | OCRNL); //添加的
  44. //ISTRIP
  45.         opt.c_iflag &= ~(ICRNL | INLCR);
  46. opt.c_iflag &= ~(IXON | IXOFF | IXANY); //添加的
  47. */

  48. opt.c_cc[VTIME] = 0;
  49. opt.c_cc[VMIN] = 0;

  50. tcflush(fd, TCIOFLUSH);

  51. printf("configure complete\n");

  52. if(tcsetattr(fd, TCSANOW, &opt) != 0)
  53. {
  54. perror("serial error");
  55. return -1;
  56. }
  57. printf("start send and receive data\n");

  58. while(1)
  59. {
  60. n = 0;
  61. len = 0;

  62. m = write(fd, write_buf, 1);
  63. printf("write %02X chars\n",write_buf[0]);

  64. while( (n = read(fd, read_buf, 1)) > 0 )
  65. {
  66.         printf("read len is %d \n ",n);
  67. for(i = len; i < (len + n); i++)
  68. {
  69. write_buf[i] = read_buf[i - len];
  70. }
  71. len += n;
  72. printf("recive char is :%02X \n", read_buf[0]);
  73. }

  74. sleep(2);
  75. }
  76. }
复制代码
将串口扩展板,的串口2 的 2 和3 短接,自发自收即

  1. [root@FORLINX6410]# ./acom
  2. configure complete
  3. start send and receive data
  4. write FF chars
  5. write FF chars
  6. read len is 1
  7. recive char is :7F
  8. write 7F chars
  9. read len is 1
  10. recive char is :7F
  11. ^C

复制代码
结果就是,每个字节的最高位,自动给过滤了

代码中,变量是  无符号的,,应该没啥问题了,,硬件啥有啥特别注意的?

还是其他地方有啥没注意到呢?
点评回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋| 飞凌嵌入式 ( 冀ICP备12004394号-1 )

GMT+8, 2024-12-21 01:50

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表