最近你才开始接触Linux下的编程,实验室接手了一个小的串口协议的编写,主要是一个握手协议和相关功能的设置:很简单,还没有继续完善。就是一个简单程序而已,以后可能还要修改,不过先在这里记录下来吧! 经过这次编程,串口编程基本上是掌握了,接下来继续向Linux下的编程开进!
- #include <stdio.h>
-
#include <string.h>
-
#include <sys/types.h>
-
#include <errno.h>
-
#include <sys/stat.h>
-
#include <fcntl.h>
-
#include <unistd.h>
-
#include <termios.h>
-
#include <stdlib.h>
-
-
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':
-
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;
-
default:
-
cfsetispeed(&newtio, B9600);
-
cfsetospeed(&newtio, B9600);
-
break;
-
}
-
if( nStop == 1 )
-
newtio.c_cflag &= ~CSTOPB;
-
else if ( nStop == 2 )
-
newtio.c_cflag |= CSTOPB;
-
newtio.c_cc[VTIME] = 0;
-
newtio.c_cc[VMIN] = 1;
-
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);
-
if (-1 == fd)
-
{
-
perror("Can't Open Serial Port");
-
return(-1);
-
}
-
else
-
printf("open ttyS0 .....\n");
-
}
-
else if(comport==2)
-
{
-
fd = open( "/dev/ttyS1", O_RDWR);
-
if (-1 == fd)
-
{
-
perror("Can't Open Serial Port");
-
return(-1);
-
}
-
else
-
printf("open ttyS1 .....\n");
-
}
-
else if (comport==3)
-
{
-
fd = open( "/dev/ttyS2", O_RDWR);
-
if (-1 == fd){
-
perror("Can't Open Serial Port");
-
return(-1);
-
}
-
else
-
printf("open ttyS2 .....\n");
-
}
-
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;
-
}
-
float data_sovle(char buff[])
-
{
-
float a,b,c;
-
if (buff[0]/32%2==0)
-
{
-
a=((float)(buff[0]/32%2*256+buff[1]))/10.0;
-
}
-
else
-
{
-
a=-((float)(buff[0]/32%2*256+buff[1]))/10.0;
-
}
-
printf("Current W %2.2f/s\n",a);
-
if (buff[0]/128%2==0)
-
{
-
b=(float)buff[2]/100;
-
}
-
else
-
{
-
b=-(float)(buff[2])/100;
-
}
-
printf("Current Vx %2.2fm/s\n",b);
-
if (buff[0]/64%2==0)
-
{
-
c=(float)buff[3]/100;
-
}
-
else
-
{
-
c=-(float)(buff[3])/100;
-
}
-
printf("Current Vy %2.2fm/s\n",c);
-
return 0;
-
}
-
int main(void)
-
{
-
int fd;
-
int nread_hand,nwrite_hand,nread,nwrite,i;
-
char buff_hand[1];
-
char buff_handsend[1]={21};
-
char buff_recv_data4[4];
-
char buff_recv_data5[5];
-
char buff_send_data1[5]={24,0,182,100,31};
-
char buff_send_data2[5]={1,144,0,0,31};
-
char buff_send_data3[5]={137,44,194,80,41};
-
char buff_signal[1];
-
char buff_switch[1];
-
char buff_temp[1];
-
if((fd=open_port(fd,1))<0)
-
{
-
perror("open_port error");
-
return;
-
}
-
if((i=set_opt(fd,115200,8,'O',1))<0)
-
{
-
perror("set_opt error");
-
return;
-
}
-
printf("fd=%d\n",fd);
-
sleep(3);
-
-
nread_hand=read(fd,buff_hand,1);
-
/* printf("buff_hand is %d %d\n",nread_hand,buff_hand[0]);
-
nwrite=write(fd,buff_handsend,1);
-
printf("222-%d",buff_handsend[0]);*/
-
if(buff_hand[0]==21)
-
{
-
printf("buf_hand1 %d\n",buff_hand[0]);
-
nwrite_hand=write(fd,buff_handsend,1);
-
printf("buf_hand2 %d\n",buff_handsend[0]);
-
}
-
else
-
{
-
printf("receive hand error\n");
-
return 0;
-
}
-
-
nread=read(fd,buff_recv_data4,4);
-
data_sovle(buff_recv_data4);
-
printf("Input the control mode");
-
scanf("%d",buff_signal);
-
buff_switch[0]=buff_signal[0];
-
while(1)
-
{
-
-
switch(buff_switch[0])
-
{
-
case 31:
-
{
-
printf("Please input control mode ");
-
scanf("%d",buff_temp);
-
printf("%d",buff_temp[0]);
-
switch(buff_temp[0])
-
{
-
case 1:
-
nwrite=write(fd,buff_send_data1,5);
-
nread=read(fd,buff_recv_data5,5);
-
data_sovle(buff_recv_data5);
-
buff_switch[0]=buff_send_data1[4];
-
break;
-
case 2:
-
nwrite=write(fd,buff_send_data2,5);
-
nread=read(fd,buff_recv_data5,5);
-
data_sovle(buff_recv_data5);
-
buff_switch[0]=buff_send_data2[4];
-
break;
-
case 3:
-
nwrite=write(fd,buff_send_data3,5);
-
nread=read(fd,buff_recv_data5,5);
-
data_sovle(buff_recv_data5);
-
buff_switch[0]=buff_send_data3[4];
-
break;
-
}
-
-
}
-
break;
-
case 41:
-
nread=read(fd,buff_recv_data4,4);
-
data_sovle(buff_recv_data4);
-
printf("Please input replay signal ");
-
scanf("%d",buff_signal);
-
buff_switch[0]=buff_signal[0];
-
break;
-
case 51:
-
nread=read(fd,buff_recv_data4,4);
-
data_sovle(buff_recv_data4);
-
printf("Please input replay signal ");
-
scanf("%d",buff_signal);
-
buff_switch[0]=buff_signal[0];
-
break;
-
default:
-
nread=read(fd,buff_recv_data4,4);
-
data_sovle(buff_recv_data4);
-
printf("Please input replay signal ");
-
scanf("%d",buff_signal);
-
buff_switch[0]=buff_signal[0];
-
break;
-
}
-
}
-
-
close(fd);
-
return 0;
-
-
}
还会继续修改,这个程序也是参考别人的修改的,加上了一些附加功能,还有中间编程还有部队的地方,一些细节还要继续修改!
第一次记录自己的Linux学习经历,有什么不好的地方还请大家指正,帮助我进步!编程还真是一个技术活啊,吹牛真是不行的,实践了才知道啊!当然感觉不错,也许这就是开源分享。
阅读(503) | 评论(0) | 转发(0) |