全志H616——通过改变串口demo来收发数据

该代码示例是一个C程序,它打开了与/dev/ttyS5设备的串行连接,以115200波特率进行通信。程序创建了一个线程`Sendhandler`用于发送数据,主程序则负责接收数据。`Sendhandler`通过用户输入填充缓冲区并逐字节通过串口发送,而主程序则持续检查串口是否有可用数据并打印出来。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码:

  1 /*
  2    serialTest.c:
  3    */
  4
  5 #include <stdio.h>
  6 #include <string.h>
  7 #include <errno.h>
  8 #include <pthread.h>
  9 #include <wiringPi.h>
 10 #include <wiringSerial.h>
 11 #include <stdlib.h>
 12
 13
 14 int fd;
 15
 16 void* Sendhandler()
 17 {
 18     char *sendbuf;
 19     sendbuf = (char *)malloc(32*sizeof(32));
 20
 21     while(1){
 22
 23     memset(sendbuf,'\0',32);
 24     scanf("%s",sendbuf);
 25
 26     while(*sendbuf){
 27
 28         serialPutchar(fd,*sendbuf++);
 29
 30     }
 31
 32
 33     }
 34
 35
 36
 37 }
 38
 39
 40 int main ()
 41 {
 42     int count ;
 43     unsigned int nextTime ;
 44
 45     pthread_t idsend;
 46     if ((fd = serialOpen ("/dev/ttyS5", 115200)) < 0)
 47     {
 48         fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
 49         return 1 ;
 50     }
 51
 52     pthread_create(&idsend,NULL,Sendhandler,NULL);
 53
 54     if (wiringPiSetup () == -1)
 55     {
 56         fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
 57         return 1 ;
 58     }
 59
 60     while(1){
 61     while (serialDataAvail (fd))
 62     {
 63         printf ("%c", serialGetchar(fd)) ;
 64         fflush (stdout) ;
 65     }
 66 }
 67
 68 printf ("\n") ;
 69 return 0 ;
 70 }
~

代码逻辑:

打开serial->新建一个线程用来发送数据(主程序用来收数据)

运行结果展示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值