/* * The Spectral LMM5 has a silly difference between USB and serial communication: * Commands can be sent straight to USB. Commands to the serial port need to converted in some kind of weird ASCI: The command "0x1A0xFF0x000x12<CR>" becomes "1AFF0012<CR>". Presumably, the same weird conversion takes place on the way back. We handle this translation in this function */ int SpectralLMM5Interface::ExecuteCommand(MM::Device& device, MM::Core& core, unsigned char* buf, unsigned long bufLen, unsigned char* answer, unsigned long answerLen, unsigned long& read) { int ret; if (portType_ == MM::SerialPort) { std::string serialCommand; char tmp[3]; tmp[2] = 0; for (unsigned long i=0; i<bufLen; i++) { sprintf(tmp, "%.2x", buf[i]); serialCommand += tmp; } ret = core.SetSerialCommand(&device, port_.c_str(), serialCommand.c_str(), "\r"); } else // check for USB port { ret = core.WriteToSerial(&device, port_.c_str(), buf, bufLen); } if (ret != DEVICE_OK) return ret; if (portType_ == MM::SerialPort) { char strAnswer[128]; read = 0; ret = core.GetSerialAnswer(&device, port_.c_str(), 128, strAnswer, "\r"); if (ret != DEVICE_OK) return ret; std::ostringstream os; os << "LMM5 answered: " << strAnswer << " Port status: " << ret; core.LogMessage(&device, os.str().c_str(), true); // 'translate' back into numbers: std::string tmp = strAnswer; for (unsigned int i=0; i < tmp.length()/2; i++) { char * end; long j = strtol(tmp.substr(i*2,2).c_str(), &end, 16); answer[i] = (unsigned char) j; read++; } } else if (portType_ == MM::HIDPort) { // The USB port will attempt to read up to answerLen characters ret = core.ReadFromSerial(&device, port_.c_str(), answer, answerLen, read); if (ret != DEVICE_OK) return ret; /* // Uncomment for debugging (although port should give the same info) std::ostringstream os; os << "LMM5 answered: " << std::hex << std::setfill('0'); for (unsigned int i=0; i < read; i++) os << std::setw(2) << (unsigned int) answer[i] << " "; core.LogMessage(&device, os.str().c_str(), true); */ } return DEVICE_OK; }
/* * The Spectral LMM5 has a silly difference between USB and serial communication: * Commands can be sent straight to USB. Commands to the serial port need to converted in some kind of weird ASCI: The command "0x1A0xFF0x000x12<CR>" becomes "1AFF0012<CR>". Presumably, the same weird conversion takes place on the way back. We handle this translation in this function */ int SpectralLMM5Interface::ExecuteCommand(MM::Device& device, MM::Core& core, unsigned char* buf, unsigned long bufLen, unsigned char* answer, unsigned long answerLen, unsigned long& read) { int ret; if (portType_ == MM::SerialPort) { std::string serialCommand; char tmp[3]; tmp[2] = 0; for (unsigned long i=0; i<bufLen; i++) { sprintf(tmp, "%.2x", buf[i]); serialCommand += tmp; } ret = core.SetSerialCommand(&device, port_.c_str(), serialCommand.c_str(), "\r"); } else // check for USB port { unsigned char c[2]; c[0]=0x01; c[1]=0x02; ret = core.WriteToSerial(&device, port_.c_str(), c, 2); //ret = core.WriteToSerial(&device, port_.c_str(), buf, bufLen); } if (ret != DEVICE_OK) return ret; if (portType_ == MM::SerialPort) { char strAnswer[128]; read = 0; ret = core.GetSerialAnswer(&device, port_.c_str(), 128, strAnswer, "\r"); if (ret != DEVICE_OK) return ret; std::ostringstream os; os << "LMM5 answered: " << strAnswer << " Port status: " << ret; core.LogMessage(&device, os.str().c_str(), true); // 'translate' back into numbers: std::string tmp = strAnswer; for (unsigned int i=0; i < tmp.length()/2; i++) { char * end; long j = strtol(tmp.substr(i*2,2).c_str(), &end, 16); answer[i] = (unsigned char) j; // printf("c:%x i:%u j:%ld\n", answer[i], i, j); read++; } } else // TODO: check that we have a USB port { // The USB port will attempt to read up to answerLen characters ret = core.ReadFromSerial(&device, port_.c_str(), answer, answerLen, read); if (ret != DEVICE_OK) return ret; std::ostringstream os; os << "LMM5 answered: "; for (unsigned int i=0; i < read; i++) os << std::hex << answer[i]; os << std::endl; core.LogMessage(&device, os.str().c_str(), true); } return DEVICE_OK; }
/* * The LMM5 USB HID commands are a sequence of binary bytes with no terminator. * The serial commands are the same bytes formatted as an ASCII hex string, two * characters per byte, and terminated with a CR; e.g.: * USB: "\x1a\xff\x00\x12" (4 bytes) * RS-232: "1AFF0012\r" (9 bytes) * The opposite transformation takes place for the reply. * * This function abstracts these differences. Note that the exact answerLen is * important for USB HID: reading excess bytes can result in garbage being * appended to the reply (at least on Windows). */ int SpectralLMM5Interface::ExecuteCommand(MM::Device& device, MM::Core& core, unsigned char* buf, unsigned long bufLen, unsigned char* answer, unsigned long answerLen, unsigned long& read) { int ret; if (portType_ == MM::SerialPort) { std::string serialCommand; char tmp[3]; tmp[2] = 0; for (unsigned long i=0; i<bufLen; i++) { sprintf(tmp, "%.2x", buf[i]); serialCommand += tmp; } ret = core.SetSerialCommand(&device, port_.c_str(), serialCommand.c_str(), "\r"); } else // check for USB port { ret = core.WriteToSerial(&device, port_.c_str(), buf, bufLen); } if (ret != DEVICE_OK) return ret; if (portType_ == MM::SerialPort) { char strAnswer[128]; read = 0; ret = core.GetSerialAnswer(&device, port_.c_str(), 128, strAnswer, "\r"); if (ret != DEVICE_OK) return ret; // 'translate' back into numbers: std::string tmp = strAnswer; for (unsigned int i=0; i < tmp.length()/2; i++) { char * end; long j = strtol(tmp.substr(i*2,2).c_str(), &end, 16); answer[i] = (unsigned char) j; read++; } } else if (portType_ == MM::HIDPort) { // The USB port will attempt to read up to answerLen characters ret = core.ReadFromSerial(&device, port_.c_str(), answer, answerLen, read); if (ret != DEVICE_OK) return ret; } return DEVICE_OK; }
/* * Change command level, 65 = high command set, 66 = low command set */ int changeCommandLevel(MM::Device& device, MM::Core& core, const char* commandLevel) { int level; if (strcmp(commandLevel, g_CommandLevelHigh) == 0) level = 65; else if (strcmp(commandLevel, g_CommandLevelLow) == 0) level = 66; else return ERR_INVALID_COMMAND_LEVEL; if (port_ == "") return ERR_NO_CONTROLLER; const unsigned cmdLen = 2; unsigned char cmd[cmdLen]; cmd[0] = (unsigned)255; cmd[1] = (unsigned char)level; int ret = core.WriteToSerial(&device, port_.c_str(), cmd, cmdLen); if (ret !=DEVICE_OK) return ret; return DEVICE_OK; }