#include "common.h"
void SetDebug(unsigned char Debug)
{
DEBUG=Debug;
}
int GetCommDataLength(int Device_Handle)
{
int pcbLength;
pcbLength=0;
ioctl(Device_Handle,FIONREAD,&pcbLength);
return pcbLength;
}
int AutoDetect(int Device_Handle)
{
int r,l;
r=0;
for(;;)
{
l=0;
l=GetCommDataLength(Device_Handle);
if(l==0)
{
if(r<50000)r++;
else return 0;
}
else return l;
}
}
//3妙
int ShortBlockDetect(int Device_Handle)
{
int r,l;
r=0;
for(;;)
{
l=0;
l=GetCommDataLength(Device_Handle);
if(l==0)
{
if(r<25000)r++;
else return 0;
}
else return l;
}
}
void delay(unsigned long dly)
{
while(dly!=0)
{
dly-=1;
dly+=1;
dly-=1;
dly+=1;
dly-=1;
dly+=1;
dly-=1;
};
}
unsigned int FormatIntToInter(unsigned int arg)
{
CharToInt ctis,ctid;
CharToLong ctl;
ctis.i=arg;
ctid.ch2.by0=ctis.ch2.by1;
ctid.ch2.by1=ctis.ch2.by0;
return ctid.i>>16;
}
unsigned int FormatIntToInter2(unsigned int arg)
{
CharToInt ctis,ctid;
CharToLong ctl;
ctis.i=arg;
ctid.ch2.by0=ctis.ch2.by0;
ctid.ch2.by1=ctis.ch2.by1;
return ctid.i>>16;
}
unsigned int FormatInterToInt(unsigned int arg)
{
CharToInt ctis,ctid;
CharToLong ctl;
ctis.i=arg<<16;
ctid.ch2.by0=ctis.ch2.by1;
ctid.ch2.by1=ctis.ch2.by0;
return ctid.i & 0xffff0000;
}
unsigned int FormatInterToInt2(unsigned int arg)
{
CharToInt ctis,ctid;
CharToLong ctl;
ctis.i=arg<<16;
ctid.ch2.by0=ctis.ch2.by0;
ctid.ch2.by1=ctis.ch2.by1;
return ctid.i & 0xffff0000;
}
unsigned char FormatCharToInter(unsigned char arg)
{
byte_bit bs,bd;
bs.ch=arg;
bd.bits.b7=bs.bits.b0;
bd.bits.b6=bs.bits.b1;
bd.bits.b5=bs.bits.b2;
bd.bits.b4=bs.bits.b3;
bd.bits.b3=bs.bits.b4;
bd.bits.b2=bs.bits.b5;
bd.bits.b1=bs.bits.b6;
bd.bits.b0=bs.bits.b7;
return bd.ch;
}
void ListenAllDeviceOnRead_Error()
{
int rel;
fd_set DeviceRead; //设备读准备就绪;
fd_set DeviceError; // 设备出现错误
int DeviceMax;
for(;;)
{
DeviceMax=0;
FD_ZERO(&DeviceRead);
FD_ZERO(&DeviceError);
FD_SET(ListenPort,&DeviceRead);
FD_SET(HS1,&DeviceRead);
FD_SET(HS2,&DeviceRead);
FD_SET(HS4,&DeviceRead);
FD_SET(HS5,&DeviceRead);
if(SocketConnect.Socket_Handle!=-1)
{
FD_SET(SocketConnect.Socket_Handle,&DeviceRead);
FD_SET(SocketConnect.Socket_Handle,&DeviceError);
DeviceMax=SocketConnect.Socket_Handle;
}
if(DeviceMax<ListenPort)DeviceMax=ListenPort;
if(HS1>DeviceMax)DeviceMax=HS1;
if(HS2>DeviceMax)DeviceMax=HS2;
if(HS4>DeviceMax)DeviceMax=HS4;
if(HS5>DeviceMax)DeviceMax=HS5;
rel=select(DeviceMax+1,&DeviceRead,NULL,NULL,NULL);
if(rel<=0)continue;
if(FD_ISSET(ListenPort,&DeviceRead))
{
if(SocketConnect.Socket_Handle==-1)
rel=ListenProcess();
}
if(SocketConnect.Socket_Handle!=-1)
{
if(FD_ISSET(SocketConnect.Socket_Handle,&DeviceRead))
{
rel=recv(SocketConnect.Socket_Handle,&SocketCommand,SocketReadLength,0);
if(rel<=0)
continue;
else
ProcessReceive(SocketConnect.Socket_Handle);
}
}
if(FD_ISSET(HS1,&DeviceRead))
{
ReadFromComm(HS1,GetCommDataLength(HS1));
}
if(FD_ISSET(HS2,&DeviceRead))
{
ReadFromComm(HS2,GetCommDataLength(HS2));
}
if(FD_ISSET(HS4,&DeviceRead))
{
ReadFromComm(HS4,GetCommDataLength(HS4));
}
if(FD_ISSET(HS5,&DeviceRead))
{
ReadFromComm(HS5,GetCommDataLength(HS5));
}
/* if(SocketConnect.Socket_Handle!=-1)
{
if(FD_ISSET(SocketConnect.Socket_Handle,&DeviceError))
{
SocketConnect.Socket_Handle=-1;
printf("Socket Error... Disconnect the Client");
}
}*/
};
}
int CreateSocket()
{
ListenPort=socket(AF_INET,SOCK_STREAM,0);
if(ListenPort==-1)return -1;
Listen_Addr.sin_family=AF_INET;
Listen_Addr.sin_port=htons(LISTENPORT);
Listen_Addr.sin_addr.s_addr=INADDR_ANY;//htonl(INADDR_ANY);
bzero(&Listen_Addr.sin_zero,8);
if(bind(ListenPort,(struct sockaddr*)&Listen_Addr,sizeof(struct sockaddr))==-1)return -2;
if(listen(ListenPort,MAXCONNECTED)==-1)return -3;
fcntl(ListenPort,F_SETFL,O_NONBLOCK);
return 0;
}
int ListenProcess()
{
int Size;
Size=sizeof(struct sockaddr_in);
// SocketConnect.Socket_Handle==-1)
SocketConnect.Socket_Handle=accept(ListenPort,(struct sockaddr*)&SocketConnect.Socket_Info,(socklen_t*)&Size);
if(SocketConnect.Socket_Handle==-1)return -1;
printf("\na Client Connect to the Server Client Connect Count is:%i,Address is:%s\n",Connects,inet_ntop(SocketConnect.Socket_Info.sin_addr));
printf("欢迎你,你今天是:%i-%i-%i 星期:%s 现在时间:%i:%i:%i",2003,12,31,"三",9,18,34);
fcntl(SocketConnect.Socket_Handle,F_SETFL,O_NONBLOCK);
send(SocketConnect.Socket_Handle,"--欢迎你,你今天是: 星期: 现在时间:--",38,0);
return 0;
}
int ProcessReceive(int Socket_Handle)
{
int i,p;
char buf[128];
CharToInt cti;
CharToLong ctl;
SocketCommand.Command=FormatIntToInter(SocketCommand.Command);
switch(SocketCommand.Command)
{
case 0xe003:exit(0);
case 0xe001:
BEGIN
// PrintToNetwork("250 连接将要被关闭\n");
close(SocketConnect.Socket_Handle);
SocketConnect.Socket_Handle=-1;
END
case 0xf710:
BEGIN
DEBUG=FormatIntToInter(SocketCommand.Content);
PrintToNetwork("250 调试状态设置成功\n");
END
case 0xf724:
BEGIN
WMode=0x55;
PrintToNetwork("工作模式设置成功 不使用车辆检测器\n");
END
case 0xf725:
BEGIN
WMode=0x50;
PrintToNetwork("工作模式设置成功 使用车辆检测器\n");
END
case 0xf726:
BEGIN
RWMode=0x55;
PrintToNetwork("工作模式设置成功 不读写卡片\n");
END
case 0xf727:
BEGIN
RWMode=0x50;
PrintToNetwork("工作模式设置成功 读写卡片\n");
END
case 0xf830:
BEGIN
sprintf(&buf,"400 %i号卡机剩余卡数=%i",SocketCommand.Content,PCMachine[SocketCommand.Content].CardNumber);
PrintToNetwork(buf);
END
case 0xf840:
BEGIN
p=0;
for(i=0;i<4;i++)
{
cti.i=FormatInterToInt2(PCMachine[i].CardNumber);
SocketCommand.Buffer[p++]=cti.ch2.by0;
SocketCommand.Buffer[p++]=cti.ch2.by1;
cti.i=FormatInterToInt2(PCMachine[i].MaxCard-PCMachine[i].CardNumber-PCMachine[i].PublishNumber);
SocketCommand.Buffer[p++]=cti.ch2.by0;
SocketCommand.Buffer[p++]=cti.ch2.by1;
ctl.l=PCMachine[i].ID;
SocketCommand.Buffer[p++]=ctl.ch4.by0;
SocketCommand.Buffer[p++]=ctl.ch4.by1;
SocketCommand.Buffer[p++]=ctl.ch4.by2;
SocketCommand.Buffer[p++]=ctl.ch4.by3;
SocketCommand.Buffer[p++]=PCMachine[i].State;
}
SocketCommand.Command=FormatInterToInt(SocketCommand.Command);
send(Socket_Handle,&SocketCommand,sizeof(SocketCommand),0);
END
default:PrintToNetwork("300 错误的命令\n");
}
}
void PrintToNetwork(unsigned char *buff)
{
int i,l;
l=strlen(buff);
printf(buff);
bzero(&SocketCommand,sizeof(SocketCommand));
SocketCommand.Command=0x0011;
for(i=0;i<l;i++)
SocketCommand.Buffer[i]=buff[i];
if(SocketConnect.Socket_Handle!=-1)
send(SocketConnect.Socket_Handle,&SocketCommand,sizeof(SocketCommand),0);
}
//---------------------------------------------------------------------------
// 初始化并打开设备
// 如果设备打开出错,返回 -1
int InitilizeComm(int comPort,int Baud,int DataLength,int StopLength,int Partery)
{
int fd;
struct termios COMOption;
//打开设备
//bzero(COMOption sizeof(struct termios));
switch(comPort)
{
case 0:HS0=open("/dev/ttyS0",O_RDWR | O_NOCTTY);fd=HS0;break;
case 1:HS1=open("/dev/ttyS1",O_RDWR | O_NOCTTY);fd=HS1;break;
case 2:HS2=open("/dev/ttyS2",O_RDWR | O_NOCTTY);fd=HS2;break;
case 3:HS3=open("/dev/ttyS3",O_RDWR | O_NOCTTY);fd=HS3;break;
case 4:HS4=open("/dev/ttyS4",O_RDWR | O_NOCTTY);fd=HS4;break;
case 5:HS5=open("/dev/ttyS5",O_RDWR | O_NOCTTY);fd=HS5;break;
}
if(fd<0)return -1;
//允许接收,并设为本地模式
COMOption.c_cflag |=(CLOCAL | CREAD);
//如果没有制定奇偶校验
if(Partery==-1)
{
COMOption.c_cflag&=~PARENB;
COMOption.c_cflag&=~PARODD;
}
else if(Partery/2==0)//如果指定偶校验
{
COMOption.c_cflag|=PARENB;
COMOption.c_cflag&=~PARODD;
}
else //否则就是奇校验
{
COMOption.c_cflag&=~PARENB;
COMOption.c_cflag|=PARODD;
}
COMOption