【串口AT指令测试程序】
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
void send_atcmd(int fd, const char *cmd)
{
char resp[100];
int ret;
printf("Command: %s\n", cmd);
write(fd, cmd, strlen(cmd));
write(fd, "\r\n", 2);
ret = read(fd, resp, sizeof(resp) - 1);
resp[ret] = '\0';
if (ret >= 1 && resp[ret - 1] == '\n')
resp[ret - 1] = '\0';
printf("Response: %s (size=%d)\n", resp, ret);
}
int main()
{
int fd;
fd = open("/dev/ttyUSB1", O_RDWR);
if (fd == -1)
{
perror("open() failed");
return -1;
}
send_atcmd(fd, "AT");
close(fd);
return 0;
}
【程序运行结果】
root@rk3308b-buildroot:/root# ./atcmd_test
Command: AT
Response: AT (size=3)
root@rk3308b-buildroot:/root#