-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added reverseEngineering sample code for HM310T
- Loading branch information
Patrick Stein
authored and
Patrick Stein
committed
Oct 22, 2022
1 parent
67ae473
commit f3a8ac4
Showing
1 changed file
with
40 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,47 @@ | ||
import XCTest | ||
import SwiftLibModbus | ||
|
||
@testable import modbus2mqtt | ||
|
||
final class modbus2mqttTests: XCTestCase { | ||
func testExample() throws { | ||
// This is an example of a functional test case. | ||
// Use XCTAssert and related functions to verify your tests produce the correct | ||
// results. | ||
XCTFail() | ||
func testReverseEngineerHM310T() async throws { | ||
// Prints out modbus address ranges and compares them to the last time | ||
|
||
|
||
let modbusDevice = try ModbusDevice(device: "/dev/tty.usbserial-42340",baudRate:9600) | ||
let stripesize = 0x10 | ||
|
||
var store = [Int:[UInt16]]() | ||
let emptyline = [UInt16](repeating:0, count:stripesize) | ||
|
||
func readData(from address:Int) async throws | ||
{ | ||
let data:[UInt16] = try await modbusDevice.readRegisters(from: address, count: stripesize, type: .holding) | ||
|
||
let previous:[UInt16] = store[address] ?? emptyline | ||
|
||
if data != previous | ||
{ | ||
print("\( String(format:"%04x", address) ): \( data.map{ $0 == 0 ? " - " : String(format:"%04x ",$0) }.joined(separator:" ") ) ") | ||
print("\( String(format:"%04x", address) ): \( data.map{ $0 == 0 ? " " : String(format:"%05d ",$0) }.joined(separator:" ") ) ") | ||
print("") | ||
store[address] = data | ||
} | ||
} | ||
|
||
for address in stride(from: 0x000, to: 0xFFFF, by: stripesize) | ||
{ | ||
try await readData(from: address) | ||
} | ||
|
||
for _ in 0...20 | ||
{ | ||
print("WRAPAROUND") | ||
|
||
|
||
for address in store.keys | ||
{ | ||
try await readData(from: address) | ||
} | ||
} | ||
} | ||
} |