明眼人一看就这是一个什么程序,放出来,给大家看一下,了解一下串口的基本操作!
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "serial.h"
#include
#include
int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
struct termios newtio,oldtio;
if ( tcgetattr( fd,&oldtio) != 0) {
perror("SetupSerial 1");
return -1;
}
bzero( &newtio, sizeof( newtio ) );
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
switch( nBits )
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}
switch( nEvent )
{
case 'O':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'E':
newtio.c_iflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
break;
case 'N':
printf("N\n");
newtio.c_cflag &= ~PARENB;
break;
}
switch( nSpeed )
{
case 2400:
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break;
case 4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case 9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
case 460800:
cfsetispeed(&newtio, B460800);
cfsetospeed(&newtio, B460800);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
if( nStop == 1 )
newtio.c_cflag &= ~CSTOPB;
else if ( nStop == 2 )
newtio.c_cflag |= CSTOPB;
/*如果不是开发终端之类的,只是串口传输数据,而不需要串口来处理,那么使用原始模式(Raw Mode)方式来通讯,设置方式如下:*/
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
newtio.c_oflag &= ~OPOST; /*Output*/
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH);
if((tcsetattr(fd,TCSANOW,&newtio))!=0)
{
perror("com set error");
return -1;
}
printf("set done!\n");
return 0;
}
int open_port(int fd,int comport)
{
char *dev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};
long vdisable;
if (comport==1)
{ fd = open( "/dev/ttyS0", O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror("Can't Open Serial Port");
return(-1);
}
}
else if(comport==2)
{ fd = open( "/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror("Can't Open Serial Port");
return(-1);
}
}
else if (comport==3)
{
fd = open( "/dev/ttyS2", O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror("Can't Open Serial Port");
return(-1);
}
}
if(fcntl(fd, F_SETFL, 0)<0)
printf("fcntl failed!\n");
else
printf("fcntl=%d\n",fcntl(fd, F_SETFL,0));
if(isatty(STDIN_FILENO)==0)
printf("standard input is not a terminal device\n");
else
printf("isatty success!\n");
printf("fd-open=%d\n",fd);
return fd;
}
int readport(int fd,char *buf,int len,int maxwaittime)//读数据,参数为串口,BUF,长度,超时时间
{
int no=0;int rc;int rcnum=len;
struct timeval tv;
fd_set readfd;
tv.tv_sec=maxwaittime/1000; //SECOND
tv.tv_usec=maxwaittime%1000*1000; //USECOND
FD_ZERO(&readfd);
FD_SET(fd,&readfd);
rc=select(fd+1,&readfd,NULL,NULL,&tv);
if(rc>0)
{
while(len)
{
rc=read(fd,&buf[no],1);
if(rc>0)
no=no+1;
len=len-1;
}
//if(no!=rcnum)
//return -1; //如果收到的长度与期望长度不一样,返回-1
//return rcnum; //收到长度与期望长度一样,返回长度
return no;
}
else
{
return -1;
}
return -1;
}
char *AT_Command[]={
/*为可靠,多做几次*/
"AT\r\n", /*0*/
"ATE0\r\n", /*1*/
"AT+CPIN?\r\n", /*2*/
"AT+CREG?\r\n", /*3*/
"AT+CSQ\r\n", /*4*/
"AT+CGREG?\r\n", /*5*/
"AT+CGATT?\r\n", /*6*/
"AT+CLIP=1\r\n", /*7*/
"AT+CNMI=0,1,0,0,0\r\n", /*8*/
"AT+CMGF=0\r\n", /*9*/
"AT+CSCA?\r\n", /*10*/
"AT+CGMI\r\n", /*11*/
"AT+CGMM\r\n", /*12*/
"AT+CGMR\r\n", /*13*/
"ATI9\r\n", /*14*/
"AT+CGSN\r\n", /*15*/
"AT+CIMI\r\n", /*16*/
"AT+CGPRS?\r\n", /*17*/
"AT+MIPCALL=1, \"CMNET\"\r\n", /*18*/
"AT+MIPOPEN=1, 23, \"\", , 0\r\n", /*19*/
"AT+MIPCLOSE=1\r\n", /*20*/
"AT+MIPCALL=0\r\n", /*21*/
"AT+CGPRS?\r",
"AT+CMEE=2\r",
"AT+MIPCALL",
"AT+MIPOPEN",
"AT+MIPSEND",
"AT+MIPCLOSE",
};
int send_command(int argc, char *argv[])
{
int fd,len,t,readlen;
char *cmd;
char *ack;
unsigned int nbytes;
char readbuf[200];
fd=open_port(fd,3);
set_opt(fd,115200, 8,'N', 1);
if(2>argc)
{
printf("less canshu!\n");
return 2;
}
t=atoi(argv[1]);
printf("argv[1] atoi=%d\n",t);
if(t>12)
{
printf("too big number\n");
return 3;
}
cmd=AT_Command[t];
printf("the command is %s,the command len=%d\n",cmd,strlen(cmd));
write(fd,cmd,strlen(cmd));
readlen=readport(fd,readbuf,50,1000);
if(readlen<0)
{
printf("Receive Unsuccess!!\n");
return 5;
}
else
{
readbuf[readlen]='\0';
printf("receive is %s,len=%d\n",readbuf,readlen);
}
close(fd);
return 0;
}
int main(int argc, char *argv[])
{
int i,readlen,fd,start,end;
char *cmd,*pra;
char readbuf[400];
if(2>argc)
{
printf("less canshu!\n");
return 1;
}
pra=argv[1];
/*串口设置*/
fd=open_port(fd,3);
set_opt(fd,115200, 8,'N', 1);
printf("the g24-l is %sing",pra);
if(strcmp(pra, "init") == 0)
{
start=0;
end=17;
}
else if(strcmp(pra, "get") == 0)
{
start=18;
end=18;
printf(" ip from cmnet");
}
/*连接到服务器*/
else if(strcmp(pra, "connect") == 0)
{
start=19;
end=19;
printf(" to the server");
}
/*从服务器断开*/
else if(strcmp(pra, "disconnect") == 0)
{
start=20;
end=20;
printf(" from server");
}
else if(strcmp(pra, "release") == 0)
{
start=21;
end=21;
printf(" ip from cmnet");
}
else if(strcmp(pra, "ioctrl") == 0)
{
start=0xff;
end=0xff;
if(3>argc)
{
printf("less canshu!\n");
return 3;
}
}
else
{
printf("\ninvalid command!\n");
return 2;
}
printf("\n");
for(i=start;i<=end;i++)
{
if((start==0xff)&&(end==0xff))
{
/*我真聪明,哈哈,解决了这个问题,这是字符串连接问题*/
cmd=strcat(argv[2],"\r\n");
printf("test %s",cmd);
}
else
cmd=AT_Command[i];
printf("\nCommand:\n%s",cmd);
write(fd,cmd,strlen(cmd));
memset(readbuf,0x00,200);
readlen=readport(fd,readbuf,200,2000);
if(readlen<0)
{
printf("Receive Unsuccess!!\n");
return 5;
}
else
{
readbuf[readlen]='\0';
printf("Receive:%s",readbuf);
}
}
return 0;
}
有一个疑问,怎么长期占用一个串口,除非你自己主动的释放??怎么完成中断接收数据??像console一样?
阅读(1696) | 评论(3) | 转发(0) |