Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <stdio.h>
- #define BUFFER_SIZE 1024
- char LastCmd[BUFFER_SIZE];
- char DataCharacters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
- bool IncreaseChar(char* ptr, DWORD index)
- {
- char curchar[2];
- curchar[0] = ptr[index];
- curchar[1] = 0;
- if (curchar[0] == DataCharacters[sizeof(DataCharacters) - 2])
- {
- if (index == 0)
- {
- ptr[index] = DataCharacters[0];
- return false;
- }
- else
- {
- ptr[index] = DataCharacters[0];
- return IncreaseChar(ptr, index - 1);
- }
- }
- else
- {
- char* temp = strstr(DataCharacters, curchar);
- ptr[index] = *(temp + 1);
- }
- return true;
- }
- bool GenerateNextCommand(char* ptr, DWORD* size)
- {
- if (*size == 0)
- {
- ptr[*size] = DataCharacters[0];
- *size = 1;
- ptr[*size] = 0;
- }
- if (!IncreaseChar(ptr, *size - 1))
- {
- if (*size < BUFFER_SIZE - 1)
- {
- ptr[*size] = DataCharacters[0];
- *size += 1;
- ptr[*size] = 0;
- return IncreaseChar(ptr, *size - 1);
- }
- else
- {
- return false;
- }
- }
- return true;
- }
- bool SysconError(char* ptr, DWORD size)
- {
- return !memcmp(ptr, "E:4E:NG F0000003", size - 1);
- }
- char GenerateChecksum(char* ptr,DWORD size)
- {
- char checksum = 0;
- for(DWORD i = 0; i < size; i++)
- {
- checksum += ptr[i];
- }
- return checksum;
- }
- void SendCommand(HANDLE Comport, char* ptr, DWORD size)
- {
- char Buffer[1024];
- DWORD BytesWritten;
- _OVERLAPPED OverlapDesc;
- memset(&OverlapDesc, 0, sizeof(_OVERLAPPED));
- sprintf_s(Buffer, 1024, "C:%02X:%s\r\n", GenerateChecksum(ptr, size) & 0xFF, ptr);
- size = strlen(Buffer);
- strcpy_s(LastCmd, BUFFER_SIZE, Buffer);
- WriteFile(Comport, Buffer, size, &BytesWritten, &OverlapDesc);
- }
- void main(int argc, char *argv[])
- {
- char Buffer[BUFFER_SIZE];
- DWORD BytesRead;
- char Command[BUFFER_SIZE];
- DWORD CommandSize;
- HANDLE Comport;
- _DCB Config;
- COMMTIMEOUTS Timeouts;
- bool UserCommands;
- OVERLAPPED OverlapDesc;
- memset(&OverlapDesc, 0, sizeof(OverlapDesc));
- UserCommands = false;
- if (argc > 2) {
- if (!strcmp(argv[2],"UserCmd")) {
- UserCommands = true;
- printf("User commands active.\n");
- } else {
- sprintf_s(Command, BUFFER_SIZE, argv[2]);
- }
- } else {
- sprintf_s(Command, BUFFER_SIZE, "A");
- }
- if(!UserCommands)
- {
- printf("Starting at:%s\n\n",Command);
- CommandSize = strlen(Command);
- }
- Comport = CreateFileA(
- argv[1],
- GENERIC_READ | GENERIC_WRITE,
- 0,
- NULL,
- OPEN_EXISTING,
- 0,
- //FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING ,
- NULL
- );
- if (Comport == INVALID_HANDLE_VALUE)
- {
- printf("Could not open comport\n");
- return;
- }
- OverlapDesc.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
- Timeouts.ReadIntervalTimeout = 1;
- Timeouts.ReadTotalTimeoutMultiplier = 1;
- Timeouts.ReadTotalTimeoutConstant = 1;
- Timeouts.WriteTotalTimeoutMultiplier = 1;
- Timeouts.WriteTotalTimeoutConstant = 1;
- if (!SetCommTimeouts(Comport, &timeouts)) {
- printf("Could not set timeouts.\n");
- return;
- }
- if (GetCommState(Comport, &Config) == 0)
- {
- printf("Get configuration port failed.\n");
- return;
- }
- Config.DCBlength = 0x1c;
- Config.BaudRate = 57600;
- Config.StopBits = 0;
- Config.Parity = 0;
- Config.ByteSize = 8;
- Config.fDtrControl = 0;
- Config.fRtsControl = 0;
- if (SetCommState(Comport, &Config) == 0)
- {
- printf("Set configuration port failed.\n");
- return;
- }
- printf("listening\n");
- memset(Buffer,0,BUFFER_SIZE);
- //while (ReadFile(Comport, Buffer, BUFFER_SIZE, &BytesRead, &OverlapDesc))
- while (ReadFile(Comport, Buffer, BUFFER_SIZE, &BytesRead, NULL))
- {
- if (UserCommands)
- {
- if (BytesRead != 0)
- {
- printf("%s\n",Buffer);
- }
- printf("inp:");
- gets_s(Command, BUFFER_SIZE);
- SendCommand(Comport, Command, strlen(Command));
- printf(LastCmd);
- Sleep(30);
- memset(Buffer,0,BytesRead);
- continue;
- }
- if (BytesRead == 0)
- {
- SendCommand(Comport, Command, CommandSize);
- Sleep(30);
- continue;
- }
- if (!SysconError(Buffer, BytesRead))
- {
- printf("Command:%s", LastCmd);
- printf("Response:%s\n\n", Buffer);
- }
- if (GenerateNextCommand(Command, &CommandSize))
- {
- SendCommand(Comport, Command, CommandSize);
- Sleep(30);
- }
- else
- {
- return;
- }
- memset(Buffer,0,BytesRead);
- }
- printf("%08X\n",GetLastError());
- getchar();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement