private void setBaudRate(BaudRate rate) { Payload p = new Payload(MessageID.Configure_Serial_Port, new byte[] { 0x00, (byte)rate, 0x02 }); Write(p); if (RESULT.RESULT_ACK == this.waitResult(MessageID.Configure_Serial_Port)) { // 成功したから、COM ポートのボーレートも変更する int[] ParaRate = { 4800, 9600, 19200, 38400, 57600, 115200, 230400 }; _com.BaudRate = ParaRate[(int)rate]; System.Threading.Thread.Sleep(50); } }
public bool EraceLatLonData() { try { Payload p = new Payload(MessageID.Clear_Data_Logging_Buffer); Write(p); if (RESULT.RESULT_ACK != this.waitResult(MessageID.Clear_Data_Logging_Buffer)) { throw new Exception("削除できない"); } return true; } catch (TimeoutException) { try { // Restartして終了 sendRestart(); } catch (TimeoutException) { // 処理なし recovery(); } return false; } }
private void RequestBufferStatus(out UInt16 totalSectors, out UInt16 freeSectors, out bool dataLogEnable) { Payload p = new Payload(MessageID.Request_Information_of_the_Log_Buffer_Status); Write(p); Payload result; if (RESULT.RESULT_ACK != this.waitResult(MessageID.Request_Information_of_the_Log_Buffer_Status)) { throw new Exception("NACK!"); } result = Read(); if (result.ID != MessageID.Output_Status_of_the_Log_Buffer) { throw new Exception("Sequence error"); } totalSectors = (UInt16)result.Body[8]; totalSectors <<= 8; totalSectors |= (UInt16)result.Body[7]; freeSectors = (UInt16)result.Body[6]; freeSectors <<= 8; freeSectors |= (UInt16)result.Body[5]; dataLogEnable = (0x01 == result.Body[33]); }
public Payload Read() { DateTime start = DateTime.Now; TimeSpan ts; bool findHeader1 = false; bool findHeader2 = false; // header check byte readData; while (true) { readData = (byte)(0x00FF & _com.ReadByte()); if (findHeader1) { if (START_OF_SEQUENCE_2 == readData) { findHeader2 = true; break; } findHeader1 = false; } else { if (START_OF_SEQUENCE_1 == readData) { findHeader2 = false; findHeader1 = true; } } // データは読み出せたけど、目的のデータが読み出せないので、タイムアウトとして処理します。 ts = DateTime.Now - start; if (READ_TIMEOUT < ts.TotalMilliseconds) throw new TimeoutException(); } // payload length UInt16 payloadLength = (UInt16)(0x00FF & _com.ReadByte()); payloadLength <<= 8; payloadLength |= (UInt16)(0x00FF & _com.ReadByte()); // read payload System.Threading.Thread.Sleep(10); byte[] rawPayload = new byte[payloadLength]; for (int readCount = 0; readCount < payloadLength;) { readCount += _com.Read(rawPayload, readCount, payloadLength - readCount); } Payload result = new Payload(rawPayload, 0, rawPayload.Length); // CS byte checkSum = (byte)(0x00FF & _com.ReadByte()); byte calcChecSum = 0; for (int index = 0; index < rawPayload.Length; ++index) { calcChecSum ^= rawPayload[index]; } if (checkSum != calcChecSum) { throw new Exception("check sum error"); } // footer check bool findFooter1 = false; bool findFooter2 = false; start = DateTime.Now; while (true) { readData = (byte)(0x00FF & _com.ReadByte()); if (findFooter1) { if (END_OF_SEQUENCE_2 == readData) { findFooter2 = true; break; } findFooter1 = false; } else { if (END_OF_SEQUENCE_1 == readData) { findFooter2 = false; findFooter1 = true; } } // データは読み出せたけど、目的のデータが読み出せないので、タイムアウトとして処理します。 ts = DateTime.Now - start; if (READ_TIMEOUT < ts.TotalMilliseconds) throw new TimeoutException(); } return result; }
private void sendReadBuffer(int offsetSector, int sectorCount) { Payload p = new Payload(MessageID.Enable_data_read_from_the_log_buffer, new byte[] { 0x00, 0x00, 0x00, 0x02 }); p.Body[0] = (byte)(0x00FF & (offsetSector >> 8)); p.Body[1] = (byte)(0x00FF & (offsetSector >> 0)); p.Body[2] = (byte)(0x00FF & (sectorCount >> 8)); p.Body[3] = (byte)(0x00FF & (sectorCount >> 0)); Write(p); this.waitResult(MessageID.Enable_data_read_from_the_log_buffer); }
private void sendRestart() { Payload p = new Payload(MessageID.System_Restart, new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }); Write(p); if (RESULT.RESULT_ACK == this.waitResult(MessageID.System_Restart)) { // リセットが成功したのでボーレートも戻す _com.BaudRate = 38400; System.Threading.Thread.Sleep(50); } }
private byte[] convert(Payload payload) { // payload length UInt16 payloadLength = (UInt16)(payload.ByteLength); // 2 = sizeof([Start of Sequence]) // 2 = sizeof([Payload Length]) // 1 = sizeof([CS]) // 2 = sizeof([End of Sequence]) int size = payloadLength + 7; // command buffer byte[] result = new byte[size]; // set [Start of Sequence] result[0] = START_OF_SEQUENCE_1; result[1] = START_OF_SEQUENCE_2; // set [Payload Length] result[2] = (byte)(MASK_LOBYTE & (payloadLength >> 8)); result[3] = (byte)((MASK_LOBYTE & payloadLength)); // payload payload.CopyTo(result, 4, payloadLength); // [CS] byte checkSum = 0x00; for (int index = 0; index < payloadLength + 1; ++index) { checkSum ^= result[index + 4]; } result[size - 3] = checkSum; // [End of Sequence] result[size - 2] = END_OF_SEQUENCE_1; result[size - 1] = END_OF_SEQUENCE_2; return result; }
public void Write(Payload payload) { byte[] command = convert(payload); _com.Write(command, 0, command.Length); }
public Payload Read() { bool findHeader1 = false; bool findHeader2 = false; // header check byte readData; while (true) { readData = (byte)(0x00FF & _com.ReadByte()); if (findHeader1) { if (START_OF_SEQUENCE_2 == readData) { findHeader2 = true; break; } findHeader1 = false; } else { if (START_OF_SEQUENCE_1 == readData) { findHeader2 = false; findHeader1 = true; } } } // payload length UInt16 payloadLength = (UInt16)(0x00FF & _com.ReadByte()); payloadLength <<= 8; payloadLength |= (UInt16)(0x00FF & _com.ReadByte()); // read payload System.Threading.Thread.Sleep(10); byte[] rawPayload = new byte[payloadLength]; for (int readCount = 0; readCount < payloadLength;) { readCount += _com.Read(rawPayload, readCount, payloadLength - readCount); } Payload result = new Payload(rawPayload, 1, rawPayload.Length); // CS byte checkSum = (byte)(0x00FF & _com.ReadByte()); byte calcChecSum = 0; for (int index = 0; index < rawPayload.Length; ++index) { calcChecSum ^= rawPayload[index]; } if (checkSum != calcChecSum) { throw new Exception("check sum error"); } // footer check bool findFooter1 = false; bool findFooter2 = false; while (true) { readData = (byte)(0x00FF & _com.ReadByte()); if (findFooter1) { if (END_OF_SEQUENCE_2 == readData) { findFooter2 = true; break; } findFooter1 = false; } else { if (END_OF_SEQUENCE_1 == readData) { findFooter2 = false; findFooter1 = true; } } } return result; }