免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5835 | 回复: 9
打印 上一主题 下一主题

[C] 请教!c实现串口输出 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-10-11 15:45 |只看该作者 |倒序浏览
请教一个串口输出的例子。
例如:在串口显示hello world!

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
2 [报告]
发表于 2010-10-11 16:13 |只看该作者
串口又不是显示设备

论坛徽章:
0
3 [报告]
发表于 2010-10-11 16:24 |只看该作者
回复 2# hellioncu


    就是想通过串口显示信息。

论坛徽章:
0
4 [报告]
发表于 2010-10-11 16:28 |只看该作者
设置属性  读写。

论坛徽章:
0
5 [报告]
发表于 2010-10-11 16:58 |只看该作者
以搞定,echo “test” > /dev/console

论坛徽章:
1
黑曼巴
日期:2020-02-27 22:54:26
6 [报告]
发表于 2010-10-11 17:24 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
7 [报告]
发表于 2010-10-12 00:59 |只看该作者
open
set_speed
set_parity
write
c/unix 发表于 2010-10-11 17:24



    正解(条件是linux而非windows).

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
8 [报告]
发表于 2010-10-12 11:44 |只看该作者
6 、7 楼就是俺想要说的。

论坛徽章:
0
9 [报告]
发表于 2010-10-12 15:38 |只看该作者
  1. #include     <stdio.h>      
  2. #include     <stdlib.h>     
  3. #include     <unistd.h>     
  4. #include     <sys/types.h>  
  5. #include     <sys/socket.h>
  6. #include     <netinet/in.h>
  7. #include     <arpa/inet.h>
  8. #include     <sys/stat.h>   
  9. #include     <fcntl.h>      
  10. #include     <termios.h>   
  11. #include     <errno.h>     
  12. #include     <sys/select.h>
  13. #include     <time.h>
  14. #include     <sys/time.h>
  15. #define FALSE  -1
  16. #define TRUE   0
  17. #define NET_PORT 19988
  18. int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
  19.   B38400, B19200, B9600, B4800, B2400, B1200, B300, };
  20. int name_arr[] = {38400,  19200,  9600,  4800,  2400,  1200,  300, 38400,  
  21.    19200,  9600, 4800, 2400, 1200,  300, };
  22. void set_speed(int fd, int speed)
  23. {
  24.         int   i;
  25.         int   status;
  26.         struct termios   Opt;
  27.         tcgetattr(fd, &Opt);
  28.         for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++) {
  29.                 if  (speed == name_arr[i]) {     
  30.                         tcflush(fd, TCIOFLUSH);     
  31.                         cfsetispeed(&Opt, speed_arr[i]);  
  32.                         cfsetospeed(&Opt, speed_arr[i]);   
  33.                         status = tcsetattr(fd, TCSANOW, &Opt);  
  34.                         if  (status != 0) {        
  35.                                 perror("tcsetattr fd1");  
  36.                                 return;     
  37.                         }   
  38.                         tcflush(fd,TCIOFLUSH);   
  39.                 }  
  40.         }
  41. }
  42. int set_parity(int fd,int databits,int stopbits,int parity)
  43. {
  44. struct termios options;
  45. if  ( tcgetattr( fd,&options)  !=  0) {
  46.   perror("SetupSerial 1");     
  47.   return(FALSE);  
  48. }
  49. options.c_cflag &= ~CSIZE;
  50. switch (databits) /*设置数据位数*/
  51. {   
  52. case 7:  
  53.   options.c_cflag |= CS7;
  54.   break;
  55. case 8:     
  56.   options.c_cflag |= CS8;
  57.   break;   
  58. default:   
  59.   fprintf(stderr,"Unsupported data size\n");
  60.   return (FALSE);  
  61. }
  62. switch (parity)
  63. {   
  64.   case 'n':
  65.   case 'N':   
  66.    options.c_cflag &= ~PARENB;   /* Clear parity enable */
  67.    options.c_iflag &= ~INPCK;     /* Enable parity checking */
  68.    break;  
  69.   case 'o':   
  70.   case 'O':     
  71.    options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/  
  72.    options.c_iflag |= INPCK;             /* Disnable parity checking */
  73.    break;  
  74.   case 'e':  
  75.   case 'E':   
  76.    options.c_cflag |= PARENB;     /* Enable parity */   
  77.    options.c_cflag &= ~PARODD;    /* 转换为偶效验*/     
  78.    options.c_iflag |= INPCK;      /* Disnable parity checking */
  79.    break;
  80.   case 'S':
  81.   case 's':  /*as no parity*/   
  82.    options.c_cflag &= ~PARENB;
  83.    options.c_cflag &= ~CSTOPB;
  84.    break;  
  85.   default:   
  86.    fprintf(stderr,"Unsupported parity\n");   
  87.    return (FALSE);  
  88. }  
  89. /* 设置停止位*/  
  90. switch (stopbits)
  91. {   
  92.   case 1:   
  93.    options.c_cflag &= ~CSTOPB;  
  94.    break;  
  95.   case 2:   
  96.    options.c_cflag |= CSTOPB;  
  97.    break;
  98.   default:   
  99.     fprintf(stderr,"Unsupported stop bits\n");  
  100.     return (FALSE);
  101. }
  102. /* Set input parity option */
  103. if (parity != 'n' && parity != 'N')   
  104.   options.c_iflag |= INPCK;
  105. tcflush(fd,TCIFLUSH);
  106. options.c_cc[VTIME] = 30;  /* 设置超时3 seconds*/   
  107. options.c_cc[VMIN] = 0;  /* Update the options and do it NOW */
  108. /**
  109. *如果不是开发终端之类的,只是串口传输数据,
  110. *而不需要串口来处理,那么使用原始模式(Raw Mode)方式来通讯
  111. **/
  112. options.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON | IXOFF | IXANY);
  113. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG |IEXTEN);
  114. options.c_oflag &= ~OPOST;

  115. if (tcsetattr(fd,TCSANOW,&options) != 0)   
  116. {
  117.   perror("SetupSerial 3");   
  118.   return (FALSE);  
  119. }
  120. return (TRUE);  
  121. }
  122. int main()
  123. {
  124.         int fd;
  125.         fd = open("/dev/console", O_RDWR);
  126.         set_speed(fd,9600);
  127.         set_parity(fd,8,1,'N');
  128.         write(fd, "test", 4);
  129.         close(fd);
  130. }
复制代码

论坛徽章:
15
射手座
日期:2014-11-29 19:22:4915-16赛季CBA联赛之青岛
日期:2017-11-17 13:20:09黑曼巴
日期:2017-07-13 19:13:4715-16赛季CBA联赛之四川
日期:2017-02-07 21:08:572015年亚冠纪念徽章
日期:2015-11-06 12:31:58每日论坛发贴之星
日期:2015-08-04 06:20:00程序设计版块每日发帖之星
日期:2015-08-04 06:20:00程序设计版块每日发帖之星
日期:2015-07-12 22:20:002015亚冠之浦和红钻
日期:2015-07-08 10:10:132015亚冠之大阪钢巴
日期:2015-06-29 11:21:122015亚冠之广州恒大
日期:2015-05-22 21:55:412015年亚洲杯之伊朗
日期:2015-04-10 16:28:25
10 [报告]
发表于 2010-10-13 14:06 |只看该作者
请教一个串口输出的例子。
例如:在串口显示hello world!
angele25eeast 发表于 2010-10-11 15:45


最古典的用法,现代人居然全忘了。在网络时代之前都是这么用的。
1.在串口接一个终端,打开串口login(用getty)。用终端执行命令。
2.打开串口文件“/dev/tty??”,fprintf到这个文件。可以用ioctl调整IO属性。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP