static void FindDeviceDt() { if (IsFindDeviceDt) { return; } WriteBuf = WriteBuf_6; BytesSucceed = 0; // Send output data out to the board SLUSBXpressDLL.Status = SLUSBXpressDLL.SI_Write(SLUSBXpressDLL.hUSBDevice, ref WriteBuf[0], BytesWriteRequest, ref BytesSucceed, 0); if ((BytesSucceed != BytesWriteRequest) || (SLUSBXpressDLL.Status != SLUSBXpressDLL.SI_SUCCESS)) { if (SLUSBXpressDLL.Status == SLUSBXpressDLL.SI_WRITE_TIMED_OUT) { return; } Debug.Log("Error writing to USB. Wrote " + BytesSucceed.ToString() + " of " + BytesWriteRequest.ToString() + " bytes. Application is aborting. Reset hardware and try again."); Debug.Log("Status " + SLUSBXpressDLL.Status); IsOpenDevice = false; Application.Quit(); return; } //clear out bytessucceed for the next read BytesSucceed = 0; //read data from the board SLUSBXpressDLL.Status = SLUSBXpressDLL.SI_Read(SLUSBXpressDLL.hUSBDevice, ref ReadBuf[0], BytesReadRequest, ref BytesSucceed, 0); if ((BytesSucceed != BytesReadRequest) || (SLUSBXpressDLL.Status != SLUSBXpressDLL.SI_SUCCESS)) { if (SLUSBXpressDLL.Status == SLUSBXpressDLL.SI_READ_TIMED_OUT) { return; } Debug.Log("Error reading to USB. Read " + BytesSucceed.ToString() + " of " + BytesReadRequest.ToString() + " bytes. Application is aborting. Reset hardware and try again."); Debug.Log("Status " + SLUSBXpressDLL.Status); IsOpenDevice = false; Application.Quit(); return; } if (ReadBuf[0] == 0x65) { IsFindDeviceDt = true; Debug.Log("FindDeviceDt over..."); } /*SLUSBXpressDLL.Status = SLUSBXpressDLL.SI_FlushBuffers(SLUSBXpressDLL.hUSBDevice, 1, 1); * if (SLUSBXpressDLL.Status != SLUSBXpressDLL.SI_SUCCESS) { * Debug.Log("SI_FlushBuffers was wrong!"); * IsOpenDevice = false; * Application.Quit(); * return; * }*/ }
static void FindDevice() { if (!IsOpenDevice) { return; } //clear out bytessucceed for the next read BytesSucceed = 0; //read data from the board SLUSBXpressDLL.Status = SLUSBXpressDLL.SI_Read(SLUSBXpressDLL.hUSBDevice, ref ReadBuf[0], BytesReadRequest, ref BytesSucceed, 0); if ((BytesSucceed != BytesReadRequest) || (SLUSBXpressDLL.Status != SLUSBXpressDLL.SI_SUCCESS)) { if (SLUSBXpressDLL.Status == SLUSBXpressDLL.SI_READ_TIMED_OUT) { return; } Debug.Log("Error reading to USB. Read " + BytesSucceed.ToString() + " of " + BytesReadRequest.ToString() + " bytes. Application is aborting. Reset hardware and try again."); Debug.Log("Status " + SLUSBXpressDLL.Status); IsOpenDevice = false; Application.Quit(); return; } if (ReadBuf[0] == 0x65 && ReadBuf[20] == 0x01) { IsFindDevice = true; string readStr = "ReadBuf: "; for (int i = 0; i < BytesReadRequest; i++) { readStr += ReadBuf[i].ToString("X2"); readStr += " "; } //Debug.Log(readStr); Debug.Log("find device..."); } BytesSucceed = 0; // Send output data out to the board SLUSBXpressDLL.Status = SLUSBXpressDLL.SI_Write(SLUSBXpressDLL.hUSBDevice, ref WriteBuf[0], BytesWriteRequest, ref BytesSucceed, 0); if ((BytesSucceed != BytesWriteRequest) || (SLUSBXpressDLL.Status != SLUSBXpressDLL.SI_SUCCESS)) { if (SLUSBXpressDLL.Status == SLUSBXpressDLL.SI_WRITE_TIMED_OUT) { return; } Debug.Log("Error writing to USB. Wrote " + BytesSucceed.ToString() + " of " + BytesWriteRequest.ToString() + " bytes. Application is aborting. Reset hardware and try again."); Debug.Log("Status " + SLUSBXpressDLL.Status); IsOpenDevice = false; Application.Quit(); return; } }
public static void TimerTick() { if (!IsOpenDevice) { return; } if (!IsFindDeviceDt) { if (!IsFindDevice) { FindDevice(); return; } if (!IsCheckDevice) { CheckDevice(); return; } FindDeviceDt(); return; } WriteBuf = PcvrWriteByte; WriteBuf[17] = (byte)(LastWriteByte == 0x01 ? 0x02 : 0x01); if (WriteBuf[17] == 0x02) { WriteBuf[14] = 0x02; WriteBuf[15] = 0; for (int i = 6; i < 14; i++) { WriteBuf[15] ^= WriteBuf[i]; } } else { WriteBuf[17] = 0x01; } BytesSucceed = 0; // Send output data out to the board SLUSBXpressDLL.Status = SLUSBXpressDLL.SI_Write(SLUSBXpressDLL.hUSBDevice, ref WriteBuf[0], BytesWriteRequest, ref BytesSucceed, 0); if ((BytesSucceed != BytesWriteRequest) || (SLUSBXpressDLL.Status != SLUSBXpressDLL.SI_SUCCESS)) { if (SLUSBXpressDLL.Status == SLUSBXpressDLL.SI_WRITE_TIMED_OUT) { return; } Debug.Log("Error writing to USB. Wrote " + BytesSucceed.ToString() + " of " + BytesWriteRequest.ToString() + " bytes. Application is aborting. Reset hardware and try again."); Debug.Log("Status " + SLUSBXpressDLL.Status); IsOpenDevice = false; Application.Quit(); return; } LastWriteByte = WriteBuf[17]; //clear out bytessucceed for the next read BytesSucceed = 0; //read data from the board SLUSBXpressDLL.Status = SLUSBXpressDLL.SI_Read(SLUSBXpressDLL.hUSBDevice, ref ReadBuf[0], BytesReadRequest, ref BytesSucceed, 0); if ((BytesSucceed != BytesReadRequest) || (SLUSBXpressDLL.Status != SLUSBXpressDLL.SI_SUCCESS)) { if (SLUSBXpressDLL.Status == SLUSBXpressDLL.SI_READ_TIMED_OUT) { return; } Debug.Log("Error reading to USB. Read " + BytesSucceed.ToString() + " of " + BytesReadRequest.ToString() + " bytes. Application is aborting. Reset hardware and try again."); Debug.Log("Status " + SLUSBXpressDLL.Status); IsOpenDevice = false; Application.Quit(); return; } if (ReadBuf[0] == 0x65) { /*string testReadBt = "解码前: "; * for (int i = 0; i < 21; i++) { * testReadBt += ReadBuf[i].ToString("X2"); * testReadBt += " "; * }*/ //StrTest2 = testReadBt; //Debug.Log(testReadBt); byte[] getDt = new byte[8]; for (int i = 0; i < 8; i++) { getDt[i] = ReadBuf[i + 1]; } byte[] byteTmp = EncryptHelper.TripleDesDecryptIOData(getDt); byte[] byteReadTmp = ReadBuf; for (int i = 0; i < 8; i++) { byteReadTmp[i + 1] = byteTmp[i]; } //testReadBt = "解码后: "; for (int i = 0; i < 21; i++) { ReadInfo[i] = (uint)byteReadTmp[i]; //testReadBt += ReadInfo[i].ToString("X2"); //testReadBt += " "; } //Debug.Log("ReadInfo[0] "+ReadInfo[0]); //StrTest3 = testReadBt; //Debug.Log(testReadBt); //uint SteerValCur = ((ReadInfo[4]&0x03) << 8) + ReadInfo[3]; //StrTest1 = "方向: "+SteerValCur+", byte[3] "+ReadInfo[3].ToString("X2")+", byte[4] "+ReadInfo[4].ToString("X2"); //Debug.Log(StrTest1); } SLUSBXpressDLL.Status = SLUSBXpressDLL.SI_FlushBuffers(SLUSBXpressDLL.hUSBDevice, 1, 1); if (SLUSBXpressDLL.Status != SLUSBXpressDLL.SI_SUCCESS) { Debug.Log("SI_FlushBuffers was wrong!"); IsOpenDevice = false; Application.Quit(); return; } }
void OpenDevice() { int DevNum = 0; StringBuilder DevStr = new StringBuilder(SLUSBXpressDLL.SI_MAX_DEVICE_STRLEN); SLUSBXpressDLL.Status = SLUSBXpressDLL.SI_GetNumDevices(ref DevNum); if (SLUSBXpressDLL.Status != SLUSBXpressDLL.SI_SUCCESS || DevNum <= 0) { return; } int HidIndex = 0; bool isFoundHid = false; for (int i = 0; i < DevNum; i++) { SLUSBXpressDLL.Status = SLUSBXpressDLL.SI_GetProductString(i, DevStr, SLUSBXpressDLL.SI_RETURN_SERIAL_NUMBER); if (SLUSBXpressDLL.Status != SLUSBXpressDLL.SI_SUCCESS) { continue; } if (DevicesName == "") { isFoundHid = true; HidIndex = i; DevicesName = DevStr.ToString(); break; } else if (DevicesName == DevStr.ToString()) { HidIndex = i; isFoundHid = true; break; } } if (isFoundHid) { Debug.Log("DevicesName: " + DevStr); } else { Debug.Log("not find hid..."); } SLUSBXpressDLL.SI_SetTimeouts(10, 10); SLUSBXpressDLL.Status = SLUSBXpressDLL.SI_Open(HidIndex, ref SLUSBXpressDLL.hUSBDevice); if (SLUSBXpressDLL.Status != SLUSBXpressDLL.SI_SUCCESS) { Debug.Log("Error opening device: " + DevicesName + ". Application is aborting. Reset hardware and try again."); Application.Quit(); return; } else { Debug.Log("open device..."); IsOpenDevice = true; ReadInfo = new uint[BytesReadRequest]; ReadBuf = new byte[BytesReadRequest]; WriteBuf = new byte[BytesWriteRequest]; PcvrWriteByte = new byte[BytesWriteRequest]; WriteBuf[0] = 0xff; WriteBuf[1] = 0x01; WriteBuf[17] = 0x01; BytesSucceed = 0; // Send output data out to the board SLUSBXpressDLL.Status = SLUSBXpressDLL.SI_Write(SLUSBXpressDLL.hUSBDevice, ref WriteBuf[0], BytesWriteRequest, ref BytesSucceed, 0); if ((BytesSucceed != BytesWriteRequest) || (SLUSBXpressDLL.Status != SLUSBXpressDLL.SI_SUCCESS)) { if (SLUSBXpressDLL.Status == SLUSBXpressDLL.SI_WRITE_TIMED_OUT) { return; } Debug.Log("Error writing to USB. Wrote " + BytesSucceed.ToString() + " of " + BytesWriteRequest.ToString() + " bytes. Application is aborting. Reset hardware and try again."); Debug.Log("Status " + SLUSBXpressDLL.Status); IsOpenDevice = false; Application.Quit(); return; } } }