Skip to content

Commit

Permalink
Added reverseEngineering sample code for HM310T
Browse files Browse the repository at this point in the history
  • 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.
46 changes: 40 additions & 6 deletions Tests/modbus2mqttTests/modbus2mqttTests.swift
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)
}
}
}
}

0 comments on commit f3a8ac4

Please sign in to comment.