用到的工具:source insight
ctrl+/:输入需要查找的内容,随便找个位置点进去,再用ctrl+鼠标左键查看该函数实现
应用态指挥内核,操作硬件。
串口源码:
/*
* wiringSerial.c:
* Handle a serial port
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#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"
/*
* serialOpen:
* Open and initialise the serial port, setting all the right
* port parameters - or as many as are required - hopefully!
*********************************************************************************
*/
int serialOpen (const char *device, const int baud)
{
struct termios options ;
speed_t myBaud ;
int status, fd ;
switch (baud)
{
case 50: myBaud = B50 ; break ;
case 75: myBaud = B75 ; break ;
case 110: myBaud = B110 ; break ;
case 134: myBaud = B134 ; break ;
case 150: myBaud = B150 ; break ;
case 200: myBaud = B200 ; break ;
case 300: myBaud = B300 ; break ;
case 600: myBaud = B600 ; break ;
case 1200: myBaud = B1200 ; break ;
case 1800: myBaud = B1800 ; break ;
case 2400: myBaud = B2400 ; break ;
case 4800: myBaud = B4800 ; break ;
case 9600: myBaud = B9600 ; break ;
case 19200: myBaud = B19200 ; break ;
case 38400: myBaud = B38400 ; break ;
case 57600: myBaud = B57600 ; break ;
case 115200: myBaud = B115200 ; break ;
case 230400: myBaud = B230400 ; break ;
case 460800: myBaud = B460800 ; break ;
case 500000: myBaud = B500000 ; break ;
case 576000: myBaud = B576000 ; break ;
case 921600: myBaud = B921600 ; break ;
case 1000000: myBaud = B1000000 ; break ;
case 1152000: myBaud = B1152000 ; break ;
case 1500000: myBaud = B1500000 ; break ;
case 2000000: myBaud = B2000000 ; break ;
case 2500000: myBaud = B2500000 ; break ;
case 3000000: myBaud = B3000000 ; break ;
case 3500000: myBaud = B3500000 ; break ;
case 4000000: myBaud = B4000000 ; 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 ;
}
/*
* serialFlush:
* Flush the serial buffers (both tx & rx)
*********************************************************************************
*/
void serialFlush (const int fd)
{
tcflush (fd, TCIOFLUSH) ;
}
/*
* serialClose:
* Release the serial port
*********************************************************************************
*/
void serialClose (const int fd)
{
close (fd) ;
}
/*
* serialPutchar:
* Send a single character to the serial port
*********************************************************************************
*/
void serialPutchar (const int fd, const unsigned char c)
{
int ret;
ret = write (fd, &c, 1) ;
if (ret < 0)
printf("Serial Putchar Error\n");
}
/*
* serialPuts:
* Send a string to the serial port
*********************************************************************************
*/
void serialPuts (const int fd, const char *s)
{
int ret;
ret = write (fd, s, strlen (s));
if (ret < 0)
printf("Serial Puts Error\n");
}
/*
* serialPrintf:
* Printf over Serial
*********************************************************************************
*/
void serialPrintf (const int fd, const char *message, ...)
{
va_list argp ;
char buffer [1024] ;
va_start (argp, message) ;
vsnprintf (buffer, 1023, message, argp) ;
va_end (argp) ;
serialPuts (fd, buffer) ;
}
/*
* serialDataAvail:
* Return the number of bytes of data avalable to be read in the serial port
*********************************************************************************
*/
int serialDataAvail (const int fd)
{
int result ;
if (ioctl (fd, FIONREAD, &result) == -1)
return -1 ;
return result ;
}
/*
* serialGetchar:
* Get a single character from the serial device.
* Note: Zero is a valid character and this function will time-out after
* 10 seconds.
*********************************************************************************
*/
int serialGetchar (const int fd)
{
uint8_t x ;
if (read (fd, &x, 1) != 1)
return -1 ;
return ((int)x) & 0xFF ;
}
自己实现串口源码封装:
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);//将get参数放到&status中 status |= TIOCM_DTR ;//status进行修改 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; }
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);
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]; 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)); 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("uage:%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(&readt,NULL,sendserial,NULL); while(1) sleep(10); }
编译运行:
编译:gcc uarttest.c uartTool.c -lpthread
运行:./a.out /dev/ttyS5