I’m using very basic code similar the one given in the examples to control two servos programming in C++. Everything is working fine for a couple of seconds. Can read out the servos and can move them like I want. But suddently communication gets lost.
In such a case I’m getting an error in following code (WriteFile fails) even when input data is correct (have debugged that).
int MaestroConnector_C::WriteBytes(const QByteArray &buf)
{
if ( ! _data->_initialized || _data->_handle == INVALID_HANDLE_VALUE ) {
throw NotInitialized_C();
}
DWORD written = 0;
if ( ! WriteFile(_data->_handle, buf.data(), buf.length(), &written, 0) ) {
Logger.errorStream() << "Error: Unable to open serial port. Error code " << GetLastError(); // This happens often after a few seconds working
return -1;
}
return (int) written;
}
After this happened I can’t even reconnect the servos by re-initialization. I get an ERROR_FILE_NOT_FOUND error in that case in following code:
void MaestroConnector_C::Initialize(const QString &port_name)
{
if ( _data->_initialized ) {
throw AlreadyInitialized_C();
}
_data->_handle = CreateFileA(port_name.toLocal8Bit(), GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if ( _data->_handle == INVALID_HANDLE_VALUE ) {
switch(GetLastError())
{
case ERROR_ACCESS_DENIED:
Logger.errorStream() << "Error: Access denied. Try closing all other programs that are using the device.";
break;
case ERROR_FILE_NOT_FOUND:
Logger.errorStream() << "Error: Serial port not found. Make sure that " << port_name << "is the right port name. Try closing all programs using the device and unplugging the device, or try rebooting.";
break;
default:
Logger.errorStream() << "Error: Unable to open serial port. Error code " << GetLastError();
break;
}
throw InitializationFailed_C();
}
...
}
Device manager shows that given com port is still working. But I get the whole stuff only running if I reconnect the servers by disconnect/connect the USB cable. Any idea what that could be caused by?