send a command out to the GW and receive its response. Returns FALSE on error
| 436 | |
| 437 | // send a command out to the GW and receive its response. Returns FALSE on error |
| 438 | bool GreaseWeazleInterface::sendCommand(Cmd command, void* params, unsigned int paramsLength, Ack& response, unsigned char extraResponseSize) { |
| 439 | std::vector<unsigned char> data; |
| 440 | data.resize(paramsLength + 2); |
| 441 | data[0] = (unsigned char)command; |
| 442 | data[1] = 2 + paramsLength + (extraResponseSize>0 ? 1 : 0); |
| 443 | if ((params) && (paramsLength)) { |
| 444 | #ifdef _WIN32 |
| 445 | memcpy_s(((unsigned char*)data.data()) + 2, paramsLength, params, paramsLength); |
| 446 | #else |
| 447 | memcpy(((unsigned char*)data.data()) + 2, params, paramsLength); |
| 448 | #endif |
| 449 | } |
| 450 | if (extraResponseSize>0) data.push_back(extraResponseSize); |
| 451 | |
| 452 | // Write request |
| 453 | unsigned long written = (unsigned long)m_comPort.write(data.data(), (unsigned int)data.size()); |
| 454 | if (written != data.size()) { |
| 455 | response = Ack::BadCommand; |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | // Read response |
| 460 | unsigned char responseMsg[2]; |
| 461 | unsigned long read = m_comPort.read(&responseMsg, 2); |
| 462 | if (read != 2) { |
| 463 | if (read == 0) { |
| 464 | // Try again |
| 465 | read = m_comPort.read(&responseMsg, 2); |
| 466 | } |
| 467 | if (read != 2) { |
| 468 | response = Ack::BadCommand; |
| 469 | return false; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | response = (Ack)responseMsg[1]; |
| 474 | |
| 475 | // Was it the response to the command we issued? |
| 476 | if (responseMsg[0] != (unsigned char)command) { |
| 477 | response = Ack::BadCommand; |
| 478 | return false; |
| 479 | } |
| 480 | |
| 481 | return true; |
| 482 | } |
| 483 | bool GreaseWeazleInterface::sendCommand(Cmd command, const std::vector<unsigned char>& params, Ack& response, unsigned char extraResponseSize) { |
| 484 | return sendCommand(command, (void*)params.data(), (unsigned int)params.size(), response, extraResponseSize); |
| 485 | } |