private static void GetDiveProfile(byte tocIndex) { WriteByte(sp, OSTCCommands.GET_DIVE_PROFILE); if (!(sp.ReadByte() == OSTCCommands.GET_DIVE_PROFILE)) { throw new InvalidDataException("Expected " + OSTCCommands.GET_DIVE_PROFILE.ToString("X2")); } WriteByte(sp, tocIndex); byte[] diveHeaderBuffer = new byte[256]; int bytesLeft = 256; do { int bytesRead = sp.Read(diveHeaderBuffer, (int)(256 - bytesLeft), bytesLeft); bytesLeft -= bytesRead; Console.WriteLine("got " + bytesRead.ToString() + " bytes " + bytesLeft.ToString() + " remaining!"); }while (bytesLeft > 0); OSTCBinaryReader reader = new OSTCBinaryReader(new MemoryStream(diveHeaderBuffer)); OSTCDiveHeader header = reader.ReadDiveHeader(tocIndex); int bytesToRead = (int)header.ProfileDataLength - 2; bytesLeft = bytesToRead; byte[] buffer = new byte[bytesToRead]; do { int bytesRead = sp.Read(buffer, (int)(bytesToRead - bytesLeft), bytesLeft); bytesLeft -= bytesRead; Console.WriteLine("got " + bytesRead.ToString() + " bytes " + bytesLeft.ToString() + " remaining!"); }while (bytesLeft > 0); File.WriteAllBytes("dive_profile_" + tocIndex.ToString() + ".bin", buffer); }
public OSTCDiveHeader ReadDiveHeader(byte tocIndex) { return(OSTCDiveHeader.FromBinary(this, tocIndex)); }
static void Main(string[] args) { FileStream fs = File.OpenRead("dive_headers.bin"); OSTCBinaryReader br = new OSTCBinaryReader(fs); List <OSTCDiveHeader> headers = new List <OSTCDiveHeader>(); byte tocIndex = 0; while (true) { OSTCDiveHeader header = br.ReadDiveHeader(tocIndex++); if (header != null) { headers.Add(header); } else { break; } } string jsonHeader = Newtonsoft.Json.JsonConvert.SerializeObject(headers, Newtonsoft.Json.Formatting.Indented); File.WriteAllText("headers.json", jsonHeader); Console.ReadKey(); return; sp = new SerialPort("COM3"); sp.BaudRate = 115200; sp.ReadTimeout = 3000; sp.Open(); try { WriteByte(sp, OSTCCommands.START_COMMUNICATION); if (!(sp.ReadByte() == OSTCCommands.START_COMMUNICATION)) { throw new InvalidDataException("Expected 0xBB"); } if (!(sp.ReadByte() == OSTCReplys.READY_FOR_COMMAND)) { throw new InvalidDataException("Expected 0x4D"); } for (int c = 0; c <= 20; c++) { GetDiveProfile((byte)c); } } catch (Exception ex) { Console.WriteLine("Got exceptioN!: " + ex.ToString()); } finally { try { WriteByte(sp, OSTCCommands.CLOSE_COMMUNICATION); } catch (Exception eoce) { Console.WriteLine("Exception while sending END OF COM" + eoce.ToString()); } sp.Close(); } Console.WriteLine("END!"); Console.ReadLine(); }
internal static OSTCDiveHeader FromBinary(OSTCBinaryReader br, byte tocIndex) { try { byte a = br.ReadByte(); byte b = br.ReadByte(); // if this is an empty dive! if (a == 0xFF && b == 0xFF) { return(null); } if (a != 0xFA || b != 0xFA) { throw new InvalidDataException("Expected 0xFAFA as dive header start but got " + a.ToString("X2") + b.ToString("X2")); } } catch (EndOfStreamException) { return(null); } var dh = new OSTCDiveHeader { TOCIndex = tocIndex, DataStartAddress = br.ReadUInt24(), DataStopAddress = br.ReadUInt24(), ProfileVersion = (LogbookProfileVersion)br.ReadByte(), ProfileDataLength = br.ReadUInt24(), Date = br.ReadDate(), WaterPressureMbarMax = br.ReadUInt16(), DiveTime = br.ReadTimeSpan24(), WaterTemperatureMin = br.ReadInt16() / 10.0, SurfacePressureMBar = br.ReadUInt16(), DesaturationTime = br.ReadTimeSpan16(), Gases = new List <OSTCGasInfo>(new OSTCGasInfo[] { br.ReadGas32(), // GAS1 br.ReadGas32(), // GAS2 br.ReadGas32(), // GAS3 br.ReadGas32(), // GAS4 br.ReadGas32() // GAS5 }), FirmwareVersion = br.ReadFWVersion(), BatteryVoltage = br.ReadUInt16() / 1000.0, SamplingRate = TimeSpan.FromSeconds(br.ReadByte()), CNSDiveStartPercent = br.ReadUInt16() / 12.0, GFDiveStartPercent = br.ReadByte() / 12.0, GFDiveEndPercent = br.ReadByte() / 12.0, LogbookOffset = br.ReadUInt16(), BatteryInformation = br.ReadByte(), Setpoints = new List <OSTCSetpointInfo>(new OSTCSetpointInfo[] { br.ReadSetpoint16(), // SP1 br.ReadSetpoint16(), // SP2 br.ReadSetpoint16(), // SP3 br.ReadSetpoint16(), // SP4 br.ReadSetpoint16() // SP5 }), Salinity = br.ReadByte(), MaxCNSPercent = br.ReadUInt16() / 12.0, WaterPressureMBarAverage = br.ReadUInt16(), TotalDivetime = TimeSpan.FromSeconds(br.ReadUInt16()), DecoModelInfoA = br.ReadByte(), DecoModelInfoB = br.ReadByte(), DecoModel = (DecoModel)br.ReadByte(), DiveNumber = br.ReadUInt16(), DiveMode = (DiveMode)br.ReadByte(), CompartmentsN2DesatTime = br.ReadBytes(16), CompartmentsN2 = br.ReadBytes(64), CompartmentsHEDesatTime = br.ReadBytes(16), CompartmentsHE = br.ReadBytes(64), LastDecoStopMeters = br.ReadByte(), AssumedDistanceToShownStopCm = br.ReadByte(), HWHudBatteryVoltage = br.ReadUInt16() / 1000.0, HWHudLastStatus = br.ReadByte(), BatteryGauge = br.ReadBytes(6) }; if (br.ReadByte() != 0xFB || br.ReadByte() != 0xFB) { throw new InvalidDataException("Expected 0xFBFB as dive header end but got something else!"); } return(dh); }