#include <stdio.h> /*±ê׌ÊäÈëÊä³ö¶šÒå*/
#include <stdlib.h> /*±ê׌º¯Êý¿â¶šÒå*/
#include <unistd.h> /*Unix±ê׌º¯Êý¶šÒå*/
#include <sys/types.h> /**/
#include <sys/stat.h> /**/
#include <fcntl.h> /*ÎÄŒþ¿ØÖƶšÒå*/
#include <termios.h> /*PPSIXÖÕ¶Ë¿ØÖƶšÒå*/
#include <errno.h> /*ŽíÎóºÅ¶šÒå*/
#define TRUE 1
#define FALSE 0
static int serial_fd;
int speed_arr[] = {B230400, B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300};
int name_arr[] = {230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300,
38400, 19200, 9600, 4800, 2400, 1200, 300};
//-----------------------------------------------
void set_speed(int fd,int speed)
{
int i;
int status;
struct termios Opt;
//struct termios oldOpt;
tcgetattr(fd, &Opt);
// printf("serialread.speed is %d\n",serialread.serial_speed);
for( i = 0; i < sizeof(speed_arr)/sizeof(int); i++)
{
if(speed == name_arr[i])
{
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if(status != 0)
perror("tcsetattr fd1");
return;
}
tcflush(fd, TCIOFLUSH);
}
}
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
//struct termios oldoptions;
if(tcgetattr(fd, &options) != 0)
{
perror("SetupSerial 1");
return(FALSE);
}
//options.c_cflag |= (CLOCAL|CREAD);
options.c_cflag &=~CSIZE;
// printf("serialread.databits is %d\n",serialread.databits);
switch(databits)
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return(FALSE);
}
// printf("serialread.parity is %c\n",serialread.parity);
switch(parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK;
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK;
break;
case 'e':
case 'E':
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
break;
case 'S':
case 's': /*as no parity*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr, "Unsupported parity\n");
return(FALSE);
}
// printf("serialread.stopbits is %d\n",serialread.stopbits);
switch(stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return(FALSE);
}
if(parity != 'n'&& parity != 'N')
options.c_iflag |= INPCK;
options.c_cflag &= ~CRTSCTS;
options.c_iflag &= ~IXOFF;
options.c_iflag &= ~IXON;
if(parity != 'n'&& parity != 'N')
options.c_iflag |= INPCK;
tcflush(fd, TCIOFLUSH);
// options.c_iflag |= IGNPAR|ICRNL;
options.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);
// options.c_oflag |= OPOST;
options.c_oflag &= ~OPOST;
options.c_iflag &= ~(ICRNL|IGNCR);
// options.c_iflag &= ~(IXON|IXOFF|IXANY);
options.c_cc[VTIME] = 0; //150; //15 seconds
options.c_cc[VMIN] = 0;
tcflush(fd, TCIFLUSH);
if(tcsetattr(fd, TCSANOW, &options) != 0)
{
perror("SetupSerial 3");
return(FALSE);
}
return(TRUE);
}
int OpenDev(char *Dev)
{
int fd = open(Dev, O_RDWR);
if(-1 == fd)
{
perror("Can't Open Serial Port");
return -1;
}
else
return fd;
}
int serial_init(void)
{
char *Dev = "/dev/ttyUSB0";
int i;
serial_fd = OpenDev(Dev);
printf("Open successfully!\n");
if(serial_fd > 0)
set_speed(serial_fd,115200);
else
{
printf("Can't Open Serial Port!\n");
exit(0);
}
if(set_Parity(serial_fd,8,1,'N') == FALSE)
{
printf("Set parity Error\n");
exit(1);
}
return 0;
}
/**
*
@breif main()
*/
int main(int argc, char **argv)
{
int fd;
int nread;
char buff[512];
serial_init();
int n1=0;
int n11=10;
int nT=14;
int nM=15;
int nL=16;
int nR=14;
while(1)
{
while((nread = read(serial_fd,buff,512))>0)
{
//write(serial_fd,buff,nread);
//printf("\nLen %d\n",nread);
//buff[nread+1]='\0';
//printf("\n%s",buff);
//printf("%c",buff[n1]+'0');
if(nread==20)
{
printf("\nT:%d\tM:%d\tL:%d\n",buff[nT],buff[nM],buff[nL]);
}
if(nread==17)
{
printf("\nR:%d\n",buff[nR]);
}
}
}
close(fd);
exit(0);
}