public TCMMessage(TCMPacket packet, long bapID) { this.bapID = bapID; this.protocol = packet.protocol; this.message = packet.completeData; this.datum_uhrzeit = packet.timestamp; this.highspeedMessage = true; switch (protocol) { case TCMPacket.PROTOCOL_ESP2: if (!readESP2(packet)) { headerID = HEADERID_UNKNOWN1; } break; // Meistens Protocoll ESP3 case TCMPacket.PROTOCOL_ESP3: if (!readESP3(packet)) { headerID = HEADERID_UNKNOWN1; } break; } this.dataBytes = ((int)(byte3 & 0xFF) << 24 | (int)(byte2 & 0xFF) << 16 | (int)(byte1 & 0xFF) << 8 | (int)(byte0 & 0xFF)); this.learnMessage = orgByte == ORG_D2 ? false : orgByte == ORG_1BS ? (byte3 & 0x08) == 0 : (byte0 & 0x08) == 0; this.regularData = learnMessage ? orgByte == ORG_1BS ? true : (byte0 & 0x80) == 0 : true; if (learnMessage && !regularData) { if (this.orgByte == ORG_D4) { //int test = byte4; manufacturer = byte4; function = byte1; type = byte2; } else if (this.orgByte == ORG_D2) { } else { manufacturer = readBits(dataBytes, 13, 11); function = readBits(dataBytes, 0, 6); type = readBits(dataBytes, 6, 7); } } else { function = UNKNOWN; type = UNKNOWN; manufacturer = UNKNOWN; } }
private bool readESP2(TCMPacket packet) { this.objectID = byteArrToLong(message, 8); this.byte3 = byteArrToInt(message, 4); this.byte2 = byteArrToInt(message, 5); this.byte1 = byteArrToInt(message, 6); this.byte0 = byteArrToInt(message, 7); this.orgByte = message[3]; this.headerID = readBits(message[2], 0, 3); this.status = byteArrToInt(message, 12); this.signalStrength = 255; this.securityLevel = 0; this.destinationID = 0xFFFFFFFF; return(true); }
// reads id within a timelimit private long readID(long timeout) { long id = -1; try { TCMPacket packet = readTCMPacket(timeout); if (packet == null) { return(id); } byte[] data = packet.data; if (packet.type != TCMPacket.TYPE_RESPONSE) { return(readID(timeout)); } int returnCode = data[0]; if (returnCode != 0) { return(id); } id = 0; id |= (long)(data[1] & 0xFF) << 24; id |= (long)(data[2] & 0xFF) << 16; id |= (long)(data[3] & 0xFF) << 8; id |= (long)(data[4] & 0xFF); } catch (Exception e) { Trace.WriteLine(e.StackTrace); } return(id); }
private bool readESP3(TCMPacket packet) { //Erst auf optionale Daten prüfen // PacketType muss 1 sein // SecurityLevvel muss 0 sein // Read Data -> Falls Korrekt, dann bytes setzen if (packet.type != TCMPacket.TYPE_RADIO) { //Console.WriteLine("Packet type " + packet.type + " is not supported"); return(false); } if (packet.optionalData != null) { byte[] optionalData = packet.optionalData; subtelegrams = (int)(optionalData[0] & 0xFF); destinationID = 0; destinationID |= (long)(optionalData[1] & 0xFF) << 24; destinationID |= (long)(optionalData[2] & 0xFF) << 16; destinationID |= (long)(optionalData[3] & 0xFF) << 8; destinationID |= (long)(optionalData[4] & 0xFF); signalStrength = (int)(optionalData[5] & 0xFF); securityLevel = (int)(optionalData[6] & 0xFF); } if (securityLevel != 0) { Console.WriteLine("Security type " + securityLevel + " is not supported"); return(false); } MemoryStream memoryStream = new MemoryStream(packet.data); BinaryReader binaryReader = new BinaryReader(memoryStream); int choice = binaryReader.ReadByte(); switch (choice) { case 0xA5: //4BS orgByte = ORG_4BS; break; case 0xD5: //1BS orgByte = ORG_1BS; break; case 0xF6: //RPS orgByte = ORG_RPS; break; case 0xD4: orgByte = ORG_D4; break; case 0xD2: orgByte = ORG_D2; break; default: Console.WriteLine("Unsupported choice " + choice); return(false); } switch (orgByte) { case ORG_4BS: this.byte3 = binaryReader.ReadByte(); this.byte2 = binaryReader.ReadByte(); this.byte1 = binaryReader.ReadByte(); this.byte0 = binaryReader.ReadByte(); break; case ORG_1BS: this.byte3 = binaryReader.ReadByte(); this.byte2 = 0; this.byte1 = 0; this.byte0 = 0; break; case ORG_RPS: this.byte3 = binaryReader.ReadByte(); this.byte2 = 0; this.byte1 = 0; this.byte0 = 0; break; case ORG_D4: this.byte6 = binaryReader.ReadByte(); this.byte5 = binaryReader.ReadByte(); this.byte4 = binaryReader.ReadByte(); this.byte3 = binaryReader.ReadByte(); this.byte2 = binaryReader.ReadByte(); this.byte1 = binaryReader.ReadByte(); this.byte0 = binaryReader.ReadByte(); break; case ORG_D2: if (packet.completeData[2] == 0x0E) { this.byte7 = binaryReader.ReadByte(); this.byte6 = binaryReader.ReadByte(); this.byte5 = binaryReader.ReadByte(); this.byte4 = binaryReader.ReadByte(); this.byte3 = binaryReader.ReadByte(); this.byte2 = binaryReader.ReadByte(); this.byte1 = binaryReader.ReadByte(); this.byte0 = binaryReader.ReadByte(); } else if (packet.completeData[2] == 0x0C) { this.byte5 = binaryReader.ReadByte(); this.byte4 = binaryReader.ReadByte(); this.byte3 = binaryReader.ReadByte(); this.byte2 = binaryReader.ReadByte(); this.byte1 = binaryReader.ReadByte(); this.byte0 = binaryReader.ReadByte(); } else if (packet.completeData[2] == 0x08) { this.byte1 = binaryReader.ReadByte(); this.byte0 = binaryReader.ReadByte(); } break; } objectID = 0; objectID |= (long)(binaryReader.ReadByte() & 0xFF) << 24; objectID |= (long)(binaryReader.ReadByte() & 0xFF) << 16; objectID |= (long)(binaryReader.ReadByte() & 0xFF) << 8; objectID |= (long)(binaryReader.ReadByte() & 0xFF); status = binaryReader.ReadByte(); headerID = HEADERID_RRT; return(true); }
//readTCMPacket with timeout limit IMPORTANT private TCMPacket readTCMPacket(long timeout) { TCMPacket tcmPacket = null; DateTime startTime = System.DateTime.Now.AddMilliseconds(timeout); while (startTime.Ticks > System.DateTime.Now.Ticks) { while (serialPort.BytesToRead == 0 && startTime.Ticks > System.DateTime.Now.Ticks) { Thread.Sleep(10); } if (serialPort.BytesToRead == 0) { return(null); } int tmpByte = serialPort.ReadByte(); switch (tmpByte) { case 0x55: tmpByte = serialPort.ReadByte(); byte[] header = new byte[6]; header[0] = (byte)0x55; header[1] = (byte)tmpByte; DateTime start = System.DateTime.Now.AddMilliseconds(2000); while (start.Ticks > System.DateTime.Now.Ticks && serialPort.BytesToRead < 4) { Thread.Sleep(10); } if (serialPort.BytesToRead < 4) { continue; } serialPort.Read(header, 2, 4); if (Tools.calculateCRC8(header, 1, 4) != header[5]) { Trace.WriteLine("ESP3 Prüfsummenfehler im Header"); continue; } int dataLength = 0; dataLength |= (header[1] & 0xFF) << 8; dataLength |= header[2] & 0xFF; byte[] data = new byte[dataLength]; start = System.DateTime.Now.AddMilliseconds(2000); while (start.Ticks > System.DateTime.Now.Ticks && serialPort.BytesToRead < dataLength) { Thread.Sleep(10); } if (serialPort.BytesToRead < dataLength) { continue; } serialPort.Read(data, 0, data.Length); int optionalDataLength = header[3]; byte[] optionalData = new byte[optionalDataLength]; start = System.DateTime.Now.AddMilliseconds(2000); while (start.Ticks > System.DateTime.Now.Ticks && serialPort.BytesToRead < optionalDataLength) { Thread.Sleep(10); } if (serialPort.BytesToRead < optionalDataLength) { continue; } serialPort.Read(optionalData, 0, optionalDataLength); int packetType = header[4]; int checksum = serialPort.ReadByte(); byte[] checkdata = new byte[dataLength + optionalDataLength]; System.Array.Copy(data, 0, checkdata, 0, dataLength); System.Array.Copy(optionalData, 0, checkdata, dataLength, optionalDataLength); if (Tools.calculateCRC8(checkdata) != (byte)checksum) { Debug.WriteLine("ESP3 Prüfsummenfehler im Datenbereich"); continue; } byte[] completeData = new byte[header.Length + data.Length + optionalData.Length + 1]; System.Array.Copy(header, 0, completeData, 0, header.Length); System.Array.Copy(data, 0, completeData, header.Length, data.Length); System.Array.Copy(optionalData, 0, completeData, header.Length + data.Length, optionalData.Length); completeData[completeData.Length - 1] = (byte)checksum; return(tcmPacket = new TCMPacket(TCMPacket.PROTOCOL_ESP3, packetType, data, optionalData, completeData, System.DateTime.Now)); default: continue; } } return(null); }