#include "adrport.h"
int OpenAdrPort()
{
char sPortName[64];
sprintf(sPortName, "/dev/ttyUSB0");
printf("sPortName=%s\n", sPortName);
CloseAdrPort();
fd = open(sPortName, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd < 0)
{
printf("open error %d %s\n", errno, strerror(errno));
}
else //else fd<=0 begin
{
struct termios t;
// switch off non-blocking mode
int fdFlags;
if ((fdFlags = fcntl(fd, F_GETFL)) == -1)
{
perror("fdFlags");
return -1;
}
fdFlags &= ~O_NONBLOCK;
if (fcntl(fd, F_SETFL, fdFlags) == -1)
{
perror("switching of non-blocking faild");
return -1;
//throwModemException(_("switching of non-blocking mode failed"));
}
// flush all pending output
tcflush(fd, TCOFLUSH);
// toggle DTR to reset modem
int mctl = TIOCM_DTR;
if (ioctl(fd, TIOCMBIC, &mctl) < 0)
{
perror("clear DTR faild");
return -1;
}
usleep(600000);
if (ioctl(fd, TIOCMBIS, &mctl) < 0)
{
perror("set DTR faild");
return -1;
}
//throwModemException(_("setting DTR failed"));
// get line modes
if (tcgetattr(fd, &t) < 0)
{
perror("tcgetattr device");
return -1;
}
// set line speed
cfsetispeed(&t, B9600);
cfsetospeed(&t, B9600);
// set the device to a sane state
t.c_iflag |= IGNPAR | (false ? IXON | IXOFF : 0);
t.c_iflag &= ~(INPCK | ISTRIP | IMAXBEL |
(false ? 0 : IXON | IXOFF)
| IXANY | IGNCR | ICRNL | IMAXBEL | INLCR | IGNBRK);
t.c_oflag &= ~(OPOST);
// be careful, only touch "known" flags
/*t.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD |
(false ? CRTSCTS : 0 ));
t.c_cflag |= CS8 | CREAD | HUPCL | (false ? 0 : CRTSCTS) | CLOCAL;*/
t.c_cflag = B9600 | CS8 |CREAD | CLOCAL | HUPCL;
t.c_lflag &= ~(ECHO | ECHOE | ECHOPRT | ECHOK | ECHOKE | ECHONL |
ECHOCTL | ISIG | IEXTEN | TOSTOP | FLUSHO | ICANON);
t.c_lflag |= NOFLSH;
t.c_cc[VMIN] = 1;
t.c_cc[VTIME] = 0;
t.c_cc[VSUSP] = 0;
// write back
if(tcsetattr (fd, TCSANOW, &t) < 0)
{
perror("tcsetattr device");
return -1;
}
//throwModemException(stringPrintf(_("tcsetattr device '%s'"),
// device.c_str()));
// the waiting time for writing to the ME/TA is increased with each loop
usleep(600000);
// flush all pending input
tcflush(fd, TCIFLUSH);
fdFlags |= O_NONBLOCK;
if (fcntl(fd, F_SETFL, fdFlags) == -1)
{
perror("switching of non-blocking faild");
return -1;
//throwModemException(_("switching of non-blocking mode failed"));
}
// flush all pending output
tcflush(fd, TCOFLUSH);
}//else fd<=0 end
return fd;
} // end OpenAdrPort
// writes zero terminated string to the serial port
// return code:
// >= 0 = number of characters written
// -1 = write failed
int WriteAdrPort(char* psOutput)
{
int iOut;
if (fd < 1)
{
printf(" port is not open\n");
return -1;
} // end if
iOut = write(fd, psOutput, strlen(psOutput));
if (iOut < 0)
{
printf("write error %d %s\n", errno, strerror(errno));
}
else
{
printf("wrote %d chars: %s\n", iOut, psOutput);
} // end if
return iOut;
} // end WriteAdrPort
// read string from the serial port
// return code:
// >= 0 = number of characters read
// -1 = read failed
int ReadAdrPort(char* psResponse, int iMax)
{
int iIn;
printf("in ReadAdrPort iMax=%d\n", iMax);
if (fd < 1)
{
printf(" port is not open\n");
return -1;
} // end if
strncpy (psResponse, "N/A", iMax<4?iMax:4);
iIn = read(fd, psResponse, iMax-1);
if (iIn < 0)
{
if (errno == EAGAIN)
{
return 0; // assume that command generated no response
}
else
{
printf("read error %d %s\n", errno, strerror(errno));
} // end if
}
else
{
psResponse[iIn<iMax?iIn:iMax] = '\0';
printf("read %d chars: %s\n", iIn, psResponse);
} // end if
return iIn;
} // end ReadAdrPort
// closes the serial port
void CloseAdrPort()
{
// you may want to restore the saved port attributes
if (fd > 0)
{
tcflush(fd, TCIOFLUSH);
close(fd);
} // end if
} // end CloseAdrPort
用QT(底层用的是C)写的基于AT命令Text格式短信
5星 · 超过95%的资源 需积分: 41 185 浏览量
2009-05-21
17:21:10
上传
评论 1
收藏 461KB GZ 举报
mountain2007
- 粉丝: 0
- 资源: 2
- 1
- 2
前往页