gpt4 book ai didi

c - 为什么我的串口不断收到数据?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:21:25 26 4
gpt4 key购买 nike

我买了一个 GSM 模块,我试图通过 USB 到串行接口(interface)(FT232 来命名组件)来控制它。

我在 GSM 模块之前写了一个小程序来测试 FT232(通过将 TX 连接到 RX),但我遇到了一个问题。

我可以发送数据(通常是“AT\r”)并且我可以接收相同的数据(希望如此)但我会一次又一次地接收数据(通常是“0x0A”,有时是原始数据:“AT\r”)。 ..

你能帮我解决这个问题吗?


main.c代码

#include    <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>

int open_serial();

int close_serial(int serial_fd);

int read_serial (int fd);

int main ()
{
int serial_fd = open_serial();

if (serial_fd == EXIT_FAILURE)
return EXIT_FAILURE;

struct termios old_termios;
struct termios serial_termios;

// Port configuration backup
tcgetattr(serial_fd, &old_termios);
serial_termios = old_termios;
// Port setup
serial_termios.c_cflag |= (B9600 | CS8 | CLOCAL | CREAD);
serial_termios.c_lflag |= (ICANON);
// Port configuration update
tcsetattr(serial_fd, TCSANOW, &serial_termios);

int num_char;
char* str2send = "AT\r";

num_char = write(serial_fd, str2send, strlen(str2send));
if (num_char < 0)
{
fputs("FAIL !\n", stderr);
}
else
{
printf("%d char sended\n", num_char);
}

fd_set serial_in;
int count=0;

while ((read_serial(serial_fd) > 0) & (count < 25))
count++;

// Port configuration restore
tcsetattr(serial_fd, TCSANOW, &old_termios);

return close_serial(serial_fd);
}

int open_serial()
{
int serial_fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);

printf("Serial port opening...\n");
if (serial_fd == -1)
{
printf("FAIL !\n");
perror("open()");
return EXIT_FAILURE;
}
printf("SUCCESS ! [FD] : [%02d]\n", serial_fd);

return serial_fd;
}

int close_serial(int serial_fd)
{
printf("Serial port closing...\n[FD] : [%02d]\n", serial_fd);
if (close(serial_fd) == -1)
{
printf("FAIL !\n");
perror("close()");
return EXIT_FAILURE;
}
printf("SUCCESS !\n");

return EXIT_SUCCESS;
}
int read_serial (int fd)
{
char rcv_serial [256];
int max_fd = fd + 1;
fd_set input;
FD_ZERO(&input);
FD_SET(fd, &input);
int count;
// 1sec timeout
struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;

int nb_char, ret_read;
int ret_sel = select(max_fd, &input, NULL, NULL, &timeout);
if (ret_sel < 0)
{
perror("select() :");
}
else if (ret_sel == 0)
{
printf("TIMEOUT\n");
}
else
{
if (FD_ISSET(fd, &input))
{
nb_char = 10;
ioctl(fd, FIONREAD, &nb_char);
printf("%d chars availables\n", nb_char);
ret_read = read(fd, &rcv_serial, nb_char);
printf("[ RET READ : %d]\n", ret_read);
rcv_serial[ret_read] ='\0';
for (count = 0; count < ret_read; count++)
printf("[%02X]", rcv_serial[count]);
printf("\n");
//printf("[%s]\n", rcv_serial);
}
}
return ret_sel;
}

结果

Serial port opening...
SUCCESS ! [FD] : [03]
3 char sended
3 chars availables
[ RET READ : 3]
[41][54][0A]
3 chars availables
[ RET READ : 3]
[41][54][0A]
1 chars availables
[ RET READ : 1]
[0A]
3 chars availables
[ RET READ : 3]
[41][54][0A]
1 chars availables
[ RET READ : 1]
[0A]
1 chars availables
[ RET READ : 1]
[0A]
1 chars availables
[ RET READ : 1]
[0A]
3 chars availables
[ RET READ : 3]
[41][54][0A]
1 chars availables
[ RET READ : 1]
[0A]
1 chars availables
[ RET READ : 1]
[0A]
1 chars availables
[ RET READ : 1]
[0A]
Serial port closing...

最佳答案

感谢@sawdust 的评论。我添加这个来解决我的问题以禁用输入回显:

serial_termios.c_lflag &= ~(ECHO | ECHOE | ECHONL | IEXTEN)

关于c - 为什么我的串口不断收到数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42278424/

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