Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. while(ReadFile(hSerial, LPVOID(szBuff), 1, &dwBytesRead, NULL))
  2. {
  3. cout<<szBuff;
  4. }
  5.  
  6. #include<Windows.h>
  7. #include<stdio.h>
  8. #include<iostream>
  9. using namespace std;
  10.  
  11. void main()
  12. {
  13. HANDLE hSerial;
  14. hSerial = CreateFile(TEXT("COM3"),
  15. GENERIC_READ | GENERIC_WRITE,
  16. 0,
  17. NULL,
  18. OPEN_EXISTING,
  19. FILE_ATTRIBUTE_NORMAL,//FILE_FLAG_OVERLAPPED,
  20. NULL);
  21.  
  22.  
  23.  
  24. if ( hSerial == INVALID_HANDLE_VALUE)
  25. {
  26. printf("Error initializing handler");
  27. }
  28. else
  29. {
  30.  
  31. // Set the parameters of the handler to the serial port.
  32. DCB dcb = {0};
  33.  
  34. dcb.DCBlength = sizeof(dcb);
  35.  
  36. if ( !GetCommState(hSerial, &dcb) )
  37. {
  38. printf("Error setting parameters");
  39. }
  40.  
  41. //FillMemory(&dcb, sizeof(dcb), 0);
  42. dcb.BaudRate = CBR_9600;
  43. dcb.ByteSize = 8;
  44. dcb.StopBits = ONESTOPBIT;
  45. dcb.Parity = NOPARITY;
  46.  
  47. if ( !SetCommState(hSerial, &dcb) )
  48. {
  49. // error setting serial port state.
  50. }
  51.  
  52. // Tell the program not to wait for data to show up
  53. COMMTIMEOUTS timeouts = {0};
  54.  
  55. timeouts.ReadIntervalTimeout = 0;//20;
  56. timeouts.ReadTotalTimeoutConstant = 0;//20;
  57. timeouts.ReadTotalTimeoutMultiplier = 0;//50;
  58. timeouts.WriteTotalTimeoutConstant = 0;//100;
  59. timeouts.WriteTotalTimeoutMultiplier = 0;//100;
  60.  
  61. //if ( !SetCommTimeouts(hSerial, &timeouts) )
  62. //{
  63. // printf("Error setting the timeouts");
  64. //}
  65.  
  66. char szBuff[2] = "";
  67. DWORD dwBytesRead = 0;
  68. int i = 0;
  69. char test[] = "Bn";
  70. // int maxSamples = 10;
  71. // DWORD dwCommStatus;
  72.  
  73. //WriteFile(hSerial, test, 2, &dwBytesRead, NULL);
  74.  
  75. // SetCommMask(hSerial,EV_RXCHAR);
  76. // WaitCommEvent (hSerial, &dwCommStatus, 0);
  77.  
  78. // if (dwCommStatus & EV_RXCHAR)
  79. {
  80. // memset(szBuff,0,sizeof(szBuff));
  81. while(ReadFile(hSerial, LPVOID(szBuff), 1, &dwBytesRead, NULL))
  82. {
  83. cout<<szBuff;
  84. }
  85. cout<<endl;
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92. CloseHandle(hSerial);
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement