【Orangepi Zero2】串口通信
~/wiringOP/examples 底下的serialTest.c 示例程序
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <wiringPi.h>
#include <wiringSerial.h>
int main ()
{
int fd ;
int count ;
unsigned int nextTime ;
if ((fd = serialOpen ("/dev/ttyS2", 115200)) < 0)
{
fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
return 1 ;
}
if (wiringPiSetup () == -1)
{
fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
return 1 ;
}
nextTime = millis () + 300 ;
for (count = 0 ; count < 256 ; )
{
if (millis () > nextTime)
{
printf ("\nOut: %3d: ", count) ;
fflush (stdout) ;
serialPutchar (fd, count) ;
nextTime += 300 ;
++count ;
}
delay (3) ;
while (serialDataAvail (fd))
{
printf (" -> %3d", serialGetchar (fd)) ;
fflush (stdout) ;
}
}
printf ("\n") ;
return 0 ;
}

脱离 wiringPi 库 自己实现串口通信
根据用户手册修改配置文件



进入linux系统后,先确认下/dev下是否存在uart5的设备节点
ls /dev/ttyS5

mySerial2.h
int my_serialOpen (const char *device, const int baud);
void my_serialPuts(const int fd, const char *s);
int my_serialGets(const int fd, const char *buffer);
char* serialGetchar(const int fd);
mySerial2.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>
int my_serialOpen (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 ;
default:
return -2 ;
}
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 my_serialPuts(const int fd, const char *s)
{
int n_write = write(fd, s, strlen(s));
if(n_write == -1)
{
perror("write");
exit(-1);
}
}
int my_serialGets(const int fd, char *buffer)
{
int n_read = read(fd, buffer, 32);
if(n_read == -1)
{
perror("read");
exit(-1);
}
return n_read;
}
char* serialGetchar (const int fd)
{
char *x;
if (read (fd, x, 1) != 1)
exit(-1);
return x;
}
serialTest.c
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include "mySerial.h"
int fd;
void* serialSend()
{
char *send_buf;
send_buf = (char *)malloc(32*sizeof(send_buf));
while(1)
{
memset(send_buf, '\0', sizeof(send_buf));
fgets(send_buf, sizeof(send_buf), stdin);
my_serialPuts (fd, send_buf);
}
}
void* serialRecieve()
{
char *recieve_buf;
recieve_buf = (char *)malloc(32*sizeof(recieve_buf));
// char recieve_buf[32];
while(1)
{
memset(recieve_buf, '\0', sizeof(recieve_buf));
my_serialGets(fd, recieve_buf);
printf ("GET->%s\n", recieve_buf);
fflush(stdout);
}
}
int main ()
{
pthread_t t1;
pthread_t t2;
if ((fd = my_serialOpen ("/dev/ttyS5", 115200)) < 0)
{
fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
return 1 ;
}
pthread_create(&t1, NULL, serialSend, NULL);
pthread_create(&t2, NULL, serialRecieve, NULL);
while(1)
{
sleep(3);
}
printf ("\n") ;
return 0 ;
}
运行结果

1569

被折叠的 条评论
为什么被折叠?



