📄 com.c.c
字号:
/**********arm实验时编写的linux串口程序 2006.4.26*********
南京理工大学自动化学院,narihh@sina.com.cn huhao
*********/
#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> /*错误号定义*/
#include <time.h>
#define MAX_TIMER 2
#define PORTSTATETIMER 0
#define TRUE 1
#define FALSE 0
#define BAUTE B9600
#define MAX_BUFF_LEN 512
unsigned char ReadTempBuff[128]; //在内部定义时用static char...或 char *malloc();
int StartFlag=0;//程序启动标志,用于初始化
int CurrentRunMode = 0; //运行模式,用于给其它程序提供控制标识 1-6
int Com1_fd =0;
long int SelfTimer[MAX_TIMER];
int Timer_RunFlag[MAX_TIMER];
struct termios oldios,newios;
int InitCom(int ComPort)
{
char *dev1 = "/dev/ttyS0"; //串口1
int status;
if(ComPort==1)
Com1_fd = open(dev1,O_RDWR|O_NOCTTY|O_NDELAY);
else
return -1;
if(Com1_fd<=0)
{
perror("Can't open Serial port1");
return -1;
}
else //
{
tcgetattr(Com1_fd, &oldios); // 保存端口属性
bzero(&newios,sizeof(newios)); /*清空当前串口设置*/
cfsetispeed(&newios,BAUTE); //设置输入波特
cfsetospeed(&newios,BAUTE); //设置输出波特
newios.c_cflag &= ~CSIZE; /* Mask the character size bits */
newios.c_cflag |= CS8; /* Select 8 data bits */
newios.c_cflag &= ~CSTOPB; //1 stopbit
newios.c_cflag &= ~PARENB; //rity enableno check
newios.c_iflag &= ~INPCK; /* Clear parity checking */
newios.c_cflag &= ~CRTSCTS; //disable hardware flow control;
newios.c_cflag |= CLOCAL|CREAD;
//newios.c_iflag = IGNPAR; /*忽略奇偶校验错的输入数据*/
newios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);/*raw input*/
newios.c_oflag &= ~OPOST; /*raw output*/
newios.c_cc[VTIME] = 0; /*不使用超时等待*/
newios.c_cc[VMIN] = 1; /*read()到一个char时就返回。*/
tcflush(Com1_fd, TCIOFLUSH); //reset com
status = tcsetattr(Com1_fd, TCSANOW, &newios);
if(status!= 0)
{
perror("Failed to SetCom1!\n");
return -1;
}
}
return TRUE;
}
struct Com_Buff
{
unsigned char ReadBuff[MAX_BUFF_LEN];
int RPointer,WPointer;
int RecLenth;
}Com1Buff;
void Read_Com1()
{
int nReadLen,tempLen;
int n,len;
fd_set input;
struct timeval timeout;
FD_ZERO(&input);
FD_SET(Com1_fd, &input);
/* Initialize the timeout structure */
timeout.tv_sec = 1;
timeout.tv_usec = 0;
n = select(Com1_fd+1,&input, NULL,NULL,&timeout);
if (n<0)
{
perror("select failed");
return;
}
else if (n==0)
{
perror("TIMEOUT");
return;
}
else
{
ioctl(Com1_fd,FIONREAD,&len);
if(!len)
{
perror("Communication closed by server\n");
return ;
}
}
memset(ReadTempBuff,0,128);
nReadLen = 0;
tempLen = 0;
nReadLen = read(Com1_fd,ReadTempBuff,len);
ReadTempBuff[nReadLen]=0;
if(nReadLen>0)
{
if(Com1Buff.WPointer+nReadLen<=MAX_BUFF_LEN)
memcpy(&Com1Buff.ReadBuff[Com1Buff.WPointer],ReadTempBuff,nReadLen);
else
{
tempLen = MAX_BUFF_LEN -Com1Buff.WPointer;
memcpy(&Com1Buff.ReadBuff[Com1Buff.WPointer],ReadTempBuff,tempLen);
memcpy(Com1Buff.ReadBuff,&ReadTempBuff[tempLen],nReadLen-tempLen);
}
Com1Buff.WPointer += nReadLen;
Com1Buff.WPointer %= MAX_BUFF_LEN;
Com1Buff.RecLenth += nReadLen;
}
}
int Write_Com1(char *Buff,int SendLenth)
{
write(Com1_fd,Buff,SendLenth);
}
void Send_LinkReport() //向上位机发送联络报文
{
unsigned char tempBuff[10];
tempBuff[0] = 0x68;
tempBuff[1] = 0x04;
tempBuff[2] = 0x04;
tempBuff[3] = 0x68;
tempBuff[4] = 0x01;
tempBuff[5] = 0x80; //联络报文
tempBuff[6] = 0x88;
tempBuff[7] = 0x88;
tempBuff[8] = 0x16;
Write_Com1(tempBuff,9);
}
void proc_report(unsigned char *Buff)
{
if(Buff[4]!=0x01)
return;
if((Buff[5]!=0x80)||(Buff[6]!=0x81))
return;
if(Buff[7]==Buff[8])
{
if((Buff[7]>=1)&&(Buff[7]<=6))
CurrentRunMode = Buff[7];
}
}
void InitTimer()
{
int i;
for(i=0;i<MAX_TIMER;i++)
{
SelfTimer[i] = 0;
Timer_RunFlag[i] = 0;
}
}
int timer_s(int Id,int delay) //second delay
{
long int Seconds;
if(Id>=MAX_TIMER||Id<0)
return -1;
if(Timer_RunFlag[Id]==0)
{
SelfTimer[Id] = time((time_t*)NULL);
Timer_RunFlag[Id] = 1;
}
else //Timer_RunFlag[Id]=1;
{
Seconds = time((time_t*)NULL);
if(Seconds-SelfTimer[Id]>=delay)
Timer_RunFlag[Id]=0;
}
return Timer_RunFlag[Id];
}
/*报文说明
//0x68 len len 0x68 add repType byte0 byte1 0x16,len start from add to byte1
add: 1
reptype:80(状态联络) 81(模式设置)
*/
int main()
{
unsigned char ReportBuff[10];
int ComStatus =0;
int tempLen=0;
if(StartFlag==0)
{
ComStatus = InitCom(1); //COM1
if(ComStatus<=0)
{
perror("Failed to init com!\n");
return -1;
}
memset(&Com1Buff,0,sizeof(struct Com_Buff));
InitTimer();
StartFlag=1;
}
while(1)
{
Read_Com1(); //先接收数据添加到Com_Buff结构
while(Com1Buff.RecLenth>=9)//0x68 len len 0x68 add repType byte0 byte1...byten 0x16,len start from add to byten
{
memset(ReportBuff,0,10);
if(Com1Buff.RPointer+9<=MAX_BUFF_LEN)
memcpy(ReportBuff,&Com1Buff.ReadBuff[Com1Buff.RPointer],9);
else
{
tempLen = MAX_BUFF_LEN - Com1Buff.RPointer;
memcpy(ReportBuff,&Com1Buff.ReadBuff[Com1Buff.RPointer],tempLen);
memcpy(&ReportBuff[tempLen],Com1Buff.ReadBuff,9-tempLen);
}
if((ReportBuff[0]==0x68)&&(ReportBuff[3]==0x68)&&(ReportBuff[1]==ReportBuff[2])&&(ReportBuff[9])==0x16)
{
Com1Buff.RPointer += 9;
Com1Buff.RPointer %= MAX_BUFF_LEN;
Com1Buff.RecLenth -= 9;
proc_report(&ReportBuff[4]);
}
else
{
Com1Buff.RPointer++;
Com1Buff.RPointer %= MAX_BUFF_LEN;
Com1Buff.RecLenth--;
}
}
if(!timer_s(PORTSTATETIMER,3)) //3 seconds /once
Send_LinkReport();
switch(CurrentRunMode) //CurrentRunMode
{
case 1:
//
break;
case 2:
//
break;
case 3:
//
break;
case 4:
//
break;
case 5:
break;
case 6:
//
break;
default:
break;
}
}
tcsetattr(Com1_fd,TCSANOW,&oldios); //restore old com1 property
close(Com1_fd);
exit(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -