linux串口编程

         关于串口的东西我就不扯了,这个串口的原理和联线方法这个网上很多,而且也是靠谱的。我几天还是谈谈我用C编程时遇到的问题。

还是先说说我实验环境吧,我的使用环境是两台虚拟机之间的串口的通信,我的用

Virtualbox建立的虚拟机,一台为ubuntu,一台fedora。用两个不同发行版本linux来做,完全是为了我远程的时候可以区别,而且我刚好有两台这样的虚拟机。

关于virtualbox上串口联机的方法比较少,我在这里就顺便说一下。

这个是ubuntu虚拟机的硬件设置的串口部分。

这是fedora上的

那个virtualbox创建的虚拟串口文件位置,\\.\pipe\vbox

两台虚拟机不同的就是要有一台设置那里要勾创建通道,另外一台不要勾通道,这样的两台虚拟机才会公用一个通道,才能把两台虚拟机的串口连在一起,还要记住的是启动两台虚拟机的时候要先启动那台勾了创建通道的机子。

启动机子后,我们就远程过去,看看测试一下串口有没有通先。

在这步,我们将用到重定向。

我这里两台机子的串口都是ttyS0

我fedora中显示串口

然后我在ubuntu下用echo输出重定向到串口

现在我们就可以在fedora下就可以看到我们重定向发送的内容了

需要用到的头文件

#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<sys/stat.h>

#include<sys/types.h>

#include<fcntl.h>

#include<termios.h>

#include<errno.h>

#include<string.h>

这些就是要用到的头文件

接下来,我主要讲讲思路,这个算法就是

一:打开串口设备文件,/dev/ttyS0;

二:配置并使能串口。

三:让配置生效。

四:向串口写入buf字符数组中的字符串或者读取串口中的字符串到buf字符数组里面。

五:关闭串口文件。

算法就是这样。

下面就是我的代码:

发送部分:

 

   
   
   
   
  1. #include<stdio.h> 
  2. #include<stdlib.h> 
  3. #include<unistd.h> 
  4. #include<sys/stat.h> 
  5. #include<sys/types.h> 
  6. #include<fcntl.h> 
  7. #include<termios.h> 
  8. #include<errno.h> 
  9. #include<string.h> 
  10. int main() 
  11.     int fd; 
  12.     char buf[100]; 
  13.     fd=open("/dev/ttyS0",O_RDWR); 
  14.     if(-1==fd){ 
  15.     perror("打开串口错误!:"); 
  16.     } 
  17.     struct termios newport; 
  18.     newport.c_cflag|=CLOCAL|CREAD;//串口连接使能 
  19.     cfmakeraw(&newport);//设置串口为原始模式 
  20.     cfsetispeed(&newport,B115200);//设置输入波特率为115200 
  21.     cfsetospeed(&newport,B115200);//设置输出波特率为115200 
  22.     newport.c_cflag &=~PARENB;//不启用奇偶校验 
  23.     newport.c_cflag &=~CSTOPB;//将停止位设置为一个比特 
  24.     newport.c_cflag &=~CSIZE;//用数据位的掩码清空数据位设置 
  25.     newport.c_cflag &= CS8;//设置为数据位为8位 
  26.     tcflush(fd,TCIOFLUSH);//清空串口缓冲队列 
  27.     tcsetattr(fd,TCSANOW,&newport);//串口配置立即生效 
  28.   memset(buf,0,100); 
  29.     while(1) 
  30.     { 
  31.         scanf("%s",buf); 
  32.         write(fd,buf,strlen(buf)); 
  33.         memset(buf,0,100); 
  34.     } 
  35.     close(fd); 
  36.     return 0; 

接收部分:

 

   
   
   
   
  1. #include<stdio.h> 
  2. #include<stdlib.h> 
  3. #include<unistd.h> 
  4. #include<sys/types.h> 
  5. #include<sys/stat.h> 
  6. #include<fcntl.h> 
  7. #include<termios.h> 
  8. #include<errno.h> 
  9. #include <string.h> 
  10. int main() 
  11.         int fd; 
  12.   char buf[100]; 
  13.         fd=open("/dev/ttyS0",O_RDWR); 
  14.         if(-1==fd){ 
  15.         perror("提示错误!:"); 
  16.         } 
  17.         struct termios newport; 
  18.         newport.c_cflag |= (CLOCAL | CREAD);//串口连接使能 
  19.         cfmakeraw(&newport);//设置串口为原始模式 
  20.         cfsetispeed(&newport,B115200);//设置输入波特率为115200 
  21.         cfsetospeed(&newport,B115200);//设置输出波特率为115200 
  22.         newport.c_cflag &=  ~PARENB;//不启用奇偶校验 
  23.         newport.c_cflag &=  ~CSTOPB;//将停止位设置为一个比特 
  24.         newport.c_cflag &=  ~CSIZE;//用数据位掩码清空数据位设置 
  25.         newport.c_cflag |=  CS8;//设置为数据位为8位 
  26.   tcflush(fd,TCIOFLUSH);//清空串口缓冲队列 
  27.         tcsetattr(fd,TCSANOW,&newport);//串口配置立即生效 
  28.         memset(buf,0,100);//这里是对数组初始化 
  29.         while(1) 
  30.         {   
  31.             read(fd,buf,99); 
  32.   //因为传过来的字符是没有结束标志的,所以我们这里要少一位  
  33.             printf("%s\n",buf); 
  34.             memset(buf,0,100); 
  35.         } 
  36.         close(fd); 
  37.         return 0; 

你可能感兴趣的:(编程,linux)