#include"head.h"
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<errno.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistad.h>
#include<termios.h>
#include<stdlib.h>
#include<sys/select.h>
#include<sys/mman.h>
#include"init.h"
#define COM2 "/dev/ttyS1"
#define OPEN_FLAGS (O_RDWR | O_NOCTTY /*| O_NONBLOCK*/)
typedef enum
{
NON, /*无校验*/
ODD, /*偶校验*/
EVEN,/*奇校验*/
}cal_t;
/*打开串口*/
static int open_com(char *comname)
{
int fd = 0;
fd = open(comname, OPEN_FLAGS);
if (-1 == fd)
{
perror("Failed to open");
return -1;
}
printf("%s is open(%d)\n", comname, fd);
return fd;
}
/*设置串口*/
static int set_com(int fd, int speed, int bits, cal_t cal, int stop )
{
struct termios curtio;
memset(&curtio, 0, sizeof(curtio));
/*取得串口已有的属性*/
if (0 != tcgetattr(fd, &curtio))
{
perror("Failed to tcgetattr");
return -1;
}
/*设置输入输出波特率*/
cfsetispeed(&curtio, speed);
cfsetospeed(&curtio, speed);
/*设置为原始模式*/
curtio.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG | ECHOE | ECHOK | ECHONL);
curtio.c_iflag &= ~(BRKINT | IUCLC | ICRNL | INLCR | IGNCR);
/*激活相应选项*/
curtio.c_cflag |= CLOCAL | CREAD;
/*设置数据位*/
curtio.c_cflag &= ~CSIZE;
curtio.c_cflag |= bits;
/*设置校验位*/
if (ODD == cal)
{
curtio.c_iflag |= (INPCK | ISTRIP);
curtio.c_cflag |= PARENB;
curtio.c_cflag |= PARODD;
}
else if(EVEN == cal)
{
curtio.c_iflag |= (INPCK | ISTRIP);
curtio.c_cflag |= PARENB;
curtio.c_cflag &= ~PARODD;
}
else
{
curtio.c_cflag &= ~PARENB;
}
/*设置停止位*/
if (2 == stop)
{
curtio.c_cflag |= CSTOPB;
}
else
{
curtio.c_cflag &= ~CSTOPB;
}
/*设置最少字符等待时间*/
curtio.c_cc[VTIME] = 0;
curtio.c_cc[VMIN] = 0;
/*清空缓冲*/
tcflush(fd, TCIOFLUSH);
/*设置新串口属性*/
if (0 != tcsetattr(fd, TCSANOW, &curtio))
{
perror("Failed to tcgetattr");
return -1;
}
printf("set done!\n");
return 0;
}
int main (void)
{
int fd = -1;
int ret = 0;
int len = 0;
fd_set rset = {0};
fd_set wset = {0};
char buf[128]={0};
// sell_t sell = {0};
queue_t *add_r = NULL;
/*打开串口*/
fd = open_com(COM2);
//初始化队列
add_r = init_my_queue();
printf("addr=%p\n", add_r);
if (-1 == fd)
{
printf("open_com failed!\n");
ret = -1;
goto _out;
}
/*设置串口参数*/
if (0 != set_com(fd, B115200, CS8, NON, 1))
{
printf("set_com failed!\n");
ret = -1;
goto _out;
}
while(1)
{
FD_ZERO(&rset);
FD_SET(fd, &rset);
FD_ZERO(&wset);
FD_SET(fd, &wset);
//printf("select now....\n");
ret = select(fd + 1, &rset, &wset, NULL, NULL);
if ((-1 == ret) && (EINTR == errno))
continue;
if (-1 == ret)
{
perror("select failed");
goto _out;
}
lseek(fd, 0, SEEK_SET);
if (FD_ISSET(fd, &rset))/*串口是否可读*/
{
memset(buf, 0, sizeof(buf));
printf("read now...\n");
while((-1 == (ret = read(fd, buf, sizeof(buf))))
&& (errno == EINTR));
if (-1 == ret)
{
perror("read failed");
goto _out;
}
printf("read from comm(%d): %s\n", ret, buf);
if (FD_ISSET(fd, &wset))/*串口可写*/
{
memset(buf, 0, sizeof(buf));
//获取长度
len = whole_sell(buf, strlen(buf));
//解码
// mystrtok (&mysell, buf, len);
// if (-1 == enqueue(&mysell, add_r)
// {
// perror("enqueue failed");
// goto _out;
// }
printf("write to comm(%d): %s\n", ret, buf);
}
}
}
_out:
return ret;
}