c - 在ubuntu 12.04中使用C发送AT命令到teltonika GSM调制解调器

标签 c linux

您好,我正在尝试使用 ubuntu 桌面的 com 端口(ttyS0)向 teltonika gsm 调制解调器发送 AT 命令。问题是,同一个 AT 命令没有得到对 AT 命令(或任何其他命令或字符串)的“OK”答复,而是得到了回显。在这方面的任何帮助将不胜感激。这是我正在处理的 C 代码:

#include <sys/types.h>                                                  
#include <sys/stat.h>                                                      
#include <fcntl.h>                                                       
#include <termios.h>                                                    
#include <stdio.h> 
#include <string.h>
#include <stdlib.h>
#include <unistd.h> 
#include <time.h>
#include <errno.h>   
#define BAUDRATE B115200
#define COM1 "/dev/ttyS0"
static int fd;
static struct termios oldtio,newtio;

//==============================================================
int tty_read(char *buf1,int nbytes)
{
int temp;
temp = read(fd,buf1,nbytes);
printf("Read string: %s\n", buf1);
return temp;
}
//==============================================================
int tty_end()
{
tcsetattr(fd,TCSANOW,&oldtio);
close(fd);
}
//==============================================================
int tty_writecmd(char *buf,int nbytes)
{ 

write(fd,buf,nbytes);

usleep(1000);
return tcdrain(fd);
}
//==============================================================
int baud = B115200;
int tty_init()
{
fd = open(COM1, O_RDWR );
if (fd <0) {
  perror(COM1);
  exit(1);
}
tcgetattr(fd,&oldtio);
bzero(&newtio, sizeof(newtio)); 
newtio.c_cflag = baud | CRTSCTS | CS8 | CLOCAL | CREAD ;

newtio.c_iflag = IGNPAR | ICRNL; 
newtio.c_oflag = 0; 
newtio.c_lflag = ICANON;
newtio.c_cc[VINTR]    = 0;     
newtio.c_cc[VQUIT]    = 0;     
newtio.c_cc[VERASE]   = 0;     
newtio.c_cc[VKILL]    = 0;    
newtio.c_cc[VEOF]     = 4;     
newtio.c_cc[VTIME]    = 0;
newtio.c_cc[VMIN]     = 1;
newtio.c_cc[VSWTC]    = 0;     
newtio.c_cc[VSTART]   = 0;     
newtio.c_cc[VSTOP]    = 0;
newtio.c_cc[VSUSP]    = 0; 
newtio.c_cc[VEOL]     = 0;
newtio.c_cc[VREPRINT] = 0; 
newtio.c_cc[VDISCARD] = 0;
newtio.c_cc[VWERASE]  = 0;
newtio.c_cc[VLNEXT]   = 0;
newtio.c_cc[VEOL2]    = 0; 
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
return 0;
}
int main(int argc, char *argv[])
{ int wr,rd;
char *buff;
char recv[15];
char command[] = "AT\r\n";
tty_init();
printf("Write: %d\n", tty_writecmd(command, sizeof(command)));

usleep(10000);
printf("Read: %d\n", tty_read(recv ,sizeof(recv)));
tty_end();
}

and the output is like
write:0
Read string: AT
Read:3

谢谢 P.S:此行为发生在 Ubuntu 桌面上,程序从 VMware station 中的串口读取不到任何内容。

最佳答案

而不是 usleep(10000); 使用 我们 sleep (1000);

关于c - 在ubuntu 12.04中使用C发送AT命令到teltonika GSM调制解调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22940050/

相关文章:

linux - bash lsof : get pid from one tty to another one

c - 用于所有 VM 的 C 中的数据包嗅探器

c - 将 char 数组类型转换为 char **

c - 如何获取程序调用的第一个参数

android - getsockname 返回-1,errno 是 EBADF?

linux - Git 推送和 pull 文件权限更改

c - 按照 3 条规则(奇数、偶数、排序)显示输出

在 linux 下使用 C 控制 C920 Logitech HD Pro 网络摄像头

java - Eclipse 自动退出退出代码 127

c++ - 重载新运算符以将对象存储在 mmap 文件中