Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to new IO library. #26

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions examples/package.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sdk: ^2.0.0-alpha.21
sdk: ^2.0.0-alpha.144
prefixes:
modbus: ..
rs485: toit-rs485
Expand All @@ -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
2 changes: 1 addition & 1 deletion examples/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ dependencies:
path: ..
rs485:
url: github.com/toitware/toit-rs485
version: ^1.0.1
version: ^1.3.0
6 changes: 3 additions & 3 deletions package.lock
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions package.yaml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions src/framer.toit
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

82 changes: 41 additions & 41 deletions src/protocol.toit
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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)
13 changes: 6 additions & 7 deletions src/rs485.toit
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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...
//
Expand Down
26 changes: 13 additions & 13 deletions src/station.toit
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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

Expand All @@ -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


Expand All @@ -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

/**
Expand Down
Loading
Loading