linux下实现串口功能

简介: linux下实现串口功能

一、参考官方代码

1.先从wiringpi库复制一个串口代码

2.查看串口类型

3.将代码修改成ttyS5

4.改完代码后打开串口助手然后编译

代码示例:

#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(10);
  }
 
  printf ("\n") ;
  return 0 ;
}

二、linux原生串口开发

1.在linux环境下封装串口相关代码

(1)vi 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)vi 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 <unistd.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(&sendt, NULL, sendSerial,NULL);
  while(1){sleep(10);}
}

(3)vi 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);

2.开发板接线和共地图示(根据原理图)

3.编译结果:

串口调试助手图示

相关文章
|
21天前
|
监控 Shell Linux
【Shell 命令集合 网络通讯 】Linux 分析串口的状态 statserial命令 使用指南
【Shell 命令集合 网络通讯 】Linux 分析串口的状态 statserial命令 使用指南
38 0
|
21天前
|
存储 监控 Shell
【Shell 命令集合 磁盘管理 】Linux 关闭磁盘配额功能 quotaoff命令使用教程
【Shell 命令集合 磁盘管理 】Linux 关闭磁盘配额功能 quotaoff命令使用教程
31 1
|
21天前
|
存储 Shell Linux
【Shell 命令集合 磁盘管理 】Linux 启用指定文件系统上的磁盘配额功能 quotaon 命令使用教程
【Shell 命令集合 磁盘管理 】Linux 启用指定文件系统上的磁盘配额功能 quotaon 命令使用教程
36 1
|
15天前
|
存储 运维 关系型数据库
2024年最全ceph的功能组件和架构概述(2),Linux运维工程面试问题
2024年最全ceph的功能组件和架构概述(2),Linux运维工程面试问题
2024年最全ceph的功能组件和架构概述(2),Linux运维工程面试问题
|
15天前
|
运维 Ubuntu 安全
运维最全linux 命令行操作串口_linux串口命令(2),2024年最新Linux运维源码的Binder权限是如何控制
运维最全linux 命令行操作串口_linux串口命令(2),2024年最新Linux运维源码的Binder权限是如何控制
运维最全linux 命令行操作串口_linux串口命令(2),2024年最新Linux运维源码的Binder权限是如何控制
|
15天前
|
运维 Linux Perl
运维最全linux 命令行操作串口_linux串口命令(1),21年Linux运维面经分享
运维最全linux 命令行操作串口_linux串口命令(1),21年Linux运维面经分享
运维最全linux 命令行操作串口_linux串口命令(1),21年Linux运维面经分享
|
21天前
|
Linux Shell 开发者
|
21天前
|
数据采集 Linux Go
Linux系统是如何控制串口收发数据的?
Linux系统是如何控制串口收发数据的?
20 0
|
21天前
|
缓存 算法 Linux
Linux操作体系结构与功能流程
Linux操作体系结构与功能流程
11 2
|
21天前
|
安全 Linux
嵌入式Linux系统关闭串口调试信息的输出
嵌入式Linux系统关闭串口调试信息的输出
66 1