wheelsmanx

ArduinoToC++ SerialClass.cpp

May 29th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. #include "SerialClass.h"
  2.  
  3. Serial::Serial(const char *portName)
  4. {
  5. //We're not yet connected
  6. this->connected = false;
  7.  
  8. //Try to connect to the given port throuh CreateFile
  9. this->hSerial = CreateFile(portName,
  10. GENERIC_READ | GENERIC_WRITE,
  11. 0,
  12. NULL,
  13. OPEN_EXISTING,
  14. FILE_ATTRIBUTE_NORMAL,
  15. NULL);
  16.  
  17. //Check if the connection was successfull
  18. if (this->hSerial == INVALID_HANDLE_VALUE)
  19. {
  20. //If not success full display an Error
  21. if (GetLastError() == ERROR_FILE_NOT_FOUND) {
  22.  
  23. //Print Error if neccessary
  24. printf("ERROR: Handle was not attached. Reason: %s not available.\n", portName);
  25.  
  26. }
  27. else
  28. {
  29. printf("ERROR!!!");
  30. }
  31. }
  32. else
  33. {
  34. //If connected we try to set the comm parameters
  35. DCB dcbSerialParams = { 0 };
  36.  
  37. //Try to get the current
  38. if (!GetCommState(this->hSerial, &dcbSerialParams))
  39. {
  40. //If impossible, show an error
  41. printf("failed to get current serial parameters!");
  42. }
  43. else
  44. {
  45. //Define serial connection parameters for the arduino board
  46. dcbSerialParams.BaudRate = CBR_9600;
  47. dcbSerialParams.ByteSize = 8;
  48. dcbSerialParams.StopBits = ONESTOPBIT;
  49. dcbSerialParams.Parity = NOPARITY;
  50. //Setting the DTR to Control_Enable ensures that the Arduino is properly
  51. //reset upon establishing a connection
  52. dcbSerialParams.fDtrControl = DTR_CONTROL_ENABLE;
  53.  
  54. //Set the parameters and check for their proper application
  55. if (!SetCommState(hSerial, &dcbSerialParams))
  56. {
  57. printf("ALERT: Could not set Serial Port parameters");
  58. }
  59. else
  60. {
  61. //If everything went fine we're connected
  62. this->connected = true;
  63. //Flush any remaining characters in the buffers
  64. PurgeComm(this->hSerial, PURGE_RXCLEAR | PURGE_TXCLEAR);
  65. //We wait 2s as the arduino board will be reseting
  66. Sleep(ARDUINO_WAIT_TIME);
  67. }
  68. }
  69. }
  70.  
  71. }
  72.  
  73. Serial::~Serial()
  74. {
  75. //Check if we are connected before trying to disconnect
  76. if (this->connected)
  77. {
  78. //We're no longer connected
  79. this->connected = false;
  80. //Close the serial handler
  81. CloseHandle(this->hSerial);
  82. }
  83. }
  84.  
  85. int Serial::ReadData(char *buffer, unsigned int nbChar)
  86. {
  87. //Number of bytes we'll have read
  88. DWORD bytesRead;
  89. //Number of bytes we'll really ask to read
  90. unsigned int toRead;
  91.  
  92. //Use the ClearCommError function to get status info on the Serial port
  93. ClearCommError(this->hSerial, &this->errors, &this->status);
  94.  
  95. //Check if there is something to read
  96. if (this->status.cbInQue>0)
  97. {
  98. //If there is we check if there is enough data to read the required number
  99. //of characters, if not we'll read only the available characters to prevent
  100. //locking of the application.
  101. if (this->status.cbInQue>nbChar)
  102. {
  103. toRead = nbChar;
  104. }
  105. else
  106. {
  107. toRead = this->status.cbInQue;
  108. }
  109.  
  110. //Try to read the require number of chars, and return the number of read bytes on success
  111. if (ReadFile(this->hSerial, buffer, toRead, &bytesRead, NULL))
  112. {
  113. return bytesRead;
  114. }
  115.  
  116. }
  117.  
  118. //If nothing has been read, or that an error was detected return 0
  119. return 0;
  120.  
  121. }
  122.  
  123.  
  124. bool Serial::WriteData(const char *buffer, unsigned int nbChar)
  125. {
  126. DWORD bytesSend;
  127.  
  128. //Try to write the buffer on the Serial port
  129. if (!WriteFile(this->hSerial, (void *)buffer, nbChar, &bytesSend, 0))
  130. {
  131. //In case it don't work get comm error and return false
  132. ClearCommError(this->hSerial, &this->errors, &this->status);
  133.  
  134. return false;
  135. }
  136. else
  137. return true;
  138. }
  139.  
  140. bool Serial::IsConnected()
  141. {
  142. //Simply return the connection status
  143. return this->connected;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment