gpt4 book ai didi

c - 使用C代码读取Ubuntu linux上的COM端口

转载 作者:行者123 更新时间:2023-11-30 16:58:30 25 4
gpt4 key购买 nike

我想读取运行 Ubuntu Linux 的 ODROID C1 上的 USB0 传入的信息。我在 codereview 上找到了一些东西并在 3 个单独的文件中使用它,如下所示:

主.C:

#include <stdio.h>   /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include "serial_port.h"

int main()
{
int local_socket;
int serial_fd;
int max_fd;
fd_set input;
fd_set tmp_input;
char *serial_output_buffer;
serial_output_buffer=malloc(11 * sizeof(char));

serial_fd=open_port();
local_socket=open_local_socket();

FD_ZERO(&input);
FD_SET(serial_fd, &input);
FD_SET(local_socket, &input);
max_fd = (local_socket > serial_fd ? local_socket : serial_fd) + 1;

si_processed=0;
serial_output_buffer[10]='\0';
while(1) {
tmp_input=input;

n = select(max_fd,&tmp_input,NULL,NULL,NULL);

/* See if there was an error */
if (n<0)
perror("select failed");
else {
/* We have input */
if (FD_ISSET(serial_fd, &input)) {
if(process_serial(serial_fd,serial_output_buffer)) {
printf("read:%s\n",serial_output_buffer);
fflush(stdout);
}
}
if (FD_ISSET(local_socket, &input))
process_socket(local_socket);
}
usleep(20000);
}
return 0;
}

serial_port.h:

#ifndef SERIAL_PORT
#define SERIAL_PORT

int open_port();
int process_serial(int serial_fd,char *output);

int si_processed;

#endif

串行端口.c:

#include "serial_port.h"

char serial_buffer[256];


int process_serial(int serial_fd,char *output) {
int bytes;
int n,i;
char tmp_buffer[256];

ioctl(serial_fd, FIONREAD, &bytes);
if(!bytes && si_processed<INPUT_BYTES_NUM) //proceed if data still in buffer
return 0;

n=read(serial_fd, tmp_buffer, sizeof(tmp_buffer));
for(i=0;i<n;i++) {
serial_buffer[si_processed+i]=tmp_buffer[i];
}
si_processed+=n;
if(si_processed>=INPUT_BYTES_NUM) {
for (i = 0; i < si_processed; ++i)
if (serial_buffer[i] == '1') // found start of packet
break;
if (i > 0) {
// start of packet is not start of buffer
// so discard bad bytes at the start of the buffer

memmove(serial_buffer, serial_buffer+i, si_processed - i);
si_processed -= i;
}
if(si_processed>=INPUT_BYTES_NUM) {
memmove(output, serial_buffer, 10);

//move what left to the beginning
memmove(serial_buffer,serial_buffer+10,si_processed-10);
si_processed -= 10;
return 1;
}
}
return 0;
}

int open_port(void) {
int fd; /* File descriptor for the port */
struct termios options;

fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);

if (fd == -1) {
/*
* Could not open the port.
*/
error_exit("open_port: Unable to open /dev/ttyUSB0");
}
else
fcntl(fd, F_SETFL, 0);

/*
* Get the current options for the port...
*/

tcgetattr(fd, &options);

/*
* Set the baud rates to 19200...
*/

cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);

/*
* Enable the receiver and set local mode...
*/

options.c_cflag |= (CLOCAL | CREAD);

//set 8N1
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;

/*
* Set the new options for the port...
*/

tcsetattr(fd, TCSANOW, &options);


return (fd);
}

并且我使用命令 gcc -o app main.c serial_port.c ,但出现以下错误: In serial_port.c: 'FIONREAD undeclared(first use in function)在serial_port.c中:“INPUT_BYTES_NUM”未声明

代码似乎比我想要的要多得多,我只想要最简单的C代码来读取来自 USB0 的数据,波特率 115200 8N1 ,并像 minicom 一样显示它。

最佳答案

包含以下头文件,

#include <sys/ioctl.h>

关于c - 使用C代码读取Ubuntu linux上的COM端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38787409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com