Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- int main(int argc, char* argv[])
- {
- char INBUFFER[2]; // Create Input Buffer
- char PacketOne = '\x01';
- DWORD bytes_read = 1024; // Number of bytes read from port
- DWORD bytes_written = 512; // Number of bytes written to the port
- HANDLE comport = NULL; // Handle COM port
- int bStatus; // Status indicator
- DCB comSettings; // Contains various port settings
- COMMTIMEOUTS CommTimeouts; // Contains various COM timeouts
- // Open COM port
- if ((comport =
- CreateFile("\\\\.\\COM4", // open com4:
- GENERIC_READ | GENERIC_WRITE, // for reading and writing
- 0, // exclusive access
- NULL, // no security attributes
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- NULL)) == INVALID_HANDLE_VALUE)
- {
- printf("CreateFile() funktioniert nicht\r\n"); // error processing code goes here
- system("pause");
- exit(0);
- }
- CommTimeouts.ReadIntervalTimeout = 0; // Timeout in ms
- CommTimeouts.ReadTotalTimeoutMultiplier = 0; // Timeout in ms
- CommTimeouts.ReadTotalTimeoutConstant = 0; // Timeout in ms
- CommTimeouts.WriteTotalTimeoutMultiplier = 0; // Timeout in ms
- CommTimeouts.WriteTotalTimeoutConstant = 65000; // Timeout in ms
- bStatus = SetCommTimeouts(comport,&CommTimeouts); // Set COM timeouts
- if (bStatus != 0)
- {
- printf("SetCommTimeouts() funktioniert nicht\r\n"); // error processing code goes here
- }
- GetCommState(comport, &comSettings); // Getting COM state
- comSettings.BaudRate = 5; // COM setting
- comSettings.StopBits = ONESTOPBIT; // COM setting
- comSettings.ByteSize = 7; // COM setting
- comSettings.Parity = EVENPARITY; // COM setting
- comSettings.fParity = FALSE; // COM setting
- bStatus = SetCommState(comport, &comSettings); // Setting COM state
- if (bStatus == 0)
- {
- printf("SetCommState() funktioniert nicht.\r\n"); // error processing code goes here
- }
- while(!kbhit()) // Solange keine Taste gedrückt wird
- {
- /////Kommunikation
- comSettings.BaudRate = 9600; // COM setting
- bStatus = SetCommState(comport, &comSettings); // Setting COM state
- bStatus = ReadFile(comport,&INBUFFER,2,&bytes_read,NULL);
- printf("Receiving: %d <-> %d\r\n", INBUFFER[0], INBUFFER[1]);
- system("pause");
- bStatus = WriteFile(comport,&PacketOne,1,&bytes_written,NULL);
- printf("Sending: %d\r\n", PacketOne+169);
- system("pause");
- }
- CloseHandle(comport);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment