Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <Windows.h>
- #include <stdio.h>
- char id;
- HANDLE createPortHandle(const char* port, int baudrate){
- HANDLE portHandle = CreateFile(port, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
- if (portHandle == INVALID_HANDLE_VALUE){
- printf("Cannot open port!\n");
- return INVALID_HANDLE_VALUE;
- }
- DCB params = { 0 };
- params.DCBlength = sizeof(params);
- GetCommState(portHandle, ¶ms);
- params.BaudRate = baudrate;
- params.ByteSize = 8;
- params.StopBits = 0; // 0 == 1
- params.Parity = 0; //StartBit| xxxxxxxx |1or0| StopBit|
- SetCommState(portHandle, ¶ms);
- COMMTIMEOUTS timeouts = { 0 };
- timeouts.ReadIntervalTimeout = 10;
- timeouts.ReadTotalTimeoutConstant = 1;
- timeouts.ReadTotalTimeoutMultiplier = 1;
- timeouts.WriteTotalTimeoutConstant = 50;
- timeouts.WriteTotalTimeoutMultiplier = 50;
- SetCommTimeouts(portHandle, &timeouts);
- return portHandle;
- }
- static bool terminate = false;
- DWORD _stdcall readFunction(void* params) {
- HANDLE portHandle = *(HANDLE*)params;
- char message[256];
- char recvID[1];
- bool msgSent = false;
- DWORD IDbytesRead = 0, MSGbytesRead = 0;
- while (!terminate) {
- if (IDbytesRead == 0) {
- ReadFile(portHandle, recvID, 256, &IDbytesRead, 0);
- //printf("RECVID: %c", recvID[0]);
- }
- if (!msgSent){
- ReadFile(portHandle, message, 256, &MSGbytesRead, 0);
- if (MSGbytesRead > 0){
- if (recvID[0] == id) { printf("\nRecieved message: %s\n", message); }
- msgSent = true;
- }
- }
- if (msgSent) {
- IDbytesRead = 0;
- msgSent = false;
- }
- Sleep(100);
- }
- return 0;
- }
- int main() {
- char portName[100];
- char recvID[1];
- bool IDsent = false, MSGsent = false;
- printf("Enter port: ");
- scanf("%s", portName);
- printf("\nEnter your ID: ");
- scanf("%s", &id);
- printf("MY ID IS: %c\n", id);
- HANDLE portHandle = createPortHandle(portName, 115200);
- if (portHandle == INVALID_HANDLE_VALUE) { return -1; }
- DWORD threadID;
- HANDLE threadHandle = CreateThread(NULL, 0, readFunction, &portHandle, 0, &threadID);
- char message[256];
- DWORD bytesSent;
- do{
- printf("Enter reciever ID: ");
- //fgets(message, 256, stdin);
- scanf("%s", &recvID[0]);
- //printf("int: %d\n", int(recvID[0]) - 48);
- recvID[strlen(recvID)] = '\0';
- printf("Reciever id is %s", recvID);
- if (((recvID[0] == '1' || (recvID[0] == '2') || (recvID[0] == '3'))) && !IDsent){
- IDsent = WriteFile(portHandle, recvID, strlen(recvID) + 1, &bytesSent, 0);
- printf("*ID* Number of bytes sent: %ld\n", bytesSent);
- }
- if (IDsent){
- printf("\nMessage: ");
- scanf("%s", &message);
- message[strlen(message - 1)] = '\0';
- if (strlen(message) > 0) {
- DWORD bytesSent2;
- MSGsent = WriteFile(portHandle, message, strlen(message) + 1, &bytesSent2, 0);
- printf("Number of bytes sent: %ld\n", bytesSent2);
- if(MSGsent){
- IDsent = false;
- printf("MSG sent to %c\n", recvID[0]);
- }
- }
- }
- }while (strcmp(message, "x") != 0);
- terminate = true;
- WaitForSingleObject(threadHandle, INFINITE);
- CloseHandle(threadHandle);
- CloseHandle(portHandle);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement