I have mini2440 and GSM modem that they connected to each other with USB
serial port (ttyUSB0). this is my code in c++ to send AT:
#include
#include /* Standard input/output definitions */
#include /* String function definitions */
#include /* UNIX standard function definitions */
#include /* File control definitions */
#include /* Error number definitions */
#include /* POSIX terminal control definitions */
#include /*To use string type*/
#include
#include
using namespace std;
// Definations
int fd; /* File descriptor for the port */
string wr;
int rd;
char buffer; /* Input buffer */
int openport(void);
void closeport(void);
void configport(void);
string WriteRead(void);
//-------------------------------------
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
openport();
if(fd>-1)
{
configport();
string str=WriteRead();
qDebug(str.c_str());
}
return a.exec();
}
//-------------------------------------
int openport(void)
{
fd=open("/dev/ttyUSB0",O_RDWR|O_NOCTTY|O_NDELAY);
if (fd==-1)
{
perror("open_port: unable to open port /dev/ttyUSB0\n");
return -1;
}
else
{
printf("open_port: succesfully open port /dev/ttyUSB0\n");
fcntl(fd,F_SETFL,0);
return 1;
}
}
//-------------------------------------
void closeport(void)
{
close(fd);
}
//-------------------------------------
void configport(void)
{
struct termios options;
tcgetattr(fd,&options);
cfsetispeed(&options,B9600);
cfsetospeed(&options,B9600);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag &= ~ PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_iflag &= ~(IXON|IXOFF|IXANY);
tcsetattr(fd,TCSANOW,&options);
}
//------------------------------------
string WriteRead(void)
{
char buffer; /* Input buffer */
char *bufptr; /* Current char in buffer */
int nbytes; /* Number of bytes read */
int tries; /* Number of tries so far */
for (tries = 0; tries 0)
{
bufptr += nbytes;
if (bufptr[-1] == '\n' || bufptr[-1] == '\r')
break;
}
/* nul terminate the string and see if we got an OK response */
*bufptr = '\0';
string s(buffer);
if (s.find("OK"))
{
return s;
}
else
return "not answer";
}
}
but after send AT i say if find "OK" set the buffer to string s. then write
string s in output. but it just write AT. there is no OK but my (if(s.find("OK"))) return true. so what happen? why echo AT?
Asked by H.Ghassami
(145 rep)
Nov 10, 2015, 06:42 AM