大家好,我想利用程序来控制GPRS模块,也就是通过程序来发送AT指令以及接受
但是我的程序只发送了一个简单的“AT”,确不能读到回应,想请教一下大家。
通过minicom或者sscom发送AT指令,GPRS模块都能正确的回应,所以应该是程序问题。
但是通过minicom或者sscom调试程序,接受和发送都与我预想的一致,很困惑。
附上出错信息:
Serial Port Speed:115200bps
/*************************/
now 4 bytes has been written to the serial port
Reading timeout
以下是程序清单:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef
enum
{
SERIAL_8N1=0,
SERIAL_7E1=1,
SERIAL_7O1=2,
SERIAL_7S1=3
}serial_format;
const char * serial_port[]={
"/dev/ttyS0",
"/dev/ttyS1",
"/dev/ttyS2",
"/dev/ttyS3",
"/dev/ttyS4",
"/dev/ttyUSB0"
};
int serial_open(int port)
{
int fd = open(serial_port[port], O_RDWR);//|O_NONBLOCK);
if (-1 == fd)
{
perror("Can't Open Serial Port");
exit (-1);
}
else
{
fcntl(fd, F_SETFL, 0);
}
return fd;
}
void serial_format_set(int fd,serial_format format)
{
int status=0;
struct termios options;
if ( tcgetattr( fd,&options) != 0)
{
perror("serial fomat abnormal");
exit (-1);
}
options.c_cflag &= ~CSIZE;
switch (format)
{
case SERIAL_8N1:
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_iflag &= ~(INPCK | ISTRIP);
break;
case SERIAL_7E1:
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS7;
options.c_iflag |= (INPCK | ISTRIP);
break;
case SERIAL_7O1:
options.c_cflag |= PARENB;
options.c_cflag |= PARODD;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS7;
options.c_iflag |= (INPCK | ISTRIP);
break;
case SERIAL_7S1:
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_iflag &= ~INPCK;
options.c_iflag|=ISTRIP;
break;
default:
perror("serial format abnormal");
exit (-1);
}
tcflush(fd,TCIOFLUSH);
status=tcsetattr(fd, TCSANOW, &options);
if (status != 0)
{
perror("tcsetattr format abnormal");
exit(-1);
}
}
void serial_speed_set(int fd,int baudrate)
{
int status;
struct termios options;
/*
* Get the current options for the port
*/
tcgetattr(fd, &options);
/*
* Set the baud rates to 19200
*/
cfsetispeed(&options, baudrate);
cfsetospeed(&options, baudrate);
/*
* Enable the receiver and set local mode
*/
options.c_cflag |= (CLOCAL | CREAD);
/*
* Set the new options for the port
*/
tcflush(fd,TCIOFLUSH);
status=tcsetattr(fd, TCSANOW, &options);
if (status != 0)
{
perror("tcsetattr speed abnormal");
exit(-1);
}
}
void serial_etc_config(int fd)
{
int status=0;
struct termios options;
if ( tcgetattr( fd,&options) != 0)
{
perror("serial etc abnormal");
exit (-1);
}
//no hardware flow control
options.c_cflag &= ~CRTSCTS;
//no soft control
options.c_iflag &=~(IXON | IXOFF | IXANY);
//raw set_input
options.c_lflag &=~(ICANON | ECHO | ECHOE | ISIG);
//no output process , raw set_input
options.c_oflag &=~OPOST;
//min character
options.c_cc[VMIN] = 1;
//time to wait for data
options.c_cc[VTIME] = 15;
tcflush(fd,TCIOFLUSH);
status=tcsetattr(fd, TCSANOW, &options);
if (status != 0)
{
perror("tcsetattr etc abnormal");
exit(-1);
}
}
void serial_config(int fd,int baudrate,serial_format format)
{
serial_speed_set(fd,baudrate);
serial_format_set(fd,format);
serial_etc_config(fd);
}
int main(int argc, char **argv)
{
fd_set set_input;
int fd;
char buff[50]={'\0',};
struct timeval timeout;
int ret_select=0;
int bytes=0;
int write_num=0;
int read_num=0;
int wait=0;
fd = serial_open(5);
serial_config(fd,B115200,SERIAL_8N1);
printf("Serial Port Speed:115200bps\n");
printf("/*************************/\n");
FD_ZERO(&set_input);
FD_SET(fd,&set_input);
timeout.tv_sec=1;
timeout.tv_usec=0;
write_num=write(fd,"AT\r\n",strlen("AT\r\n"));
printf("now %d bytes has been written to the serial port\n",write_num);
tcflush(fd,TCIOFLUSH);
sleep(1);
ret_select=select(fd+1,&set_input,NULL,NULL,&timeout);
if(ret_select<0)
perror("select failed");
else if(ret_select==0)
printf("Reading timeout\n");
else
{
if(FD_ISSET(fd,&set_input))
{
read_num=read(fd,buff,512);
if(read_num>0)
{
buff[read_num]='\0';
printf("read:%s\n",buff);
}
else if(read_num==0)
{
printf("No Data Ready");
}
else
perror("Read");
}
}
close(fd);
exit (0);
}