bool IMI::FlashRead(Port &port, void *buffer, unsigned address, unsigned size) { if (!_connected) return false; if (size == 0) return true; const TMsg *pMsg = SendRet(port, MSG_FLASH, 0, 0, MSG_FLASH, -1, IMICOMM_BIGPARAM1(address), IMICOMM_BIGPARAM2(address), size, 300, 2); if (pMsg == NULL || size != pMsg->parameter3) return false; return RLEDecompress((IMIBYTE*)buffer, pMsg->payload, pMsg->payloadSize, size); }
/** * @brief Connects to the device * * @param d Device handle * @param errBufSize The size of the buffer for error string * @param errBuf The buffer for error string * * @return Operation status */ bool CDevIMI::Connect(PDeviceDescriptor_t d, unsigned errBufSize, TCHAR errBuf[]) { if(_connected) if(!Disconnect(d, errBufSize, errBuf)) return false; _connected = false; memset(&_info, 0, sizeof(_info)); _serialNumber = 0; _parser.Reset(); // check connectivity const TMsg *msg = 0; for(unsigned i=0; i<IMICOMM_CONNECT_RETRIES_COUNT && (!msg || msg->msgID != MSG_CFG_HELLO); i++) { if(Send(d, errBufSize, errBuf, MSG_CFG_HELLO)) msg = Receive(d, errBufSize, errBuf, 200, 0); } if(msg) { if(msg->msgID == MSG_CFG_HELLO) { _serialNumber = msg->sn; } else { // LKTOKEN _@M1414_ = "Device not responsive!" _sntprintf(errBuf, errBufSize, _T("%s"), gettext(_T("_@M1414_"))); return false; } } else if(errBuf[0] == '\0') { // LKTOKEN _@M1414_ = "Device not responsive!" _sntprintf(errBuf, errBufSize, _T("%s"), gettext(_T("_@M1414_"))); return false; } // configure baudrate unsigned long baudRate = d->Com->GetBaudrate(); if(!Send(d, errBufSize, errBuf, MSG_CFG_STARTCONFIG, 0, 0, IMICOMM_BIGPARAM1(baudRate), IMICOMM_BIGPARAM2(baudRate))) return false; // get device info msg = 0; for(int i = 0; i < 5 && (!msg || msg->msgID != MSG_CFG_DEVICEINFO); i++) { if(Send(d, errBufSize, errBuf, MSG_CFG_DEVICEINFO)) msg = Receive(d, errBufSize, errBuf, 400, sizeof(TDeviceInfo)); } if(msg) { if(msg->msgID == MSG_CFG_DEVICEINFO) { if(msg->payloadSize == sizeof(TDeviceInfo)) { memcpy(&_info, msg->payload, sizeof(TDeviceInfo)); } else if(msg->payloadSize == 16) { // old version of the structure memset(&_info, 0, sizeof(TDeviceInfo)); memcpy(&_info, msg->payload, 16); } _connected = true; return true; } } else if(errBuf[0] == '\0') { // LKTOKEN _@M1414_ = "Device not responsive!" _sntprintf(errBuf, errBufSize, _T("%s"), gettext(_T("_@M1414_"))); return false; } return false; }
bool IMI::Connect(Port &port, OperationEnvironment &env) { if (_connected) return true; memset(&_info, 0, sizeof(_info)); _serialNumber = 0; MessageParser::Reset(); // check connectivity if (!Send(port, env, MSG_CFG_HELLO) || env.IsCancelled()) return false; const TMsg *msg = Receive(port, env, 100, 0); if (!msg || msg->msgID != MSG_CFG_HELLO || env.IsCancelled()) return false; _serialNumber = msg->sn; // configure baudrate unsigned baudRate = port.GetBaudrate(); if (baudRate == 0) return false; if (!Send(port, env, MSG_CFG_STARTCONFIG, 0, 0, IMICOMM_BIGPARAM1(baudRate), IMICOMM_BIGPARAM2(baudRate)) || env.IsCancelled()) return false; // get device info for (unsigned i = 0; i < 4; i++) { if (!Send(port, env, MSG_CFG_DEVICEINFO)) continue; if (env.IsCancelled()) return false; const TMsg *msg = Receive(port, env, 300, sizeof(TDeviceInfo)); if (!msg || env.IsCancelled()) return false; if (msg->msgID != MSG_CFG_DEVICEINFO) continue; if (msg->payloadSize == sizeof(TDeviceInfo)) { memcpy(&_info, msg->payload, sizeof(TDeviceInfo)); } else if (msg->payloadSize == 16) { // old version of the structure memset(&_info, 0, sizeof(TDeviceInfo)); memcpy(&_info, msg->payload, 16); } else { return false; } _connected = true; return true; } return false; }