Skip to content

Commit

Permalink
Add Station.read-raw. (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperl authored Sep 4, 2024
1 parent 2ab340e commit 05d20c7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/protocol.toit
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion src/station.toit
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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.
Expand Down

0 comments on commit 05d20c7

Please sign in to comment.