From b3a3812ee8f68b56eb32099413d20718bcec95ce Mon Sep 17 00:00:00 2001 From: Florian Loitsch Date: Thu, 4 Apr 2024 14:34:02 +0200 Subject: [PATCH] Migrate to new IO library. (#26) --- examples/package.lock | 7 ++-- examples/package.yaml | 2 +- package.lock | 6 ++-- package.yaml | 4 +-- src/framer.toit | 4 +-- src/protocol.toit | 82 +++++++++++++++++++++---------------------- src/rs485.toit | 13 ++++--- src/station.toit | 26 +++++++------- src/tcp.toit | 34 +++++++++--------- tests/package.lock | 10 +++--- tests/package.yaml | 2 +- 11 files changed, 94 insertions(+), 96 deletions(-) diff --git a/examples/package.lock b/examples/package.lock index 05e6a8e..f857f07 100644 --- a/examples/package.lock +++ b/examples/package.lock @@ -1,4 +1,4 @@ -sdk: ^2.0.0-alpha.21 +sdk: ^2.0.0-alpha.144 prefixes: modbus: .. rs485: toit-rs485 @@ -9,5 +9,6 @@ packages: rs485: toit-rs485 toit-rs485: url: github.com/toitware/toit-rs485 - version: 1.1.0 - hash: 607b0acb6a1e222eb36727c2818b82365b332fa9 + name: rs485 + version: 1.3.0 + hash: fc9d92d11ccb95816a0029140585b2a9bddc93a7 diff --git a/examples/package.yaml b/examples/package.yaml index 8678d0a..81dae37 100644 --- a/examples/package.yaml +++ b/examples/package.yaml @@ -3,4 +3,4 @@ dependencies: path: .. rs485: url: github.com/toitware/toit-rs485 - version: ^1.0.1 + version: ^1.3.0 diff --git a/package.lock b/package.lock index abd5b41..70741b4 100644 --- a/package.lock +++ b/package.lock @@ -1,9 +1,9 @@ -sdk: ^2.0.0-alpha.118 +sdk: ^2.0.0-alpha.144 prefixes: rs485: toit-rs485 packages: toit-rs485: url: github.com/toitware/toit-rs485 name: rs485 - version: 1.2.0 - hash: 8e38736d19dab1d9275568df269f982e6118acec + version: 1.3.0 + hash: fc9d92d11ccb95816a0029140585b2a9bddc93a7 diff --git a/package.yaml b/package.yaml index ed267a9..36b86de 100644 --- a/package.yaml +++ b/package.yaml @@ -1,8 +1,8 @@ name: modbus description: A Modbus Toit client. environment: - sdk: ^2.0.0-alpha.118 + sdk: ^2.0.0-alpha.144 dependencies: rs485: url: github.com/toitware/toit-rs485 - version: ^1.2.0 + version: ^1.3.0 diff --git a/src/framer.toit b/src/framer.toit index 1e0737b..b0c27f0 100644 --- a/src/framer.toit +++ b/src/framer.toit @@ -2,7 +2,7 @@ // Use of this source code is governed by an MIT-style license that can be // found in the LICENSE file. -import reader +import io class Frame: static NO-TRANSACTION-ID ::= -1 @@ -36,6 +36,6 @@ class Frame: constructor --.transaction-id --.unit-id --.function-code --.data: interface Framer: - read reader/reader.BufferedReader -> Frame? + read reader/io.Reader -> Frame? write frame/Frame writer diff --git a/src/protocol.toit b/src/protocol.toit index af321b5..60dff06 100644 --- a/src/protocol.toit +++ b/src/protocol.toit @@ -2,7 +2,7 @@ // Use of this source code is governed by an MIT-style license that can be // found in the LICENSE file. -import binary +import io import .exception import .framer @@ -85,8 +85,8 @@ class ReadBitsRequest extends Request: to-byte-array -> ByteArray: data := ByteArray 4 - binary.BIG-ENDIAN.put-uint16 data 0 address - binary.BIG-ENDIAN.put-uint16 data 2 bit-count + io.BIG-ENDIAN.put-uint16 data 0 address + io.BIG-ENDIAN.put-uint16 data 2 bit-count return data class ReadBitsResponse extends Response: @@ -98,7 +98,7 @@ class ReadBitsResponse extends Response: constructor.deserialize frame/Frame --is-coils/bool: Response.check-frame_ (is-coils ? COILS-ID : DISCRETE-INPUTS-ID) frame Response.check-sized-data_ frame - byte-count := binary.BIG-ENDIAN.uint8 frame.data 0 + byte-count := io.BIG-ENDIAN.uint8 frame.data 0 bits = frame.data[1..].copy class WriteSingleCoilRequest extends Request: @@ -111,8 +111,8 @@ class WriteSingleCoilRequest extends Request: to-byte-array -> ByteArray: data := ByteArray 4 - binary.BIG-ENDIAN.put-uint16 data 0 address - binary.BIG-ENDIAN.put-uint16 data 2 (value ? 0xFF00 : 0) + io.BIG-ENDIAN.put-uint16 data 0 address + io.BIG-ENDIAN.put-uint16 data 2 (value ? 0xFF00 : 0) return data class WriteSingleCoilResponse extends Response: @@ -124,8 +124,8 @@ class WriteSingleCoilResponse extends Response: constructor.deserialize frame/Frame: Response.check-frame_ ID frame Response.check-sized-data_ frame 4 - address = binary.BIG-ENDIAN.uint16 frame.data 0 - value = (binary.BIG-ENDIAN.uint16 frame.data 2) == 0xFF00 + address = io.BIG-ENDIAN.uint16 frame.data 0 + value = (io.BIG-ENDIAN.uint16 frame.data 2) == 0xFF00 class WriteSingleRegisterRequest extends Request: static ID ::= 6 @@ -137,8 +137,8 @@ class WriteSingleRegisterRequest extends Request: to-byte-array -> ByteArray: data := ByteArray 4 - binary.BIG-ENDIAN.put-uint16 data 0 address - binary.BIG-ENDIAN.put-uint16 data 2 value + io.BIG-ENDIAN.put-uint16 data 0 address + io.BIG-ENDIAN.put-uint16 data 2 value return data class WriteSingleRegisterResponse extends Response: @@ -150,8 +150,8 @@ class WriteSingleRegisterResponse extends Response: constructor.deserialize frame/Frame: Response.check-frame_ ID frame Response.check-sized-data_ frame 4 - address = binary.BIG-ENDIAN.uint16 frame.data 0 - value = binary.BIG-ENDIAN.uint16 frame.data 2 + address = io.BIG-ENDIAN.uint16 frame.data 0 + value = io.BIG-ENDIAN.uint16 frame.data 2 class WriteMultipleCoilsRequest extends Request: static ID ::= 15 @@ -165,8 +165,8 @@ class WriteMultipleCoilsRequest extends Request: to-byte-array -> ByteArray: data := ByteArray (5 + values.size) - binary.BIG-ENDIAN.put-uint16 data 0 address - binary.BIG-ENDIAN.put-uint16 data 2 count + io.BIG-ENDIAN.put-uint16 data 0 address + io.BIG-ENDIAN.put-uint16 data 2 count data[4] = values.size // Copy over all the values. data.replace 5 values @@ -186,8 +186,8 @@ class WriteMultipleCoilsResponse extends Response: constructor.deserialize frame/Frame: Response.check-frame_ ID frame Response.check-sized-data_ frame 4 - address = binary.BIG-ENDIAN.uint16 frame.data 0 - count = binary.BIG-ENDIAN.uint16 frame.data 2 + address = io.BIG-ENDIAN.uint16 frame.data 0 + count = io.BIG-ENDIAN.uint16 frame.data 2 class WriteHoldingRegistersRequest extends Request: static ID ::= 16 @@ -199,11 +199,11 @@ class WriteHoldingRegistersRequest extends Request: to-byte-array -> ByteArray: data := ByteArray 5 + registers.size * 2 - binary.BIG-ENDIAN.put-uint16 data 0 address - binary.BIG-ENDIAN.put-uint16 data 2 registers.size - binary.BIG-ENDIAN.put-uint8 data 4 registers.size * 2 + io.BIG-ENDIAN.put-uint16 data 0 address + io.BIG-ENDIAN.put-uint16 data 2 registers.size + io.BIG-ENDIAN.put-uint8 data 4 registers.size * 2 registers.size.repeat: - binary.BIG-ENDIAN.put-uint16 data (5 + it * 2) registers[it] + io.BIG-ENDIAN.put-uint16 data (5 + it * 2) registers[it] return data class WriteHoldingRegistersResponse extends Response: @@ -216,8 +216,8 @@ class WriteHoldingRegistersResponse extends Response: Response.check-frame_ ID frame Response.check-sized-data_ frame 4 - first-address = binary.BIG-ENDIAN.uint16 frame.data 0 - changes = binary.BIG-ENDIAN.uint16 frame.data 2 + first-address = io.BIG-ENDIAN.uint16 frame.data 0 + changes = io.BIG-ENDIAN.uint16 frame.data 2 class ReadRegistersRequest extends Request: static HOLDING-ID ::= 3 @@ -230,8 +230,8 @@ class ReadRegistersRequest extends Request: to-byte-array -> ByteArray: data := ByteArray 4 - binary.BIG-ENDIAN.put-uint16 data 0 address - binary.BIG-ENDIAN.put-uint16 data 2 register-count + io.BIG-ENDIAN.put-uint16 data 0 address + io.BIG-ENDIAN.put-uint16 data 2 register-count return data class ReadRegistersResponse extends Response: @@ -243,9 +243,9 @@ class ReadRegistersResponse extends Response: constructor.deserialize frame/Frame --holding/bool: Response.check-frame_ (holding ? HOLDING-ID : INPUT-ID) frame Response.check-sized-data_ frame --count-must-be-even - byte-count := binary.BIG-ENDIAN.uint8 frame.data 0 + byte-count := io.BIG-ENDIAN.uint8 frame.data 0 registers = List (byte-count / 2): - binary.BIG-ENDIAN.uint16 frame.data (1 + 2 * it) + io.BIG-ENDIAN.uint16 frame.data (1 + 2 * it) class ReportServerIdRequest extends Request: static ID ::= 17 @@ -265,7 +265,7 @@ class ReportServerIdResponse extends Response: constructor.deserialize frame/Frame: Response.check-frame_ ID frame Response.check-sized-data_ frame - byte-count := binary.BIG-ENDIAN.uint8 frame.data 0 + byte-count := io.BIG-ENDIAN.uint8 frame.data 0 if byte-count == 0: exception := ModbusException.other ModbusException.MISSING-INFORMATION @@ -287,9 +287,9 @@ class MaskWriteRegisterRequest extends Request: to-byte-array -> ByteArray: data := ByteArray 6 - binary.BIG-ENDIAN.put-uint16 data 0 address - binary.BIG-ENDIAN.put-uint16 data 2 and-mask - binary.BIG-ENDIAN.put-uint16 data 4 or-mask + io.BIG-ENDIAN.put-uint16 data 0 address + io.BIG-ENDIAN.put-uint16 data 2 and-mask + io.BIG-ENDIAN.put-uint16 data 4 or-mask return data class MaskWriteRegisterResponse extends Response: @@ -302,9 +302,9 @@ class MaskWriteRegisterResponse extends Response: constructor.deserialize frame/Frame: Response.check-frame_ ID frame Response.check-sized-data_ frame 6 - address = binary.BIG-ENDIAN.uint16 frame.data 0 - and-mask = binary.BIG-ENDIAN.uint16 frame.data 2 - or-mask = binary.BIG-ENDIAN.uint16 frame.data 4 + address = io.BIG-ENDIAN.uint16 frame.data 0 + and-mask = io.BIG-ENDIAN.uint16 frame.data 2 + or-mask = io.BIG-ENDIAN.uint16 frame.data 4 class WriteReadMultipleRegistersRequest extends Request: static ID ::= 23 @@ -318,13 +318,13 @@ class WriteReadMultipleRegistersRequest extends Request: to-byte-array -> ByteArray: data := ByteArray (9 + write-registers.size * 2) - binary.BIG-ENDIAN.put-uint16 data 0 read-address - binary.BIG-ENDIAN.put-uint16 data 2 read-register-count - binary.BIG-ENDIAN.put-uint16 data 4 write-address - binary.BIG-ENDIAN.put-uint16 data 6 write-registers.size - binary.BIG-ENDIAN.put-uint8 data 8 write-registers.size * 2 + io.BIG-ENDIAN.put-uint16 data 0 read-address + io.BIG-ENDIAN.put-uint16 data 2 read-register-count + io.BIG-ENDIAN.put-uint16 data 4 write-address + io.BIG-ENDIAN.put-uint16 data 6 write-registers.size + io.BIG-ENDIAN.put-uint8 data 8 write-registers.size * 2 write-registers.size.repeat: - binary.BIG-ENDIAN.put-uint16 data (9 + it * 2) write-registers[it] + io.BIG-ENDIAN.put-uint16 data (9 + it * 2) write-registers[it] return data class WriteReadMultipleRegistersResponse extends Response: @@ -335,6 +335,6 @@ class WriteReadMultipleRegistersResponse extends Response: constructor.deserialize frame/Frame: Response.check-frame_ ID frame Response.check-sized-data_ frame --count-must-be-even - byte-count := binary.BIG-ENDIAN.uint8 frame.data 0 + byte-count := io.BIG-ENDIAN.uint8 frame.data 0 registers = List (byte-count / 2): - binary.BIG-ENDIAN.uint16 frame.data (1 + 2 * it) + io.BIG-ENDIAN.uint16 frame.data (1 + 2 * it) diff --git a/src/rs485.toit b/src/rs485.toit index 66f2e9e..3ec1da8 100644 --- a/src/rs485.toit +++ b/src/rs485.toit @@ -3,9 +3,8 @@ // found in the LICENSE file. import binary +import io import log -import reader -import writer import rs485 import .exception import .framer @@ -18,13 +17,13 @@ class Rs485Transport implements Transport: framer/Framer rs485_/rs485.Rs485 - reader_/reader.BufferedReader - writer_/writer.Writer + reader_/io.Reader + writer_/io.Writer constructor .rs485_ --.framer=(RtuFramer --baud-rate=rs485_.baud-rate): - reader_ = reader.BufferedReader rs485_ - writer_ = writer.Writer rs485_ + reader_ = rs485_.in + writer_ = rs485_.out supports-parallel-sessions -> bool: return false @@ -69,7 +68,7 @@ class RtuFramer implements Framer: last-activity-us_ = Time.monotonic-us - read reader/reader.BufferedReader -> Frame?: + read reader/io.Reader -> Frame?: // RTU frames don't have any knowledge of how big they are. // Their size is determined by timing... // diff --git a/src/station.toit b/src/station.toit index ac492ee..ce4a2ce 100644 --- a/src/station.toit +++ b/src/station.toit @@ -2,7 +2,7 @@ // Use of this source code is governed by an MIT-style license that can be // found in the LICENSE file. -import binary +import io import log import monitor import net.tcp @@ -163,7 +163,7 @@ abstract class RegisterReader: register-count := (byte-count + 1) / 2 registers := read-many --address=address --register-count=register-count bytes := ByteArray byte-count - (byte-count / 2).repeat: binary.BIG-ENDIAN.put-uint16 bytes it * 2 registers[it] + (byte-count / 2).repeat: io.BIG-ENDIAN.put-uint16 bytes it * 2 registers[it] if byte-count % 2 == 1: bytes[bytes.size - 1] = registers.last >> 8 return bytes @@ -184,7 +184,7 @@ abstract class RegisterReader: read-float --address/int -> float: bytes := read-byte-array --address=address --byte-count=8 return float.from-bits - binary.BIG-ENDIAN.int64 bytes 0 + io.BIG-ENDIAN.int64 bytes 0 /** Reads a 32-bit float from the given $address. @@ -214,9 +214,9 @@ abstract class RegisterReader: read-uint32 --address/int -> int: registers := read-many --address=address --register-count=2 buffer := ByteArray 4 - binary.BIG-ENDIAN.put-uint16 buffer 0 registers[0] - binary.BIG-ENDIAN.put-uint16 buffer 2 registers[1] - return binary.BIG-ENDIAN.uint32 buffer 0 + io.BIG-ENDIAN.put-uint16 buffer 0 registers[0] + io.BIG-ENDIAN.put-uint16 buffer 2 registers[1] + return io.BIG-ENDIAN.uint32 buffer 0 /** Reads a signed 32-bit int from the given $address. @@ -226,9 +226,9 @@ abstract class RegisterReader: read-int32 --address/int -> int: registers := read-many --address=address --register-count=2 buffer := ByteArray 4 - binary.BIG-ENDIAN.put-uint16 buffer 0 registers[0] - binary.BIG-ENDIAN.put-uint16 buffer 2 registers[1] - return binary.BIG-ENDIAN.int32 buffer 0 + io.BIG-ENDIAN.put-uint16 buffer 0 registers[0] + io.BIG-ENDIAN.put-uint16 buffer 2 registers[1] + return io.BIG-ENDIAN.int32 buffer 0 class InputRegisters extends RegisterReader: constructor.internal_ station/Station: @@ -347,7 +347,7 @@ class HoldingRegisters extends RegisterReader: */ write-byte-array --address/int value/ByteArray -> none: registers := List (value.size + 1) / 2 - (value.size / 2).repeat: registers[it] = binary.BIG-ENDIAN.uint16 value it * 2 + (value.size / 2).repeat: registers[it] = io.BIG-ENDIAN.uint16 value it * 2 if value.size % 2 == 1: registers[registers.size - 1] = value.last write-many --address=address registers @@ -374,7 +374,7 @@ class HoldingRegisters extends RegisterReader: */ write-float --address/int value/float: buffer := ByteArray 8 - binary.BIG-ENDIAN.put-float64 buffer 0 value + io.BIG-ENDIAN.put-float64 buffer 0 value write-byte-array --address=address buffer @@ -385,8 +385,8 @@ class HoldingRegisters extends RegisterReader: */ write-uint32 --address/int value/int: buffer := ByteArray 4 - binary.BIG-ENDIAN.put-int32 buffer 0 value - registers := [binary.BIG-ENDIAN.uint16 buffer 0, binary.BIG-ENDIAN.uint16 buffer 2] + io.BIG-ENDIAN.put-int32 buffer 0 value + registers := [io.BIG-ENDIAN.uint16 buffer 0, io.BIG-ENDIAN.uint16 buffer 2] write-many --address=address registers /** diff --git a/src/tcp.toit b/src/tcp.toit index a93b114..32bd564 100644 --- a/src/tcp.toit +++ b/src/tcp.toit @@ -2,10 +2,8 @@ // Use of this source code is governed by an MIT-style license that can be // found in the LICENSE file. -import binary +import io import net.tcp -import reader -import writer import .transport import .framer @@ -14,12 +12,12 @@ class TcpTransport implements Transport: framer/Framer socket_/tcp.Socket - reader_/reader.BufferedReader - writer_/writer.Writer + reader_/io.Reader + writer_/io.Writer constructor .socket_ --.framer=TcpFramer: - reader_ = reader.BufferedReader socket_ - writer_ = writer.Writer socket_ + reader_ = socket_.in + writer_ = socket_.out supports-parallel-sessions -> bool: return true @@ -38,24 +36,24 @@ class TcpTransport implements Transport: class TcpFramer implements Framer: static HEADER-SIZE_ ::= 8 - read reader/reader.BufferedReader -> Frame?: - if not reader.can-ensure HEADER-SIZE_: return null + read reader/io.Reader -> Frame?: + if not reader.try-ensure-buffered HEADER-SIZE_: return null header := reader.read-bytes HEADER-SIZE_ - transaction-id := binary.BIG-ENDIAN.uint16 header 0 - length := binary.BIG-ENDIAN.uint16 header 4 - unit-id := binary.BIG-ENDIAN.uint8 header 6 - function-code := binary.BIG-ENDIAN.uint8 header 7 + transaction-id := io.BIG-ENDIAN.uint16 header 0 + length := io.BIG-ENDIAN.uint16 header 4 + unit-id := io.BIG-ENDIAN.uint8 header 6 + function-code := io.BIG-ENDIAN.uint8 header 7 data := reader.read-bytes length - 2 return Frame --transaction-id=transaction-id --unit-id=unit-id --function-code=function-code --data=data - write frame/Frame writer: + write frame/Frame writer/io.Writer: // It is important to send the data in one go. // Modbus TCP should allow fragmented packets, but many devices do not. // By sending the data in one go, we minimize the risk of fragmentation. bytes := ByteArray HEADER-SIZE_ + frame.data.size - binary.BIG-ENDIAN.put-uint16 bytes 0 frame.transaction-id - binary.BIG-ENDIAN.put-uint16 bytes 4 frame.data.size + 2 - binary.BIG-ENDIAN.put-uint8 bytes 6 frame.unit-id - binary.BIG-ENDIAN.put-uint8 bytes 7 frame.function-code + io.BIG-ENDIAN.put-uint16 bytes 0 frame.transaction-id + io.BIG-ENDIAN.put-uint16 bytes 4 frame.data.size + 2 + io.BIG-ENDIAN.put-uint8 bytes 6 frame.unit-id + io.BIG-ENDIAN.put-uint8 bytes 7 frame.function-code bytes.replace HEADER-SIZE_ frame.data writer.write bytes diff --git a/tests/package.lock b/tests/package.lock index 9fa1deb..f21ffb7 100644 --- a/tests/package.lock +++ b/tests/package.lock @@ -1,4 +1,4 @@ -sdk: ^2.0.0-alpha.64 +sdk: ^2.0.0-alpha.144 prefixes: host: pkg-host modbus: .. @@ -10,10 +10,10 @@ packages: pkg-host: url: github.com/toitlang/pkg-host name: host - version: 1.11.0 - hash: 7e7df6ac70d98a02f232185add81a06cec0d77e8 + version: 1.15.1 + hash: ff187c2c19d695e66c3dc1d9c09b4dc6bec09088 toit-rs485: url: github.com/toitware/toit-rs485 name: rs485 - version: 1.2.0 - hash: 8e38736d19dab1d9275568df269f982e6118acec + version: 1.3.0 + hash: fc9d92d11ccb95816a0029140585b2a9bddc93a7 diff --git a/tests/package.yaml b/tests/package.yaml index 56e5927..9878ad3 100644 --- a/tests/package.yaml +++ b/tests/package.yaml @@ -1,6 +1,6 @@ dependencies: host: url: github.com/toitlang/pkg-host - version: ^1.11.0 + version: ^1.15.1 modbus: path: ..