include
include
include
include
include
include
include
include
include
include
define PA_dev_ttys0 0x00
define PA_speed 9600
define PA_databits 8
define PA_parity ‘N’
define PA_stopbits 1
define PA_ReadLength 6
int speed_arr[]={B115200,B38400, B19200, B9600, B4800, B2400, B1200, B300};
int name_arr[] = {115200,38400, 19200, 9600, 4800, 2400, 1200, 300};
int OpenDev(int num) //打开串口
{
char *Dev[11]={“/dev/ttyS0”,”/dev/ttyS1”,”/dev/ttyS2”};
int fd = open(Dev[num], O_RDWR ); //| O_NOCTTY | O_NDELAY
if (-1 == fd)
{
perror(“Can’t Open Serial Port”);
return -1;
}
else
return fd;
}
int set_speed(int fd, int speed)
{
struct termios options;
int i,status;
if(tcgetattr(fd, &options) != 0)
{
perror(“SetupSerial 1”);
return -1;
}
//设置波特率
for ( i= 0; i < sizeof(speed_arr)/sizeof(int); i++)
{
if (speed == name_arr[i])
{
printf("i=%d\n",i);
tcflush(fd, TCIOFLUSH);
cfsetispeed(&options, speed_arr[i]);
cfsetospeed(&options, speed_arr[i]);
status = tcsetattr(fd, TCSADRAIN, &options);
if(status !=0)
{
printf("set_speed error\n");
return -1;
}
tcflush(fd,TCIOFLUSH);
}
}//for
return 0;
}
/********校验位和数据位的设置********************
无校验 8位 Option.c_cflag &= ~PARENB;
Option.c_cflag &= ~CSTOPB;
Option.c_cflag &= ~CSIZE;
Option.c_cflag |= ~CS8;
奇校验(Odd) 7位 Option.c_cflag |= ~PARENB;
Option.c_cflag &= ~PARODD;
Option.c_cflag &= ~CSTOPB;
Option.c_cflag &= ~CSIZE;
Option.c_cflag |= ~CS7;
偶校验(Even) 7位 Option.c_cflag &= ~PARENB;
Option.c_cflag |= ~PARODD;
Option.c_cflag &= ~CSTOPB;
Option.c_cflag &= ~CSIZE;
Option.c_cflag |= ~CS7;
Space校验 7位 Option.c_cflag &= ~PARENB;
Option.c_cflag &= ~CSTOPB;
Option.c_cflag &= &~CSIZE;
Option.c_cflag |= CS8;
*********************************************************/
//设置串口波特率,数据位,校验位,停止位,
//fd int 串口标识符
//databits short 7 或 8
//parity char 校验类型:N,E,O,S
//stopbits short 1 或 2
int setDev(int fd, int databits, char parity, int stopbits)
{
struct termios options;
int i;
if(tcgetattr(fd, &options) != 0)
{
printf(“setDev error\n”);
return -1;
}
options.c_cflag &= ~CSIZE;
//设置数据位
switch (databits)
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
printf("Unsupported data size\n");
return -1;
}//switch (databits)
//设置校验类型
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* 关闭输入奇偶校验 */
break;
case 'o':
case 'O':
options.c_cflag |= PARODD; /* 输入/输出奇校验*/
options.c_cflag |= 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;
options.c_iflag |= INPCK;
break;
default:
printf("Unsupported parity\n");
return -1;
}//switch (parity)
//设置停止位
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;//设置1个停止位
break;
case 2:
options.c_cflag |= CSTOPB; //设置2个停止位
break;
default:
printf("Unsupported stop bits\n");
return -1;
}//switch (stopbits)
options.c_cflag &= ~CRTSCTS; //no hard flow control
options.c_cflag |=(CLOCAL | CREAD); //enabled receiver
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); //自动添加回车
options.c_oflag &= ~OPOST;
options.c_oflag &= ~(ONLCR | OCRNL);
options.c_iflag &= ~(ICRNL | INLCR);
options.c_iflag &= ~(IXON | IXOFF | IXANY); //no sw flow control
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 0; /* 延时15 seconds*/
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
if (tcsetattr(fd, TCSANOW, &options) != 0)
{
printf("SetupSerial 3");
return -1;
}
return 0;
}
void main()
{
unsigned char buf1[]={0x7E,0x06,0x00,0x00,0x00,0x7E};
//unsigned char buf1[]={0x7E,0x04,0x00,0xC8,0x00,0x7E};
unsigned char buf2[20];
int i,fd,wnum,rnum,res,num;
pthread_t thread;
num = 1;
fd =open(“/dev/ttyS0”, O_RDWR | O_NOCTTY | O_NDELAY);
set_speed(fd, PA_speed);
if((setDev(fd, PA_databits,PA_parity, PA_stopbits))==-1)
{
printf(“setDev error!\n”);
}
printf("fd=%d\n",fd);
wnum=write(fd,buf1,6);
printf("wnum=%d\n",wnum);
for(i=0;i<wnum;i++)
{
printf("0x%2.2x\t",buf1[i]);
}
printf("\n");
sleep(1);
fcntl(fd,F_SETFL,0);
//fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
rnum = -1;
while(rnum<0)
{
rnum=read(fd,buf2,20);
}
printf("rnum=%d\n",rnum);
for(i=0;i<rnum;i++)
{
printf("0x%2.2x\t",buf2[i]);
}
printf("\n");
}