diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7d2df78..b913b34 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,9 +13,9 @@ jobs: os: [ ubuntu-latest, macos-latest ] # The versions should contain (at least) the lowest requirement # and a version that is more up to date. - toit-version: [ v2.0.0-alpha.118, latest ] + toit-version: [ v2.0.0-alpha.144, latest ] include: - - toit-version: v2.0.0-alpha.118 + - toit-version: v2.0.0-alpha.144 version-name: old - toit-version: latest version-name: new diff --git a/src/protocol.toit b/src/protocol.toit index 60dff06..5865dee 100644 --- a/src/protocol.toit +++ b/src/protocol.toit @@ -74,6 +74,19 @@ class Response: --frame=frame throw exception +class RawRequest extends Request: + payload/ByteArray + constructor function-code/int .payload: + super function-code + to-byte-array -> ByteArray: + return payload + +class RawResponse extends Response: + bits/ByteArray + constructor.deserialize frame/Frame function-code/int: + Response.check-frame_ function-code frame + bits = frame.data + class ReadBitsRequest extends Request: static COILS-ID ::= 1 static DISCRETE-INPUTS-ID ::= 2 diff --git a/src/station.toit b/src/station.toit index ce4a2ce..cf7ac2d 100644 --- a/src/station.toit +++ b/src/station.toit @@ -81,7 +81,7 @@ class Station: return holding-registers_ /** - Reads the server identifaction. + Reads the server identification. This function is only available for serial Modbus devices, although it may also work on some Modbus TCP devices. */ @@ -94,6 +94,21 @@ class Station: server-response := response as ReportServerIdResponse return ServerIdResponse server-response.server-id server-response.on-off + /** + Reads a raw byte response for a specific $function-code. + + The request may optionally include a $payload of bytes and the + result is the raw, unframed bytes in the server response. + */ + read-raw --function-code/int --payload/ByteArray=#[] -> ByteArray: + if is-broadcast: throw "Can't read from broadcast station" + logger_.debug "read-raw" --tags={"unit_id": unit-id, "function_code": function-code} + request := RawRequest function-code payload + response := bus_.send_ request --unit-id=unit-id --is-broadcast=false: + RawResponse.deserialize it function-code + raw-response := response as RawResponse + return raw-response.bits + /** A register reader for Modbus stations. diff --git a/tests/test_server.toit b/tests/test_server.toit index 074cd04..f885fd4 100644 --- a/tests/test_server.toit +++ b/tests/test_server.toit @@ -60,7 +60,7 @@ with-test-server --logger/log.Logger --mode/string [block]: // If it didn't start we might be looking for the wrong line in its output. // There was a change between 1.6.9 and 2.0.14. Could be that there is // going to be another one. - with-timeout --ms=1_000: + with-timeout --ms=5_000: server-is-running.get network := net.open