📄 mainproc.~c
字号:
int i,p; unsigned char tmp,ch,chk,rchk,buf[2]; if(RDataLength==0)return -1; if(ReadBuffer[0]==0xaa && ReadBuffer[1]==0xdd) {#ifdef __DEBUG__ printf("Received a ACK\n");#endif return 0xaadd; } if(ReadBuffer[0]==0xaa && ReadBuffer[1]==0xee) { WriteToComm(comPort,WDataLength);#ifdef __DEBUG__ printf("Received a NAK\n");#endif return 0xaaee; } WriteBuffer[0]=0xaa; WriteBuffer[1]=0xdd; WriteToComm(comPort,2);/* if(ReadBuffer[0]!=0xaa && ReadBuffer[1]!=0xbb) { WriteBuffer[0]=0xaa; WriteBuffer[1]=0xee; WriteToComm(comPort,2); return -2; }*/#ifdef __DEBUG__ printf("Read Buffer is:\n",Command); for(i=0;i<RDataLength;i++) printf("%x ",ReadBuffer[i]); printf("\nRBuffer END--Command is:%x\n",ReadBuffer[2]);#endif Command=ReadBuffer[2]; rchk=ReadBuffer[RDataLength-1]; p=0; ch=0; chk=ReadBuffer[0]^ReadBuffer[1]^ReadBuffer[2]; for(i=3;i<RDataLength;i++) { tmp=ch; ch=ReadBuffer[i]; chk=chk^ch; if(tmp==0xaa && ch==0xaa) { DataBuffer[p++]=ch; chk=chk^ch; continue; } else if(tmp==0xaa && ch==0xcc) { break; } else DataBuffer[p++]=ch; }/* if(chk!=rchk)//校验和如果错误后的处理,默认是应该查询设备状态还是要求重发? { buf[0]=0xaa; buf[1]=0xee; write(comPort,buf,2); DataLength=p; return 0xccee; }*/ DataLength=p; return 0;}//-----------------------------------------------------------------------// 函数执行正确,返回0 将接收的数据存放在DataBuffer中// 如果包头错误 反回-1// 如果包尾错误 返回-2// 如果校验值错误返回-3//-----------------------------------------------------------------------int Canver6003ToContent(int comPort){ int LID,CID; int i; unsigned char GetCode; unsigned char CalCode; unsigned char EID;#ifdef __DEBUG__ printf("6003 Content:\n"); for(i=0;i<RDataLength;i++) printf("%x ",ReadBuffer[i]); printf("\n");#endif if(ReadBuffer[0]!=P6003H)return -1; LID=ReadBuffer[1]; CID=ReadBuffer[2]; GetCode=ReadBuffer[3+LID-1]; CalCode=P6003H^LID^CID; EID=ReadBuffer[3+LID]; if(EID!=P6003E)return -2; for(i=0;i<LID-1;i++) CalCode=CalCode^ReadBuffer[i+3]; if(CalCode!=GetCode)return -3; for(i=0;i!=LID-1;i++) DataBuffer[i]=ReadBuffer[i+3]; Result=CID; if(Result==0xe0) { WriteToComm(HS3,WDataLength); } DataLength=LID; return 0;}//------------------------------------------------------------------------// 将接收到的数据形成数据报并发送到上一层程序void CLR_RTS(int comPort){ if(HS1==comPort)*(unsigned char*)COM1_MCR |=0x2; else if(HS2==comPort)*(unsigned char*)COM2_MCR |=0x2; else if(HS3==comPort)*(unsigned char*)COM3_MCR |=0x2; else if(HS4==comPort)*(unsigned char*)COM4_MCR |=0x2; else if(HS5==comPort)*(unsigned char*)COM5_MCR |=0x2;}void SET_RTS(int comPort){ if(HS1==comPort)*(unsigned char*)COM1_MCR &=0xfd; else if(HS2==comPort)*(unsigned char*)COM2_MCR &=0xfd; else if(HS3==comPort)*(unsigned char*)COM3_MCR &=0xfd; else if(HS4==comPort)*(unsigned char*)COM4_MCR &=0xfd; else if(HS5==comPort)*(unsigned char*)COM5_MCR &=0xfd;}int GET_RTS(int comPort){ unsigned char ch; if(HS1==comPort)ch=*(unsigned char*)COM1_MCR; if(HS2==comPort)ch=*(unsigned char*)COM2_MCR; if(HS3==comPort)ch=*(unsigned char*)COM3_MCR; if(HS4==comPort)ch=*(unsigned char*)COM4_MCR; if(HS5==comPort)ch=*(unsigned char*)COM5_MCR; ch&=0x02; if(ch==0)return 0; else return 1;}void CLR_DTR(int comPort){ if(HS1==comPort)*(unsigned char*)COM1_MCR |=0x1; else if(HS2==comPort)*(unsigned char*)COM2_MCR |=0x1; else if(HS3==comPort)*(unsigned char*)COM3_MCR |=0x1; else if(HS4==comPort)*(unsigned char*)COM4_MCR |=0x1; else if(HS5==comPort)*(unsigned char*)COM5_MCR |=0x1;}void SET_DTR(int comPort){ if(HS1==comPort)*(unsigned char*)COM1_MCR &=0xfe; else if(HS2==comPort)*(unsigned char*)COM2_MCR &=0xfe; else if(HS3==comPort)*(unsigned char*)COM3_MCR &=0xfe; else if(HS4==comPort)*(unsigned char*)COM4_MCR &=0xfe; else if(HS5==comPort)*(unsigned char*)COM5_MCR &=0xfe;}int GET_DTR(int comPort){ unsigned char ch; if(HS1==comPort)ch=*(unsigned char*)COM1_MCR; if(HS2==comPort)ch=*(unsigned char*)COM2_MCR; if(HS3==comPort)ch=*(unsigned char*)COM3_MCR; if(HS4==comPort)ch=*(unsigned char*)COM4_MCR; if(HS5==comPort)ch=*(unsigned char*)COM5_MCR; ch&=0x01; if(ch==0)return 0; else return 1;}int ExecIOCOFunc(unsigned char cm){ printf("in ioco\n"); switch(cm) { case 0x40:break;//Device Initialize case 0x41: BEGIN switch(DataBuffer[0]) { case 0x18: //上发卡按钮按下 if(DataBuffer[1]==1) PublishCard(AllDevices.CurrentUpPC); break; case 0x19: //下发卡按钮按下 if(DataBuffer[1]==1) PublishCard(AllDevices.CurrentDownPC); break; case 0x1a: if(DataBuffer[1]==0)//手动栏杆机关闭 { Leap(0x42,0x0f,0x00); //前向雨棚灯红灯亮 } break; case 0x1c: //有车 提示按键取卡 if(DataBuffer[1]==0) { VoiceControl(0x08); AllDevices.Cad='Y'; AllDevices.Publish='N'; } else { VoiceControl(0x08); AllDevices.Cad='N'; AllDevices.Publish='N'; } break; } END case 0x42:break; } return 0;}int mTerminalControl(){ int pcbRead,l; unsigned char mKey; l=GetCommDataLength(HS0); pcbRead=read(HS0,&mKey,l); if(pcbRead<=0)return -1; switch(mKey) { case 0x1b: { printf("[1] 使用车检线圈 一车一卡\n"); printf("[2] 不使用车检线圈 一健一卡(缺省)\n"); printf("[3] 循环发卡(测试模式)"); break; } case 0x31: { WMode=0x5a; VoiceControl(0x13); break; } case 0x32: { WMode=0xa5; VoiceControl(0x13); break; } case 0x33: { MLoop=0x5a; VoiceControl(0x13); break; } } return 0;}int PublishCard(int DeviceIndex){ if(WMode==0x5a) { if(AllDevices.Cad!='Y')return -1; if(AllDevices.Publish=='Y') { VoiceControl(0x0d); return -2; } } switch(DeviceIndex) { case 0:Leap(0x42,0x00,0x01);Leap(0x42,0x01,0x00);break; case 1:Leap(0x42,0x01,0x01);Leap(0x42,0x00,0x00);break; case 2:Leap(0x42,0x02,0x01);Leap(0x42,0x03,0x00);break; case 3:Leap(0x42,0x03,0x01);Leap(0x42,0x02,0x00);break; } if(DeviceIndex==AllDevices.CurrentUpPC) { if(inBusy==0x55aa) { VoiceControl(0x0b); if(ReadyCard(HS4,DeviceIndex)==-2) ReadyCard(HS4,SwitchMAPCWithTrue(DeviceIndex)); return 0; } VoiceControl(0x09); OutCard(HS4,DeviceIndex); } else if(DeviceIndex==AllDevices.CurrentDownPC) { if(inBusy==0x55aa) { VoiceControl(0x0b); if(ReadyCard(HS2,DeviceIndex)==-2) ReadyCard(HS2,SwitchMAPCWithTrue(DeviceIndex)); return 0; } VoiceControl(0x09); OutCard(HS2,DeviceIndex); }}int Leap(unsigned char chCommand,unsigned char chAddress,unsigned char chState){ Command=chCommand; DataBuffer[0]=chAddress; DataBuffer[1]=chState; DataLength=2; CanverContentToBSYN(HS5); WriteToComm(HS5,WDataLength); AutoDetect(HS5); ReadFromComm(HS5,0);}int VoiceControl(unsigned char VoiceType){ Command=0x44; DataBuffer[0]=VoiceType; DataLength=1; CanverContentToBSYN(HS1); WriteToComm(HS1,WDataLength);// ShortBlockDetect(HS1);// ReadFromComm(HS1,0);}int InitVoice(){ VoiceControl(0x06);}int InitIOCO(){ Command=0x40; DataLength=0x00; CanverContentToBSYN(HS5); WriteToComm(HS5,WDataLength); ShortBlockDetect(HS5); ReadFromComm(HS5,0); if((DataBuffer[1] & 0x04)!=0) { Leap(0x42,0x0f,0x01); } else { Leap(0x42,0x0f,0x00); } return 0;}int MainInitialize(){ InitializeMAPC(); InitIOCO(); InitVoice(); return 0;}//initialize galobe Variantvoid Initialize(){ long ti; struct tm *tm; DEBUG=-1; //调试状态 Connects=0; //socket连接计数器 AllDevices.CurrentUpPC=0; //默认的上发卡机 AllDevices.CurrentDownPC=2; //默认的下发卡机 AllDevices.BadCardNumber=0; //坏卡数 WMode=0xa5; MLoop=0xa5; RWMode=0x50; time(&ti); tm=localtime(&ti);// printf("%i-%i-%i %i:%i:%i\n",tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);}int main(){ unsigned long ul; int rel; PrintToNetwork("正在初始化系统...\n"); Initialize();/* if(CreateSocket()!=0) { printf("Open Socket Error\n"); return -1; }*/ printf("Socket open ok!!!\n"); close("/dev/ttyS0"); InitilizeComm(0,19200,8,1,-1); InitilizeComm(1,9600,8,1,-1); InitilizeComm(2,9600,8,1,-1); InitilizeComm(3,9600,8,1,-1); InitilizeComm(4,9600,8,1,-1); InitilizeComm(5,9600,8,1,-1); MainInitialize(); ReadyCard(HS4,0); ReadFromComm(HS4,0); ReadyCard(HS2,2); ReadFromComm(HS2,0); Leap(0x42,0x04,0x01); ShortBlockDetect(HS5); ReadFromComm(HS5,0); Leap(0x42,0x06,0x01); ShortBlockDetect(HS5); ReadFromComm(HS5,0); //ListenAllDeviceOnRead_Error(); for(;;) { if(GetCommDataLength(HS2)!=0) { rel=ReadFromComm(HS2,0); if(rel>=0) ExecMAPCFunc(Command,HS2,rel); } if(GetCommDataLength(HS4)!=0) { rel=ReadFromComm(HS4,0); if(rel>=0) ExecMAPCFunc(Command,HS4,rel); } if(GetCommDataLength(HS5)!=0) {// if(inBusy==0x55aa)continue; rel=ReadFromComm(HS5,0); if(rel>=0) ExecIOCOFunc(Command); } }; return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -