Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. // PSVR2_RS485.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #define _CRT_SECURE_NO_WARNINGS
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <windows.h>
  9. #include <iostream>
  10. #include <fstream>
  11.  
  12. using std::ifstream;
  13. using std::ofstream;
  14. using std::ios;
  15.  
  16.  
  17. HANDLE createPortHandle(const char* port, int baudrate)
  18. {
  19. HANDLE portHandle = CreateFile(port, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  20. if (portHandle == INVALID_HANDLE_VALUE)
  21. {
  22. printf("Cannot open port!\n");
  23. return INVALID_HANDLE_VALUE;
  24. }
  25. DCB params = { 0 };
  26. params.DCBlength = sizeof(params);
  27. GetCommState(portHandle, &params);
  28. params.BaudRate = baudrate;
  29. params.ByteSize = 8;
  30. params.StopBits = 0;
  31. params.Parity = 0;
  32. SetCommState(portHandle, &params);
  33.  
  34. COMMTIMEOUTS timeouts = { 0 };
  35. timeouts.ReadIntervalTimeout = 10;
  36. timeouts.ReadTotalTimeoutConstant = 1;
  37. timeouts.ReadTotalTimeoutMultiplier = 1;
  38. timeouts.WriteTotalTimeoutConstant = 50;
  39. timeouts.WriteTotalTimeoutMultiplier = 50;
  40. SetCommTimeouts(portHandle, &timeouts);
  41.  
  42. return portHandle;
  43. }
  44.  
  45. static bool outterminate = false;
  46. DWORD _stdcall readFunction(void* params)
  47. {
  48. HANDLE portHandle = *(HANDLE*)params;
  49.  
  50. char input[2048];
  51. DWORD bytesRead;
  52.  
  53. while (!outterminate)
  54. {
  55.  
  56. ReadFile(portHandle, input, 256, &bytesRead, 0);
  57. if (bytesRead > 0)
  58. {
  59. /*printf("received message: %s\n", output);*/
  60. ofstream outputBuffer("C:/Users/Administrator/Documents/text.txt", ios::out | ios::binary);
  61.  
  62. }
  63. Sleep(100);
  64. }
  65. return 0;
  66. }
  67.  
  68.  
  69.  
  70. int main()
  71. {
  72. printf("enter port: ");
  73. char portName[100];
  74. scanf("%s", portName);
  75.  
  76. HANDLE portHandle = createPortHandle(portName, 115200);
  77. if (portHandle == INVALID_HANDLE_VALUE) {
  78. return -1;
  79. }
  80.  
  81. DWORD threadId;
  82. HANDLE threatHandle = CreateThread(NULL, 0, readFunction, &portHandle, 0, &threadId);
  83.  
  84. ifstream fileBuffer("C:/Users/DELL/Documents/text.txt", ios::in | ios::binary);
  85. ofstream outputBuffer("C:/Users/Administrator/Documents/text.txt", ios::out | ios::binary);
  86. char input[2048];
  87. char output[2048];
  88.  
  89. if (fileBuffer.is_open())
  90. {
  91. fileBuffer.seekg(0, ios::beg);
  92. fileBuffer.getline(input, 2048);
  93. }
  94. outputBuffer.write(input, sizeof(input));
  95.  
  96. char message[256];
  97. do
  98. {
  99. /* fgets(message, 256, stdin);
  100. message[strlen(message) - 1] = '\0'; */
  101.  
  102. /* if (strlen(message) > 0)
  103. {
  104. DWORD bytesSent;
  105. WriteFile(portHandle, message, strlen(message) + 1, &bytesSent, 0);
  106. printf("Number of bytes sent: %ld\n", bytesSent);
  107. }
  108. */
  109. if (strlen(input) > 0)
  110. {
  111. DWORD bytesSent;
  112. WriteFile(portHandle, input, strlen(input) + 1, &bytesSent, 0);
  113. printf("Number of bytes sent: %ld\n", bytesSent);
  114. }
  115. }
  116.  
  117. while (strcmp(input, "x") != 0);
  118. outputBuffer.close();
  119. fileBuffer.close();
  120. outterminate = true;
  121. WaitForSingleObject(threatHandle, INFINITE);
  122. CloseHandle(threatHandle);
  123. CloseHandle(portHandle);
  124. return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement