Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <Windows.h>
  4. #include <stdio.h>
  5.  
  6. char id;
  7.  
  8. HANDLE createPortHandle(const char* port, int baudrate){
  9. HANDLE portHandle = CreateFile(port, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  10.  
  11. if (portHandle == INVALID_HANDLE_VALUE){
  12. printf("Cannot open port!\n");
  13. return INVALID_HANDLE_VALUE;
  14. }
  15.  
  16. DCB params = { 0 };
  17. params.DCBlength = sizeof(params);
  18. GetCommState(portHandle, &params);
  19. params.BaudRate = baudrate;
  20. params.ByteSize = 8;
  21. params.StopBits = 0; // 0 == 1
  22. params.Parity = 0; //StartBit| xxxxxxxx |1or0| StopBit|
  23. SetCommState(portHandle, &params);
  24.  
  25. COMMTIMEOUTS timeouts = { 0 };
  26. timeouts.ReadIntervalTimeout = 10;
  27. timeouts.ReadTotalTimeoutConstant = 1;
  28. timeouts.ReadTotalTimeoutMultiplier = 1;
  29. timeouts.WriteTotalTimeoutConstant = 50;
  30. timeouts.WriteTotalTimeoutMultiplier = 50;
  31. SetCommTimeouts(portHandle, &timeouts);
  32.  
  33. return portHandle;
  34. }
  35.  
  36. static bool terminate = false;
  37.  
  38. DWORD _stdcall readFunction(void* params) {
  39. HANDLE portHandle = *(HANDLE*)params;
  40.  
  41. char message[256];
  42. char recvID[1];
  43. bool msgSent = false;
  44. DWORD IDbytesRead = 0, MSGbytesRead = 0;
  45.  
  46. while (!terminate) {
  47. if (IDbytesRead == 0) {
  48. ReadFile(portHandle, recvID, 256, &IDbytesRead, 0);
  49. //printf("RECVID: %c", recvID[0]);
  50. }
  51. if (!msgSent){
  52. ReadFile(portHandle, message, 256, &MSGbytesRead, 0);
  53. if (MSGbytesRead > 0){
  54. if (recvID[0] == id) { printf("\nRecieved message: %s\n", message); }
  55. msgSent = true;
  56. }
  57. }
  58. if (msgSent) {
  59. IDbytesRead = 0;
  60. msgSent = false;
  61. }
  62. Sleep(100);
  63. }
  64. return 0;
  65. }
  66.  
  67. int main() {
  68. char portName[100];
  69. char recvID[1];
  70. bool IDsent = false, MSGsent = false;
  71.  
  72. printf("Enter port: ");
  73. scanf("%s", portName);
  74.  
  75. printf("\nEnter your ID: ");
  76. scanf("%s", &id);
  77. printf("MY ID IS: %c\n", id);
  78.  
  79. HANDLE portHandle = createPortHandle(portName, 115200);
  80. if (portHandle == INVALID_HANDLE_VALUE) { return -1; }
  81.  
  82. DWORD threadID;
  83. HANDLE threadHandle = CreateThread(NULL, 0, readFunction, &portHandle, 0, &threadID);
  84.  
  85. char message[256];
  86. DWORD bytesSent;
  87. do{
  88. printf("Enter reciever ID: ");
  89. //fgets(message, 256, stdin);
  90. scanf("%s", &recvID[0]);
  91. //printf("int: %d\n", int(recvID[0]) - 48);
  92. recvID[strlen(recvID)] = '\0';
  93. printf("Reciever id is %s", recvID);
  94. if (((recvID[0] == '1' || (recvID[0] == '2') || (recvID[0] == '3'))) && !IDsent){
  95. IDsent = WriteFile(portHandle, recvID, strlen(recvID) + 1, &bytesSent, 0);
  96. printf("*ID* Number of bytes sent: %ld\n", bytesSent);
  97. }
  98. if (IDsent){
  99. printf("\nMessage: ");
  100. scanf("%s", &message);
  101. message[strlen(message - 1)] = '\0';
  102. if (strlen(message) > 0) {
  103. DWORD bytesSent2;
  104. MSGsent = WriteFile(portHandle, message, strlen(message) + 1, &bytesSent2, 0);
  105. printf("Number of bytes sent: %ld\n", bytesSent2);
  106. if(MSGsent){
  107. IDsent = false;
  108. printf("MSG sent to %c\n", recvID[0]);
  109. }
  110.  
  111. }
  112. }
  113. }while (strcmp(message, "x") != 0);
  114.  
  115. terminate = true;
  116. WaitForSingleObject(threadHandle, INFINITE);
  117. CloseHandle(threadHandle);
  118.  
  119. CloseHandle(portHandle);
  120.  
  121. return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement