Skip to content

Commit

Permalink
Make edits for the pull request
Browse files Browse the repository at this point in the history
There is a problem that may be cause by these changes, that being not
closing the handle, since the loop will run forever, I would have to
close the loop on some sort of exit condition. I am not sure if the
handle closes if the process is killed, if so, then I guess it should be
fine.
  • Loading branch information
mfs16101 committed May 8, 2020
1 parent f3f13b8 commit 2f29396
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ class AHRSInputIOProcessor : public IOProcessor
HANDLE hSerial;
};

#endif
#endif
53 changes: 31 additions & 22 deletions bfs/implementations/breadcrumbs/io_procs/AHRSInputIOProcessor.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
#include "AHRSInputIOProcessor.hpp"


/*
* Return Values:
* 0 : No Errors.
* 1 : Serial port does not exist.
* 2 : Some other error occurred.
* 3 : Error getting state.
* 4 : Error setting serial port state
* 5 : Error occured while setting timoemouts.
* 6 : Error occured while trying to send GoToConfig message.
* 7 : Error occured while trying to send SetOutputConfiguration message.
* 8 : Error occured while trying to send GoToMeasurment message.
*/

int AHRSInputIOProcessor::configAHRS()
{
DCB dcb = {0};
//HANDLE hSerial;

hSerial = CreateFile("COM8", // The COM port to connect to, may have to be changed
GENERIC_READ | GENERIC_WRITE, // depending on what COM port gets selected.
0,
Expand All @@ -18,19 +28,20 @@ int AHRSInputIOProcessor::configAHRS()
{
if(GetLastError() == ERROR_FILE_NOT_FOUND)
{
std::cout << "Serial port does not exist." << std::endl;
return 1; // serial port does not exist.
std::cerr << "Serial port does not exist." << std::endl;
return 1;
}
std::cout << "Some other error occurred." << std::endl;
return 2; // some other error occurred.
std::cerr << "Some other error occurred." << std::endl;
return 2;
}


DCB dcb = { 0 };
dcb.DCBlength = sizeof(dcb);

if (!GetCommState(hSerial, &dcb))
{
std::cout << "Error getting state." << std::endl;
return 3; // error getting state.
std::cerr << "Error getting state." << std::endl;
return 3;
}

dcb.BaudRate = CBR_115200; // defualt baud rate of MT
Expand All @@ -40,8 +51,8 @@ int AHRSInputIOProcessor::configAHRS()

if (!SetCommState(hSerial, &dcb))
{
std::cout << "Error setting serial port state." << std::endl;
return 4; // error setting serial port state
std::cerr << "Error setting serial port state." << std::endl;
return 4;
}

COMMTIMEOUTS timeouts = {0};
Expand All @@ -54,7 +65,7 @@ int AHRSInputIOProcessor::configAHRS()

if (!SetCommTimeouts(hSerial, &timeouts))
{
std::cout << "Error occured while setting timoemouts." << std::endl;
std::cerr << "Error occured while setting timoemouts." << std::endl;
return 5;
}

Expand All @@ -66,7 +77,7 @@ int AHRSInputIOProcessor::configAHRS()

if (!WriteFile(hSerial, wBuff1, 6, &dwBytesWritten, NULL))
{
std::cout << "Error occured while trying to send GoToConfig message." << std::endl;
std::cerr << "Error occured while trying to send GoToConfig message." << std::endl;
return 6;
}

Expand All @@ -75,7 +86,7 @@ int AHRSInputIOProcessor::configAHRS()

if (!WriteFile(hSerial, wBuff2, 10, &dwBytesWritten, NULL))
{
std::cout << "Error occured while trying to send SetOutputConfiguration message." << std::endl;
std::cerr << "Error occured while trying to send SetOutputConfiguration message." << std::endl;
return 7;
}

Expand All @@ -84,7 +95,7 @@ int AHRSInputIOProcessor::configAHRS()

if (!WriteFile(hSerial, wBuff3, 6, &dwBytesWritten, NULL))
{
std::cout << "Error occured while trying to send GoToMeasurment message." << std::endl;
std::cerr << "Error occured while trying to send GoToMeasurment message." << std::endl;
return 8;
}

Expand All @@ -94,7 +105,7 @@ int AHRSInputIOProcessor::configAHRS()

bool AHRSInputIOProcessor::loopCondition()
{
return iterations > 0;
return true;
}

void AHRSInputIOProcessor::loop()
Expand All @@ -107,8 +118,7 @@ void AHRSInputIOProcessor::loop()

if (!ReadFile(hSerial, szBuff, 64, &dwBytesRead, NULL))
{
std::cout << "Error occured while trying to read bytes." << std::endl;
//return 9;
std::cerr << "Error occured while trying to read bytes." << std::endl;
}

int index = -1;
Expand All @@ -131,8 +141,7 @@ void AHRSInputIOProcessor::loop()
double doubleYaw = *reinterpret_cast<double * const>(yawArray);
Attribute attrib("yawAngle", 8, &doubleYaw);
getComms()->sendAttribute(attrib);
iterations--;

if (iterations == 0)
CloseHandle(hSerial);
// I am not sure if it will close autimatically when the process ends, at the moment I guess we just won't close it...
//CloseHandle(hSerial);
}

0 comments on commit 2f29396

Please sign in to comment.