📄 test.c
字号:
// test.c - main, Receiver
/*///////////////////////////////////////
Protocol Translator Unit
authors:Prudhvi.V & Raju.g,ics ltd.
//////////////////////////////////////*/
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <malloc.h>
#include <crtdbg.h>
#include "test.h"
#include "globalvar.h"
#include "serialdll.h"
//------------------------------------------------------------------------
//
// Prototypes
//
//------------------------------------------------------------------------
int Receiver();
int E2SProtocolTx();
int SendSerial();
BOOL SetComPortParameters(int nPortID, int nSpeed, char chParity, int nDataBits,
int nStopBits, BOOL bXOnXOff, BOOL bHardware);
ULONG Filters[6] = {0, NDIS_PACKET_TYPE_DIRECTED,
NDIS_PACKET_TYPE_MULTICAST,
NDIS_PACKET_TYPE_BROADCAST,
NDIS_PACKET_TYPE_ALL_MULTICAST,
NDIS_PACKET_TYPE_PROMISCUOUS};
TCHAR szbuff[40];
LPADAPTER lpAdapter;
LPPACKET lpPacket,lpPacket1,lpPacket2;
ULONG Filter = 0;
DWORD tid1,tid2,tid3;
HANDLE hThread1,hThread2,hThread3;
int PacketCount = 0;
int i;
unsigned char *data=NULL;
unsigned int packetTx=1,packetRx=1;
int xt1,xt2,xt3,xt4,xt5,xt6;
unsigned int sendGdack=1,readData=0,sendData=0,readAck=0;
struct Delstruct forNack;
unsigned int ACK=0,twice=0,p=0,port1,port2,baud1,baud2;
unsigned long seq[3];
UCHAR Address[6];
HANDLE hComm;
int lines1=0,pf=0;
//------------------------------------------------------------------------
//
// packet -- basic NT packet driver test
//
// TESTS: PacketGetAdapterNames, PacketopenAdapter, PacketSetFilter,
// PacketGetAddress, PacketAllocatePacket, PacketInitPacket,
// PacketReceive, PacketFreePacket, PacketCloseAdapter
//
//------------------------------------------------------------------------
int main(int nargs, char **args)
{
TCHAR buf[256];
TCHAR *Name;
ULONG NameLength = 256;
UINT i;
if (nargs != 2) {
printf("Usage: test [Adapter 0..3]\n");
return 0;
}
if ((atoi(args[1])) > 3) { // atoi() was removed
printf("Usage: test [Adapter 0..3]\n");
return 0;
}
Superloc = 'c';
sendrequest = 1 ;
messageID = 0;
Name = buf;
PacketGetAdapterNames(Name, &NameLength);
switch (atoi(args[1])) {
case 3: Name = &Name[strlen(Name)+1];
if (!strlen(Name))
break;
case 2: Name = (char *) &Name[strlen(Name)+1];
if (!strlen(Name))
break;
case 1: Name = (char *) &Name[strlen(Name)+1];
if (!strlen(Name))
break;
case 0: break;
}
if (strlen(Name) != 0)
{
printf("Adapter Name: %s\n", Name);
}
else {
printf("Adapter not present\n");
return 0;
}
lpAdapter = PacketOpenAdapter(Name);
Filter = Filters[5];
PacketSetFilter(lpAdapter, Filter);
PacketGetAddress(lpAdapter, Address);
printf("Adapter Addr: [");
for (i=0; i<5; i++)
printf("%02x:", Address[i]);
printf("%02x]\n\n", Address[i]);
SetComPortParameters(2,9600,'N',8,1,FALSE,FALSE);
CloseHandle(hComm);
if(Open(2,9600,'n',8,1)){
printf("port Opend");
}
else
{
printf("failed to open\n");
//return 0;
}
hThread1 = CreateThread(0,
0,
(LPTHREAD_START_ROUTINE) Receiver,
0,
0,
&tid1);
hThread2 = CreateThread(0,
0,
(LPTHREAD_START_ROUTINE)E2SProtocolTx ,
0,
0,
&tid2);
SuspendThread(hThread2);
hThread3 = CreateThread(0,
0,
(LPTHREAD_START_ROUTINE) SendSerial,
0,
0,
&tid3);
printf("Hit to terminate...\n\n");
getchar();
xt1=1;
xt2=1;
xt3=1;
PacketCloseAdapter(lpAdapter);
Close();
return 0;
}
//------------------------------------------------------------------------
//
// receiver -- a packet monitor (promiscuous)
//
//------------------------------------------------------------------------
int Receiver()
{
unsigned char buf1[1518];
ULONG Length;
int i;
while (TRUE) {
memset(buf1,(char)NULL,sizeof(buf1));
lpPacket = PacketAllocatePacket();
PacketInitPacket(lpPacket, buf1, 1518);
PacketReceivePacket(lpAdapter, lpPacket, TRUE, &Length);
printf("\nReceived Data\n");
for(i=0;i<Length;i++)
printf("%02x ",buf1[i]);
addE2PBuffer(buf1,Length);
PacketFreePacket(lpPacket);
E2PPacketcount++;
ResumeThread(hThread2);
if(xt1==1){
printf("exit1\n");ExitThread(0);}
} // while (true)
} // receive()
//--------------------------------------------------------------------//
//
// E2SProtocolTx
//
//--------------------------------------------------------------------//
int E2SProtocolTx()
{
struct Delstruct s;
unsigned int i,j,pkcount,tempcount,spoint,pak[21][100],remote_id,cont;
unsigned char localip[5];
while (TRUE)
{
//if(E2PPacketcount >1)
{
s = delE2PBuffer();
E2PPacketcount--; // = E2PPacketcount - 1;
pkcount=0;
tempcount=0;
localip[0] = s.Deldata[30];
localip[1] = s.Deldata[31];
localip[2] = s.Deldata[32];
localip[3] = s.Deldata[33];
localip[4] = '\0';
Superloc='c';
remote_id = 1;
cont = 1;
if(cont == 1)
{
if(Superloc=='c' && s.Dellength > 96)
{
tempcount = s.Dellength / 96;
if(s.Dellength % 96 == 0)
pkcount = tempcount;
else
pkcount = tempcount + 1;
spoint=0;
for(i=1;i<pkcount;i++)
{
for(j=0;j<96;j++)
pak[i][j] = s.Deldata[j+spoint];
spoint +=96; // spoint + 96;
} // for(i=1;i<pkcount;i++)
for(j=0;j<s.Dellength-spoint;j++)
pak[pkcount][j] = s.Deldata[spoint+j];
} // if(Superloc=='c' && s.Dellength > 96)
else if(Superloc=='c' && s.Dellength <= 96)
{
pkcount = 1;
for(i=0;i<s.Dellength;i++)
pak[1][i] = s.Deldata[i];
} // else if(Superloc=='c' && s.Dellength <= 96)
_sleep(100);
if(Superloc=='c' && s.Dellength > 96)
{
for(i=1;i<pkcount;i++)
{
convertEp2Sp(&pak[i][0],96,remote_id,pkcount,i,s.Deldata[29],s.Deldata[33]);
_sleep(100);
}
_sleep(100);
convertEp2Sp(&pak[pkcount][0],s.Dellength-spoint,remote_id,pkcount,pkcount,s.Deldata[29],s.Deldata[33]);
}
else if(Superloc=='c' && s.Dellength <= 96)
{
_sleep(100);
convertEp2Sp(&pak[1][0],s.Dellength,remote_id,1,1,s.Deldata[29],s.Deldata[33]);
}
} // if(cont == 1)
_sleep(100);
if(E2PPacketcount <= 0)
{
SuspendThread(hThread2);
}
} // if(E2PPacketcount >0)
if(xt2==1){printf("exit2\n");ExitThread(0);}
} // while(true)
}//void E2SProtocolTx()
int SendSerial()
{
struct Delstruct p;
unsigned int j; //,i;
while (TRUE)
{
if( P2SPacketcount >0)
{
forNack = p = delP2SBuffer();
P2SPacketcount--;
SendData(p.Deldata,p.Dellength);
printf("\nSend Data:\n");
for(j=0;j<p.Dellength;j++)
printf("%02x ",p.Deldata[j]);
} // if( P2SPacketcount >1)
if(xt3 == 1){ ExitThread(0);}
}// while(1)
}//int SendSerial()
BOOL SetComPortParameters(int nPortID, int nSpeed, char chParity, int nDataBits,
int nStopBits, BOOL bXOnXOff, BOOL bHardware)
{
DCB dcb;
char szPort[15];
wsprintf( szPort, "COM%d", nPortID );
hComm=CreateFile(szPort,GENERIC_READ|GENERIC_WRITE ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
GetCommState(hComm,&dcb);
if (nPortID < 0)
return FALSE;
dcb.BaudRate = nSpeed;
dcb.ByteSize = (BYTE)nDataBits;
// Convert chParity to uppercase:
AnsiUpperBuff(&chParity, 1);
dcb.Parity = (chParity == 'N') ? NOPARITY :
(chParity == 'O') ? ODDPARITY :
(chParity == 'E') ? EVENPARITY :
(chParity == 'M') ? MARKPARITY : SPACEPARITY;
dcb.StopBits = (BYTE)((nStopBits == 1) ? ONESTOPBIT :
(nStopBits == 2) ? TWOSTOPBITS : ONE5STOPBITS);
//dcb.fRtsTimeout= 0;
//dcb.fCtsTimeout = bHardware ? 30 : 0;
//dcb.DsrTimeout = 0;
dcb.fBinary = TRUE;
// dcb.fRtsDisable = FALSE;
dcb.fParity = FALSE;
dcb.fOutxCtsFlow = (BYTE)bHardware;
dcb.fOutxDsrFlow = FALSE;
// dcb.fDummy = 0;
// dcb.fDtrDisable = FALSE;
dcb.fOutX = (BYTE)bXOnXOff;
dcb.fInX = (BYTE)bXOnXOff;
// dcb.fPeChar = FALSE;
dcb.fNull = FALSE;
//dcb.fChEvt = FALSE;
// dcb.fDtrflow = (BYTE)FALSE;
// dcb.fRtsflow = (BYTE)bHardware;
dcb.fDummy2 = 0;
dcb.XonChar = 17;
dcb.XoffChar = 19;
dcb.XonLim = 4096 / 4; // Receive buffer size / 4
dcb.XoffLim = dcb.XonLim;
dcb.EofChar = 26;
dcb.EvtChar = 0;
//dcb.TxDelay = 0;
return !SetCommState(hComm,&dcb);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -