linux virtual serial programming

Environment: unbuntu14 virtual machine, can be networked set
Objective 1: serial read-write virtualization (virtual serial connection 2) in the linux environment

Steps:
First, create a virtual serial port
1. Install a virtual software
apt-get install socat

2. Create a virtual serial port
socat -d -d pty, raw, echo = 0 pty, raw, echo = 0

Second, the serial read and write test -echo test
write serial:
echo equipment

Serial read:
CAT Equipment
Reference document:
https://blog.csdn.net/rainertop/article/details/26706847


Objective 2: Use the linux environment serial debugging tools debugging
tools: cutecom
main elements:
the use of cutecom serial debugging tools, using two virtual serial port to send and receive communication
reference documentation:
https://blog.csdn.net/zhaoqi2617/article/details / 72238546


Objective 3: Write the serial procedures in the linux environment, send and receive
yet execute
Reference:
Detailed serial communication developed under linux
https://user.qzone.qq.com/249149995/2
https://blog.csdn.net/ baweiyaoji / article / details / 72885633

Original example: serial_test.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
//#include <termbits.h>

int main()
{
char sbuf[]="humin is handsome";
struct termios opt;
int len_send=sizeof(sbuf);
int fd;
int ret;

cfsetispeed(&opt, B9600);
cfsetospeed(&opt, B9600);

fd = open("/dev/pts/24", O_RDWR|O_NOCTTY);
if(fd == -1)
perror("Can not open Serial_Port 1/n!");

ret = write(fd,sbuf,len_send);
if(ret == -1) {
printf("Wirte sbuf error./n");
}

ret = close(fd);
if(ret == -1) {
printf("Close fd error./n");
}

return 0;
}

 

Guess you like

Origin www.cnblogs.com/longzhiwen/p/11487749.html