Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. // SHORT EXAMPLE -
  2. while(!this->connected) {
  3.  
  4. //Try to connect to the given port throuh CreateFile
  5. this->hSerial = CreateFile(portName,
  6. GENERIC_READ | GENERIC_WRITE,
  7. 0,
  8. NULL,
  9. OPEN_EXISTING,
  10. FILE_ATTRIBUTE_NORMAL,
  11. NULL);
  12.  
  13. //Check if the connection was successfull
  14. if (this->hSerial == INVALID_HANDLE_VALUE)
  15. {
  16. //If not success full display an Error
  17. if (GetLastError() == ERROR_FILE_NOT_FOUND){
  18.  
  19. tekstas("ERROR: Handle was not attached. Reason: not available.n");
  20.  
  21. }
  22. else
  23. {
  24. tekstas("DEVICE ERROR!!!");
  25. }
  26. }
  27. else
  28. {
  29. //If connected we try to set the comm parameters
  30. DCB dcbSerialParams = { 0 };
  31.  
  32. //Try to get the current
  33. if (!GetCommState(this->hSerial, &dcbSerialParams))
  34. {
  35. //If impossible, show an error
  36. tekstas("failed to get current serial parameters!");
  37. }
  38. else
  39. {
  40. //Define serial connection parameters for the arduino board
  41. dcbSerialParams.BaudRate = CBR_115200; //CBR_9600 / 57600 / 115200
  42. dcbSerialParams.ByteSize = 8;
  43. dcbSerialParams.StopBits = ONESTOPBIT;
  44. dcbSerialParams.Parity = NOPARITY;
  45.  
  46. //Set the parameters and check for their proper application
  47. if (!SetCommState(hSerial, &dcbSerialParams))
  48. {
  49. tekstas("ALERT: Could not set Serial Port parameters");
  50. }
  51. else
  52. {
  53. this->connected = true;
  54. Sleep(ARDUINO_WAIT_TIME);
  55. //If everything went fine we're connected
  56.  
  57. //We wait 2s as the arduino board will be reseting
  58. }
  59. }
  60. }
  61.  
  62. Sleep(100);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement