int new_set_Parity(int fd,int databits,int stopbits,int parity)
{
int i;
int status;
struct termios options;
if(0 != tcgetattr(fd, &options))
{
printf("tcgetattr error errno=%d\n",errno);
return;
}
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag |= CS8 | CREAD;
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_cflag &= ~CSTOPB;
options.c_oflag = 0;
options.c_lflag = 0;
options.c_cflag &= ~CRTSCTS;
// options.c_cflag |= CRTSCTS;
options.c_cc[VTIME] = 150;
options.c_cc[VMIN] = 0;
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
printf("set_parity error. errno=%d\n", errno);
return (FALSE);
}
sleep(2);
return TRUE;
}
int testSet(int fd)
{
int i;
int status;
struct termios options;
speed_t baud;
printf("\n===============test======================\n");
if(0 != tcgetattr(fd, &options))
{
printf("tcgetattr error errno=%d\n",errno);
return;
}
if(B9600 != (baud=cfgetispeed(&options)))
{
printf("testSet : i baud is not equal to B9600, value = %d\n", baud);
}
if(B9600 != (baud=cfgetospeed(&options)))
{
printf("testSet : o baud is not equal to B9600, value = %d\n", baud);
}
if(options.c_cflag & CRTSCTS)
{
printf("testSet : i/o flow control is CRTSCTS\n");
}
else
{
printf("testSet : i/o flow control isnot CRTSCTS\n");
}
if(options.c_cflag & PARENB)
{
printf("testSet : PARENB\n");
}
else
{
printf("testSet : isnot PARENB\n");
}
printf("========================end==================\n");
return 0;
}
int OpenDev(char *Dev)
{
//int fd = open( Dev, O_RDWR|O_NONBLOCK | O_NOCTTY | O_NDELAY);
int fd = open( Dev, O_RDWR|O_NONBLOCK |O_NOCTTY);
//int fd = open( Dev, O_RDWR | O_NOCTTY);
if (-1 == fd)
{
printf("Can't Open Serial Port, errno=%d\n", errno);
return -1;
}
else
{
return fd;
}
}
int OpenComm(const int Port)
{
char *Comm1 = "/dev/tts/1";
int opendev_ret;
if(1 == Port)
{
printf("use %s\n", Comm1);
opendev_ret = OpenDev(Comm1);
}
else if(2 == Port)
{
printf("use %s\n", Comm2);
opendev_ret = OpenDev(Comm2);
}
else
return -1;
if(-1 == opendev_ret)
return -1;
if(FALSE == new_set_Parity(opendev_ret, 8, 1, 'N'))
{
printf("Set Parity Error\n");
return -1;
}
testSet(opendev_ret);
return opendev_ret;
}
int CloseComm(int CommHandle)
{
return close(CommHandle);
}
int ReadComm(int CommHandle, void* pData, int nLength)
{
fd_set set;
struct timeval val;
while(1){
val.tv_sec = 2;
val.tv_usec = 0;
FD_ZERO(&set);
FD_SET(CommHandle, &set);
int ret = select(1, &set, NULL, NULL, &val); // 这个地方,一直都是time out, 刚开始不使用select函数而是直接使用read函数也是出错。
if(ret)
{
printf("start to read data\n");
if(FD_ISSET(CommHandle, &set))
{
return read(CommHandle , pData, nLength);
}
}
else if(ret == 0)
{
printf("select timeout\n");
}
else if(ret == -1)
{
printf("select error : errno = %d, errinfo=%s\n", errno, strerror(errno));
}
}
}
int WriteComm(int CommHandle, const void* pData, int nLength)
{
int writeNum = write(CommHandle , pData, nLength);
if(0 != tcdrain(CommHandle)) //使用tcdrain确保所有写数据发送成功。
{
printf("tcDrain error: errno=%d, errinfo=%s\n", errno, strerror(errno));
}
return writeNum;
}
int main()
{
char data[20] = {0};
int fd = OpenComm(1);
int writeNum = WriteComm(fd, "AT\r\n", 4); //这边返回4, 说明写ok
int readNum = ReadComm(fd, data, 20); // 调用read时, 得不得任何数据
....................
}