Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SHORT EXAMPLE -
- while(!this->connected) {
- //Try to connect to the given port throuh CreateFile
- this->hSerial = CreateFile(portName,
- GENERIC_READ | GENERIC_WRITE,
- 0,
- NULL,
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- NULL);
- //Check if the connection was successfull
- if (this->hSerial == INVALID_HANDLE_VALUE)
- {
- //If not success full display an Error
- if (GetLastError() == ERROR_FILE_NOT_FOUND){
- tekstas("ERROR: Handle was not attached. Reason: not available.n");
- }
- else
- {
- tekstas("DEVICE ERROR!!!");
- }
- }
- else
- {
- //If connected we try to set the comm parameters
- DCB dcbSerialParams = { 0 };
- //Try to get the current
- if (!GetCommState(this->hSerial, &dcbSerialParams))
- {
- //If impossible, show an error
- tekstas("failed to get current serial parameters!");
- }
- else
- {
- //Define serial connection parameters for the arduino board
- dcbSerialParams.BaudRate = CBR_115200; //CBR_9600 / 57600 / 115200
- dcbSerialParams.ByteSize = 8;
- dcbSerialParams.StopBits = ONESTOPBIT;
- dcbSerialParams.Parity = NOPARITY;
- //Set the parameters and check for their proper application
- if (!SetCommState(hSerial, &dcbSerialParams))
- {
- tekstas("ALERT: Could not set Serial Port parameters");
- }
- else
- {
- this->connected = true;
- Sleep(ARDUINO_WAIT_TIME);
- //If everything went fine we're connected
- //We wait 2s as the arduino board will be reseting
- }
- }
- }
- Sleep(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement