嵌入式爱好者

查看: 8735|回复: 1

[Linux] Linux 串口通信问题(急!!!)

[复制链接]
zxynk666666 该用户已被删除
发表于 2014-9-19 13:19:57 | 显示全部楼层 |阅读模式
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

0

主题

1

帖子

2

积分

发表于 2014-9-24 18:23:54 | 显示全部楼层
随便找了个程序试了一下 好像接受到0x13不会挂起。

图上不了 就不传了
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <errno.h>
  7. #include <termios.h>
  8. #include <sys/stat.h>
  9. int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop)
  10. {
  11.                 struct termios newtio, oldtio;
  12.                 /*保存测试现有串口参数设置,在这里如果串口号等出错,会有相关的出错信息*/
  13.                 if(tcgetattr(fd, &oldtio) != 0)
  14.                 {
  15.                                 perror("SetupSerial 1");
  16.                                 return -1;
  17.                 }
  18.                 bzero(&newtio, sizeof(newtio));
  19.                 /*一 设置字符大小*/
  20.                 newtio.c_cflag |= CLOCAL | CREAD;
  21. //                newtio.c_cflag &= ~CSIZE;
  22.                 newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  23.                 newtio.c_oflag &= ~OPOST;
  24.                 newtio.c_iflag &= ~IXON;
  25.                 newtio.c_iflag |= IXANY;
  26.                 /*设置数据位 */
  27.                 switch(nBits)
  28.                 {
  29.                                 case 7:
  30.                                                 newtio.c_cflag |= CS7;
  31.                                                 break;
  32.                                 case 8:
  33.                                                 newtio.c_cflag |= CS8;
  34.                                                 break;
  35.                 }
  36.                 /*设置奇偶校验位*/
  37.                 switch(nEvent)
  38.                 {
  39.                                 case 'O'://奇数
  40.                                                 newtio.c_cflag |= PARENB ;
  41.                                                 newtio.c_cflag |= PARODD ;
  42.                                                 newtio.c_iflag |= (INPCK | ISTRIP);
  43.                                                 break;
  44.                                 case 'E'://偶数
  45.                                                 newtio.c_cflag |= PARENB ;
  46.                                                 newtio.c_cflag &= ~PARODD ;
  47.                                                 newtio.c_iflag |= (INPCK | ISTRIP);
  48.                                                 break;
  49.                                 case 'N':// 无奇偶校验位
  50.                                                 newtio.c_cflag &= ~PARENB ;
  51.                                                 break;
  52.                 }
  53.                 /*设置波特率*/
  54.                 switch(nSpeed)
  55.                 {
  56.                                 case 4800:
  57.                                                 cfsetispeed(&newtio, B4800);
  58.                                                 cfsetospeed(&newtio, B4800);
  59.                                                 break;
  60.                                 case 9600:
  61.                                                 cfsetispeed(&newtio, B9600);
  62.                                                 cfsetospeed(&newtio, B9600);
  63.                                                 break;
  64.                                 case 19200:
  65.                                                 cfsetispeed(&newtio, B19200);
  66.                                                 cfsetospeed(&newtio, B19200);
  67.                                                 break;
  68.                                 case 57600:
  69.                                                 cfsetispeed(&newtio, B57600);
  70.                                                 cfsetospeed(&newtio, B57600);
  71.                                                 break;
  72.                                 case 115200:
  73.                                                 cfsetispeed(&newtio, B115200);
  74.                                                 cfsetospeed(&newtio, B115200);
  75.                                                 break;
  76.                                 default:
  77.                                                 cfsetispeed(&newtio, B115200);
  78.                                                 cfsetospeed(&newtio, B115200);
  79.                                                 break;
  80.                 }
  81.                 /*设置停止位 */
  82.                 if(1 == nStop)
  83.                                 newtio.c_cflag &= ~CSTOPB;
  84.                 else if(2 == nStop)
  85.                                 newtio.c_cflag |= CSTOPB;
  86.                 /*设置等待时间和最小接收字符 */
  87.                 newtio.c_cc[VTIME] = 1;
  88.                 newtio.c_cc[VMIN] = 255;
  89.                 /*处理未接收字符*/
  90.                 tcflush(fd, TCIFLUSH);
  91.                 /*激活新配置*/
  92.                 if((tcsetattr(fd, TCSANOW, &newtio))!=0)
  93.                 {
  94.                                 perror("com set error!");
  95.                                 return -1;
  96.                 }
  97.                 printf("Set done!\n");
  98.                 return 0;
  99. }
  100. int open_port(int comport)
  101. {
  102.                 int fd = -1;
  103.                 switch(comport)
  104.                 {
  105.                                 case 0:
  106.                                                 {
  107.                                                                 fd = open("/dev/ttyO0", O_RDWR|O_NOCTTY|O_NDELAY);
  108.                                                                 if(-1 == fd)
  109.                                                                 {
  110.                                                                                 perror("Can't open Serial Port");
  111.                                                                                 return -1;
  112.                                                                 }
  113.                                                 }
  114.                                                 break;
  115.                                 case 1:
  116.                                                 {
  117.                                                                 fd = open("/dev/ttyO1", O_RDWR|O_NOCTTY|O_NDELAY);
  118.                                                                 if(-1 == fd)
  119.                                                                 {
  120.                                                                                 perror("Can't open Serial Port");
  121.                                                                                 return -1;
  122.                                                                 }
  123.                                                 }
  124.                                                 break;
  125.                                 case 2:
  126.                                                 {
  127.                                                                 fd = open("/dev/ttyO2", O_RDWR|O_NOCTTY|O_NDELAY);
  128.                                                                 if(-1 == fd)
  129.                                                                 {
  130.                                                                                 perror("Can't open Serial Port");
  131.                                                                                 return -1;
  132.                                                                 }
  133.                                                 }
  134.                                                 break;
  135.                                 case 3:
  136.                                                 {
  137.                                                                 fd = open("/dev/ttyO3", O_RDWR|O_NOCTTY|O_NDELAY);
  138.                                                                 if(-1 == fd)
  139.                                                                 {
  140.                                                                                 perror("Can't open Serial Port");
  141.                                                                                 return -1;
  142.                                                                 }
  143.                                                 }
  144.                                                 break;

  145.                 }
  146.                 /*恢复串口为阻塞状态*/
  147.                 if(fcntl(fd, F_SETFL, 0)<0)
  148.                                 printf("fcntl failed!\n");
  149.                 else
  150.                                 printf("fcntl=%d\n", fcntl(fd,F_SETFL,0));
  151.                 /*测试是否为终端设备*/
  152.                 if(isatty(STDIN_FILENO) == 0)
  153.                                 printf("standard input is not a terminal device \n");
  154.                 else
  155.                                 printf("isatty success!\n");
  156.                 printf("fd-open=%d\n",fd);
  157.                 return fd;
  158. }

  159. int main(int argc,char *argv[])
  160. {
  161.                 int fd,fdcom;
  162.                 int nread,maxd,nwrite,m,zread,zwrite,tmp,temp,i,j,fs_sel;
  163.                 fd_set fs_read;
  164.                 char buff[20],buffer[20];// = "comchecks";

  165.                 fdcom = open_port(atoi(argv[1]));
  166.                 if(fdcom<0)
  167.                 {
  168.                                 perror("Open_prot 2  error");
  169.                                 return;
  170.                 }
  171.                 //printf("com handle: %d \n", fdcom);
  172.                 if(argv[1]!=NULL)
  173.                 {      
  174.                                 fd = open_port(atoi(argv[1]));
  175.                                 if(fd<0)
  176.                                 {
  177.                                                 perror("Open_prot error");
  178.                                                 return;
  179.                                 }
  180.                 }
  181.                 if(argv[2]!=NULL)
  182.                 {
  183.                                 temp=set_opt(fdcom,atoi(argv[2]), 8, 'N', 1);
  184.                                 if(temp<0)
  185.                                 {
  186.                                                 perror("Set_opt ttyO2 error!");
  187.                                                 return;
  188.                                 }
  189.                                 tmp=set_opt(fd,atoi(argv[2]), 8, 'N', 1);
  190.                                 if(tmp<0)
  191.                                 {
  192.                                                 perror("Set_opt error!");
  193.                                                 return;
  194.                                 }
  195.                 }
  196.                 else
  197.                 {
  198.                                 temp=set_opt(fdcom,115200,8,'N', 1);
  199.                                 if(temp<0)
  200.                                 {
  201.                                                 perror("Set_opt ttyO2 error!");
  202.                                                 return;
  203.                                 }
  204.                                 tmp=set_opt(fd,115200,8,'N', 1);
  205.                                 if(tmp<0)
  206.                                 {
  207.                                                 perror("Set_opt error!");
  208.                                                 return;
  209.                                 }
  210.                 }

  211.                 for(j=0;j<20;)
  212.                 {        
  213.                                 nwrite = write(fdcom,"fluarttest",10);//buff,strlen(buff));
  214.                                 printf("j=%d, send is %d fluarttest\n",j,nwrite);
  215.                                 lseek(fdcom,0,SEEK_SET);
  216.                                                 zread=read(fdcom,buffer,10);
  217.                                                 if(zread<0)
  218.                                                 {
  219.                                                                 printf("zread error!\n");
  220.                                                 }
  221.                                                 else
  222.                                                 {
  223.                                                                 char tmp[64];
  224.                                                                 sprintf(tmp,"0x%x",(unsigned int)(buffer[0]));
  225.                                                                 printf("tmp %s\n",tmp);
  226.                                                 }
  227.                                
  228.                 }      
  229.                 close(fd);
  230.                 close(fdcom);
  231.                 return;
  232. }
复制代码
点评回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-20 09:08

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

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