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

Add Station.read-raw. #29

Merged
merged 2 commits into from
Sep 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
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
Loading