diff options
-rwxr-xr-x | ebus-scala/Main.scala | 35 |
1 files changed, 10 insertions, 25 deletions
diff --git a/ebus-scala/Main.scala b/ebus-scala/Main.scala index 05f40c2..b329ce4 100755 --- a/ebus-scala/Main.scala +++ b/ebus-scala/Main.scala @@ -82,8 +82,6 @@ class EbusL2MasterSlavePacket(source: Int, crc, rawLength); -class FakePacket(val rawLength : Int); - class EbusParseException(val message : String, val data : List[Int], val skipbytes : Int) extends Exception; object CRC { @@ -137,7 +135,7 @@ object EbusL2Packet { * Im Erfolgsfall wird eine EbusL2Packet-Instanz als Option zurückgegen, * sonst None */ - def apply(stream: Stream[Int]): Option[Any] = { + def apply(stream: Stream[Int]): EbusL2Packet = { var mystream = stream var rawLength = 0 def readBytes(n:Int, msg : String) : List[Int] = { @@ -190,9 +188,9 @@ object EbusL2Packet { throw new EbusParseException("Bad SYN: 0x%02x".format(syn), stream.slice(0, rawLength).toList, rawLength); } - return Some(new EbusL2Packet(source, destination, + return new EbusL2Packet(source, destination, primaryCommand, secondaryCommand, - payloadLength, payload, crc, rawLength)) + payloadLength, payload, crc, rawLength) } val ack = readBytes(1,"ACK")(0) @@ -204,9 +202,9 @@ object EbusL2Packet { throw new EbusParseException("Bad ACK: 0x%02x".format(ack), stream.slice(0, rawLength).toList, rawLength); } - return Some(new EbusL2Packet(source, destination, + return new EbusL2Packet(source, destination, primaryCommand, secondaryCommand, - payloadLength, payload, crc, rawLength)) + payloadLength, payload, crc, rawLength) } // otherwise this is a Master-Slave Telegramm @@ -238,10 +236,10 @@ object EbusL2Packet { stream.slice(0, rawLength).toList, rawLength); } - return Some(new EbusL2MasterSlavePacket(source, destination, + return new EbusL2MasterSlavePacket(source, destination, primaryCommand, secondaryCommand, payloadLength, payload, crc, - payloadSlaveLength, payloadSlave, crcSlave, rawLength)); + payloadSlaveLength, payloadSlave, crcSlave, rawLength); } } @@ -254,22 +252,9 @@ class EbusReader(var stream : Stream[Int]) { stream = stream.dropWhile(_ == Ebus.SYN) try { - EbusL2Packet(stream) match { - case Some(packet : EbusL2Packet) => { - stream = stream.drop(packet.rawLength) - return packet - } - case Some(packet : FakePacket) => { - println("Fake paket - forward %d bytes".format(packet.rawLength)) - stream = stream.drop(packet.rawLength) - } - case None => { - // Es konnte kein Paket eingelesen werden - // verschiebe die Anfangsposition um ein Byte - App.println("Paket konnte nicht gelesen werden, verschiebe start um 1 byte") - stream = stream.drop(1) - } - } + val packet = EbusL2Packet(stream) + stream = stream.drop(packet.rawLength) + return packet } catch { case exc : EbusParseException => { stream = stream.drop(exc.skipbytes) |