Advertisement
Guest User

Untitled

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