linux系统编程--read/write函数

  1. read/write函数
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
// read() attempts to read up to count bytes from file 
// descriptor fd into the buffer starting at buf.
ssize_t write(int fd, const void *buf, size_t count);
// write()  writes up to count bytes from the buffer 
// starting at buf to the file referred to by the file descriptor fd.

  1. 编程实现简单的cp功能
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>

int main(int argc, char* argv[])
{
    char buf[1024];
    int n = 0;

    int fd1, fd2;
    fd1=open(argv[1],O_RDONLY);
    if(fd1 == -1){
        perror("open argv1 error");
        exit(2);
    }

    fd2=open(argv[2],O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if(fd1 == -1){
        perror("open argv2 error");
        exit(1);
    }

    while((n=read(fd1,buf,1024)) != 0){
        if(n<0){
            perror("read error");
            break;
        }
        write(fd2,buf,n);
    }

    close(fd1);
    close(fd2);
    return 0;

}

3.错误处理函数
1、与 errno 相关

printf("xxx error: %d\n", errno);
char* *strerror(int errnum);
	printf("xxx error: %s\n", strerror(errno));

2、perror函数

void perror(const char* s);
	perror("xxx error");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RS485-RTU通讯是一种广泛应用于工业自动化领域的通讯协议,Linux下可以使用各种编程语言实现RS485-RTU通讯。 下面以C语言为例,介绍如何在Linux下实现RS485-RTU通讯编程。 1. 打开串口设备 使用Linux下的串口设备文件(如/dev/ttyS0或/dev/ttyUSB0等),可以使用open函数打开串口设备,例如: ```c int fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); if (fd < 0) { perror("Open serial port failed"); return -1; } ``` 其中,O_RDWR表示以读写方式打开串口设备,O_NOCTTY表示不将串口设备作为控制终端,O_NDELAY表示非阻塞方式打开串口设备。 2. 配置串口参数 在打开串口设备后,需要配置串口的波特率、数据位、停止位、校验位等参数,可以使用tcgetattr和tcsetattr函数实现,例如: ```c struct termios options; tcgetattr(fd, &options); cfsetispeed(&options, B9600); // 设置波特率为9600 cfsetospeed(&options, B9600); options.c_cflag |= CLOCAL; // 忽略调制解调器线路状态 options.c_cflag |= CREAD; // 开启接收器 options.c_cflag &= ~CSIZE; // 清除数据位设置 options.c_cflag |= CS8; // 设置数据位为8位 options.c_cflag &= ~PARENB; // 禁用校验位 options.c_cflag &= ~CSTOPB; // 设置停止位为1位 tcsetattr(fd, TCSANOW, &options); ``` 3. 发送数据 在配置好串口参数后,可以使用write函数向串口设备发送数据,例如: ```c char data[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A}; int len = write(fd, data, sizeof(data)); if (len != sizeof(data)) { perror("Write data failed"); return -1; } ``` 其中,data为待发送的数据,sizeof(data)为数据长度。 4. 接收数据 在发送数据后,可以使用read函数从串口设备接收数据,例如: ```c char buffer[256]; int len = read(fd, buffer, sizeof(buffer)); if (len > 0) { // 处理接收到的数据 } else { perror("Read data failed"); return -1; } ``` 其中,buffer为接收数据的缓冲区,sizeof(buffer)为缓冲区长度。 5. 关闭串口设备 在使用完串口设备后,需要使用close函数关闭串口设备,例如: ```c close(fd); ``` 以上就是在Linux下实现RS485-RTU通讯编程的基本步骤,具体实现还需要根据具体应用场景进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值