一、基于wiringPi的串口开发
1、首先查看/dev下是否存在uart5设备节点,Orangepi Zero2 全志H616可用串口协议为ttyS5,用作信息交互串口,(注意:/dev/ttysS0为调试串口,本人踩过坑,接错会出现把Linux系统映射到串口的类似现象,可自行配置使用)
2、接线(将ttl转usb接入香橙派开发板,这里可以通过gpio readall 查看引脚图 ,接的是下图8引脚和10引脚串口组)
GND-GND
TXD-RXD//ttl转usb的TXD与开发板RXD相接
RXD-TXD//ttl转usb的RXD与开发板TXD相接
3、代码实现
(源码可参考wiringPi库源码 路径:/home/orangepi/wiringOP/examples/serialTest.c)
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdlib.h>
#include <unistd.h>
int fd ;
void* Sendhandler()//发送
{
char *sendBuf;
sendBuf =(char *)malloc(32*sizeof(32));
while(1){
memset(sendBuf,'\0',32);
scanf("%s",sendBuf);
while(*sendBuf){
serialPutchar (fd, *sendBuf++) ;
}
}
}
void* Revhandler()//接收
{
while(1){
while (serialDataAvail (fd))
{
printf("%c", serialGetchar (fd)) ;
fflush (stdout) ;
}
}
}
int main ()
{
int count ;
unsigned int nextTime ;
pthread_t idSend;
pthread_t idRev;
if ((fd = serialOpen ("/dev/ttyS5",115200)) < 0)
{
fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
return 1 ;
}
//创建两个线程处理发送和接收
pthread_create(&idSend,NULL,Sendhandler,NULL);
pthread_create(&idRev,NULL,Revhandler,NULL);
if (wiringPiSetup () == -1)
{
fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
return 1 ;
}
while(1){sleep(1);}
printf("\n") ;
return 0 ;
}
4.编译实现
二、自实现串口通信(基于sourceinsight软件开发)
对wiringPi库底层源码进行优化进行自实现串口通信
1、uartTool.c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "wiringSerial.h"
int myserialOpen (const char *device, const int baud)
{
struct termios options ;
speed_t myBaud ;//波特率
int status, fd ;
switch (baud)
{
case 9600: myBaud = B9600 ; break ;
case 115200: myBaud = B115200 ; break ;
}
if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
return -1 ;
fcntl (fd, F_SETFL, O_RDWR) ;
// Get and modify current options:
tcgetattr (fd, &options) ;
cfmakeraw (&options) ;
cfsetispeed (&options, myBaud) ;
cfsetospeed (&options, myBaud) ;
options.c_cflag |= (CLOCAL | CREAD) ;
options.c_cflag &= ~PARENB ;
options.c_cflag &= ~CSTOPB ;
options.c_cflag &= ~CSIZE ;
options.c_cflag |= CS8 ;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
options.c_oflag &= ~OPOST ;
options.c_cc [VMIN] = 0 ;
options.c_cc [VTIME] = 100 ; // Ten seconds (100 deciseconds)
tcsetattr (fd, TCSANOW, &options) ;
ioctl (fd, TIOCMGET, &status);
status |= TIOCM_DTR ;
status |= TIOCM_RTS ;
ioctl (fd, TIOCMSET, &status);
usleep (10000) ; // 10mS
return fd ;
}
void serialSendstring(const int fd, const char *s)
{
int ret;
ret = write (fd, s, strlen (s));
if (ret < 0)
printf("Serial Puts Error\n");
}
int serialGetstring (const int fd,char *buffer)
{
int n_read;
n_read= read(fd, buffer,32);
return n_read;
}
2、uartTest.c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>
#include "uartTool.h"
int fd;
void* readSerial()//接收
{
char buffer[32];//处理接收数据32个字节
while(1){
memset(buffer,'\0',sizeof(buffer));//清空数组
serialGetstring(fd, buffer);//接收数据
printf("GET->%s\n",buffer);//打印接收数据
}
}
void* sendSerial()
{
char buffer[32];
while(1){
memset(buffer,'\0',sizeof(buffer)+1);//sizeof计算数据大小
scanf("%s",buffer);//写入字符串
serialSendstring(fd,buffer);//发送数据
}
}
int main(int argc, char **argv)
{
char deviceName[32]={'\0'};
pthread_t readt;//声明接收线程
pthread_t sendt;//声明发送线程
//主函数处理
if(argc<2){
printf("use:%s /dev/ttyS?\n",argv[0]);
return -1;
}
//验证串口接入信息
strcpy(deviceName,argv[1]);
if((fd = myserialOpen(deviceName,115200)) == -1){
printf("open %s error\n",deviceName);
return -1;
}
pthread_create(&readt,NULL,readSerial,NULL);//创建线程处理接收端
pthread_create(&sendt,NULL,sendSerial,NULL);//创建线程处理发送端
while(1){sleep(10);}
}
3、uartTool.h
int myserialOpen (const char *device, const int baud);//函数原型声明
void serialSendstring(const int fd, const char *s);
int serialGetstring (const int fd,char *buffer);
4、编译实现