串口应用程序

#include <termios.h>

struct termios
{
    tcflag_t c_iflag;      /* input modes */
    tcflag_t c_oflag;      /* output modes */
    tcflag_t c_cflag;      /* control modes */
    tcflag_t c_lflag;      /* local modes */
    cc_t     c_cc[NCCS];   /* special characters */
}
int tcgetattr(int fd, struct termios *termios_p); //获取与终端相关的参数
inttcsetattr(int fd, int optional_actions, struct termios *termios_p); //设置终端参数
int tcsendbreak(int fd, int duration);
int tcdrain(int fd); //等待直到所有写入 fd 引用的对象的输出都被传输
int tcflush(int fd, int queue_selector); //刷清(扔掉)输入缓存
int tcflow(int fd, int action); //挂起传输或接受
int cfmakeraw(struct termios *termios_p);// 制作新的终端控制属性
speed_t cfgetispeed(struct termios *termios_p); //得到输入速度
speed_t cfgetospeed(struct termios *termios_p); //得到输出速度
int cfsetispeed(struct termios *termios_p, speed_t speed); //设置输入速度
int cfsetospeed(struct termios *termios_p, speed_t speed) //设置输出速度

send.c

#include <fcntl.h>
#include <unistd.h>
#include <termios.h>

int main()
{
    char sbuf[] = {"Hello, this is a serial port test!\n"};
    struct termios option;

    ///dev/pts/?是两个虚拟串口之一
    int fd = open("/dev/pts/25", O_RDWR | O_NOCTTY | O_NONBLOCK);
    if(fd == -1) 
    {   
            perror("Open serial port error!\n");
            return -1; 
    }   

    printf("Open serial port success!\n");

    tcgetattr(fd, &option);
    cfmakeraw(&option);
    cfsetispeed(&option, B9600);
    cfsetospeed(&option, B9600);
    tcsetattr(fd, TCSANOW, &option);

    int length = sizeof(sbuf);
    int ret = write(fd, sbuf, length);
    if(ret == -1) 
    {   
            perror("Write data error!\n");
            return -1; 
    }   

    printf("The number of char sent is %d\n", ret);

    ret = close(fd);
    if(ret == -1)
        perror("Close the device failure!\n");

    return 0;
}

recv.c

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <math.h>

#define MAX_BUFFER_SIZE 512

int main()
{
    char hd[MAX_BUFFER_SIZE], *rbuf;
    int ret;
    struct termios opt;

    ///dev/pts/?是两个虚拟串口之一
    int fd = open("/dev/pts/28", O_RDWR|O_NOCTTY|O_NDELAY);
    if(fd == -1) 
    {   
        perror("Open serial port error!\n");
        return -1; 
    }   

    printf("Open serial port success!\n");

    tcgetattr(fd, &opt);
    cfmakeraw(&opt);
    cfsetispeed(&opt, B9600);
    cfsetospeed(&opt, B9600);
    tcsetattr(fd, TCSANOW, &opt);

    rbuf = hd; 
    printf("Ready for receiving data...\n");
    while(1)
    {
        while((ret = read(fd, rbuf, 1)) > 0)
                printf("%c", *rbuf);
    }
    printf("\n");

    ret = close(fd);
    if(ret == -1)
        perror("Close the device failure!\n");

    return 0;
}

虚拟串口http://blog.csdn.net/zhangxuechao_/article/details/70670405

posted @   thomas_blog  阅读(187)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示