基于wiringPi的串口开发#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <pthread.h>
#include <malloc.h>
int fd;
void *SendHandler()
{
char *str;
str = (char *)malloc(128*sizeof(char));
while(1) {
memset(str, '\0', 128*sizeof(char));
scanf("%s",str);
while(*str) {
serialPutchar (fd, *str) ;
str++;
}
}
}
void *RevHandler()
{
while(1) {
while (serialDataAvail (fd))
{
printf ("%c", serialGetchar (fd)) ;
}
}
}
int main ()
{
pthread_t send_t;
pthread_t rev_t;
if ((fd = serialOpen ("/dev/ttyS5", 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 ;
}
pthread_create(&send_t, NULL, SendHandler, NULL);
pthread_create(&rev_t, NULL, RevHandler, NULL);
while (1)
{
}
return 0 ;
}
Linux原生串口开发
#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 ;
default :
return -2 ;
}
if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
return -1 ;
fcntl (fd, F_SETFL, O_RDWR) ;
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 ;
tcsetattr (fd, TCSANOW, &options) ;
ioctl (fd, TIOCMGET, &status);
status |= TIOCM_DTR ;
status |= TIOCM_RTS ;
ioctl (fd, TIOCMSET, &status);
usleep (10000) ;
return fd ;
}
void myserialPuts (const int fd, const char *s)
{
int ret;
ret = write (fd, s, strlen (s));
if (ret < 0)
printf("Serial Puts Error\n");
}
int myserialGetstr (const int fd, char *buf)
{
uint8_t x;
x = read(fd, buf, 32);
if(x != 1)
return -1;
return x;
}
int myserialOpen (const char *device, const int baud);
void myserialPuts (const int fd, const char *s);
int myserialGetstr (const int fd, char *buf);
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <pthread.h>
#include <malloc.h>
int fd;
void *SendHandler()
{
char *str;
str = (char *)malloc(128*sizeof(char));
while(1) {
memset(str, '\0', 128*sizeof(char));
scanf("%s",str);
while(*str) {
serialPutchar (fd, *str) ;
str++;
}
}
}
void *RevHandler()
{
while(1) {
while (serialDataAvail (fd))
{
printf ("%c", serialGetchar (fd)) ;
}
}
}
int main ()
{
pthread_t send_t;
pthread_t rev_t;
if ((fd = serialOpen ("/dev/ttyS5", 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 ;
}
pthread_create(&send_t, NULL, SendHandler, NULL);
pthread_create(&rev_t, NULL, RevHandler, NULL);
while (1)
{
}
return 0 ;
}