gpt4 book ai didi

c - 如何通过C-Linux向串口发送AT命令

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

当然有termios.h,不过这里我说的是AT命令。我希望他们被处决

如何在 Linux 中通过 C 向串口发送 AT 命令并执行

最佳答案

看看这个简短的例子(它有效):

struct termios options;
int fd;

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

if (fd < 0)
{
printf("Error opening serial port\n");
exit(1);
}

bzero(&options, sizeof(options));
options.c_cflag = B9600 | CS8 | CLOCAL | CREAD | IGNPAR;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &options);

if (write(fd, "ATZ\r", 4) < 4)
{
printf("Write error - %s \n", strerror(errno));
exit (1);
}

// read back for OK or KO then do your stuff...

关于c - 如何通过C-Linux向串口发送AT命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14933311/

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