/** * Clears the serial receive buffer. */ void CARVIIHub::ClearAllRcvBuf(MM::Device& device, MM::Core& core) { // Read whatever has been received so far: unsigned long read; core.ReadFromSerial(&device, port_.c_str(), (unsigned char*) rcvBuf_, RCV_BUF_LENGTH, read); // Delete it all: memset(rcvBuf_, 0, RCV_BUF_LENGTH); }
// // General utility function // int ClearPort(MM::Device& device, MM::Core& core, const char* sPort) { // Clear contents of serial port const int nBufSize = 255; unsigned char sClear[nBufSize]; unsigned long lRead = nBufSize; int ret; // reset the communication port buffer while ((int) lRead == nBufSize) { // reading from the serial port ret = core.ReadFromSerial(&device, sPort, sClear, nBufSize, lRead); std::ostringstream sMessage; sMessage << "<nPC400::ClearPort> port = (" << sPort << ") :: clearBuffer(" << lRead << ") = (" << sClear << ")"; core.LogMessage(&device, sMessage.str().c_str(), false); // verify the read operation if (ret != DEVICE_OK) return ret; } // upon successful restting the port return DEVICE_OK; }
int ASIFW1000Hub::FilterWheelBusy(MM::Device& device, MM::Core& core, bool& busy) { ClearAllRcvBuf(device, core); int ret = core.SetSerialCommand(&device, port_.c_str(), "?", ""); if (ret != DEVICE_OK) return ret; unsigned long read = 0; MM::MMTime startTime = core.GetCurrentMMTime(); while (read == 0 && ( (core.GetCurrentMMTime() - startTime) < 10000)) ret = core.ReadFromSerial(&device, port_.c_str(), (unsigned char*)rcvBuf_, 1, read); if (ret != DEVICE_OK) return ret; if (read != 1) { printf (" FilterWheel received no answer!\n"); return ERR_NO_ANSWER; } if (rcvBuf_[0] == '0' || rcvBuf_[0] == '1' || rcvBuf_[0] =='2') busy = false; else if (rcvBuf_[0] == '3') busy = 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 { 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; }
/** * Clears the serial receive buffer. */ void ASIFW1000Hub::ClearAllRcvBuf(MM::Device& device, MM::Core& core) { // Read whatever has been received so far: unsigned long read = RCV_BUF_LENGTH; while (read == (unsigned long) RCV_BUF_LENGTH) { core.ReadFromSerial(&device, port_.c_str(), (unsigned char*)rcvBuf_, RCV_BUF_LENGTH, read); } // Delete it all: memset(rcvBuf_, 0, RCV_BUF_LENGTH); }
// General utility function: int ClearPort(MM::Device& device, MM::Core& core, std::string port) { // Clear contents of serial port const int bufSize = 255; unsigned char clear[bufSize]; unsigned long read = bufSize; int ret; while (read == (unsigned) bufSize) { ret = core.ReadFromSerial(&device, port.c_str(), clear, bufSize, read); if (ret != DEVICE_OK) return ret; } return DEVICE_OK; }
/* * Global Utility function for communication with the controller */ int clearPort(MM::Device& device, MM::Core& core, const char* port) { // Clear contents of serial port const unsigned int bufSize = 255; unsigned char clear[bufSize]; unsigned long read = bufSize; int ret; while (read == bufSize) { ret = core.ReadFromSerial(&device, port, clear, bufSize, read); if (ret != DEVICE_OK) return ret; } 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; }
int ASIFW1000Hub::FilterWheelBusy(MM::Device& device, MM::Core& core, bool& busy) { busy = false; ClearAllRcvBuf(device, core); int ret = core.SetSerialCommand(&device, port_.c_str(), "?", ""); if (ret != DEVICE_OK) { std::ostringstream os; os << "ERROR: SetSerialCommand returned error code: " << ret; core.LogMessage(&device, os.str().c_str(), false); return ret; } unsigned long read = 0; MM::TimeoutMs timerOut(core.GetCurrentMMTime(), 200 ); while (read == 0 && ( !timerOut.expired(core.GetCurrentMMTime()) ) ) { ret = core.ReadFromSerial(&device, port_.c_str(), (unsigned char*)rcvBuf_, 1, read); } if (ret != DEVICE_OK) { std::ostringstream os; os << "ERROR: ReadFromSerial returned error code: " << ret; core.LogMessage(&device, os.str().c_str(), false); return ret; } if (read != 1) { std::ostringstream os; os << "ERROR: Read " << read << " characters instead of 1"; core.LogMessage(&device, os.str().c_str(), false); return ERR_NO_ANSWER; } if (rcvBuf_[0] == '0' || rcvBuf_[0] == '1' || rcvBuf_[0] =='2') busy = false; else if (rcvBuf_[0] == '3') busy = true; return DEVICE_OK; }