Linux应用程序开发--串口通信

第一步:

参考“Linux应用程序开发--Hello”的第一步。

 

第二步:

示例程序如下:

#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 <pthread.h>

int main(void)
{
    int fd;
    char rxBuffer[1024];
    int len = 1024;
    int rxByte;
    struct termios opt;

    fd = open("/dev/ttyS1",O_RDWR);
    if(fd==-1)
    {
        perror("error");
    }
    //set bps
    tcgetattr(fd,&opt);
    cfsetispeed(&opt,B9600);
    cfsetospeed(&opt,B9600);
    //set raw input and output
    opt.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);
    opt.c_oflag &= ~OPOST;
    tcsetattr(fd,TCSANOW,&opt);
    //print tips
    printf("Please enter any data to the COM1, Enter key to confirm/n");
    write(fd,"READY!/n",7);
    //receive bytes and send back
    while(1)
    {
        rxByte = 0;
        rxByte = read(fd,rxBuffer,len);
        if(rxByte>0)
        {
           rxBuffer[rxByte]='/0';
            write(fd,"RE:",3);
            write(fd,rxBuffer,rxByte);
            write(fd,"/n",1);
           printf("%s/n",rxBuffer);
        }
    }
    close(fd);
    return 0;
}

 

第三步:

参考“Linux应用程序开发--Hello”的第三步。

 

第四步:

参考“Linux应用程序开发--Hello”的第四步。

 

第五步:

参考“Linux应用程序开发--Hello”的第五步。

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值