#include #include #include #include #include #define DEVICE_FILE "/dev/gpio_led" int main() { int fd; char write_buf[100] = "Hello from user space!"; char read_buf[100]; // 打开设备文件 fd = open(DEVICE_FILE, O_RDWR); if (fd < 0) { perror("open"); return -1; } // 从设备读取数据 ssize_t read_bytes = read(fd, read_buf, sizeof(read_buf)); if (read_bytes < 0) { perror("read"); close(fd); return -1; } else if (read_bytes == 0) { printf("No data read from the device.\n"); } else { read_buf[read_bytes] = '\0'; printf("Read from the device: %s\n", read_buf); } // 向设备写入数据 ssize_t written = write(fd, write_buf, strlen(write_buf)); if (written < 0) { perror("write"); close(fd); return -1; } else { printf("Wrote %zd bytes to the device.\n", written); } // 从设备读取数据 ssize_t read_bytes = read(fd, read_buf, sizeof(read_buf)); if (read_bytes < 0) { perror("read"); close(fd); return -1; } else if (read_bytes == 0) { printf("No data read from the device.\n"); } else { read_buf[read_bytes] = '\0'; printf("Read from the device: %s\n", read_buf); } // 关闭设备文件 close(fd); return 0; }