Skip to content

Commit 2b6610e

Browse files
committed
Allow setting of unit ID
1 parent 6b8e013 commit 2b6610e

5 files changed

Lines changed: 79 additions & 27 deletions

File tree

app.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,21 @@ func (a *App) Connect(ip string, port int, unitID int) error {
4343
a.SetConnected(false)
4444
return err
4545
}
46+
47+
err = client.SetUnitId(uint8(unitID))
48+
if err != nil {
49+
a.SetConnected(false)
50+
return err
51+
}
4652
a.SetConnected(true)
4753
fmt.Println("Connected")
4854
return nil
4955
}
5056

57+
func (a *App) SetUnitId(id uint8) error {
58+
return a.client.SetUnitId(id)
59+
}
60+
5161
func (a *App) Disconnect() {
5262
if a.connected {
5363
a.client.Close()
@@ -70,11 +80,12 @@ func (a *App) Read(inputType string, address uint16, quantity uint16) ([]modbusD
7080
return nil, errors.New("Client is not configured.")
7181
}
7282
if !a.connected {
73-
err := a.client.Open()
74-
if err != nil {
75-
return nil, err
76-
}
77-
a.SetConnected(true)
83+
// err := a.client.Open()
84+
// if err != nil {
85+
// return nil, err
86+
// }
87+
// a.SetConnected(true)
88+
return nil, errors.New("Disconnected from server.")
7889
}
7990
var regType modbus.RegType
8091
var results []uint16

frontend/package.json.md5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
acd9ead003bbb277dd239c88e008d2d6
1+
f5c81e946fc2887a31af38e73110e01e
Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,70 @@
11
import { Button, NumberInput, TextInput } from "@mantine/core";
2-
import { Connect, Disconnect } from "../../wailsjs/go/main/App";
2+
import { SetUnitId, Connect, Disconnect } from "../../wailsjs/go/main/App";
33
import { useState } from "react";
44
import { notifications } from "@mantine/notifications";
55

66
export default function ConnectForm(props: { connected: boolean }) {
77
const [IP, setIP] = useState<string>("");
88
const [port, setPort] = useState<string | number>(502);
9-
const [unitID, setUnitID] = useState<number>(1);
9+
const [unitID, setUnitID] = useState<string | number>(1);
1010
function ConnectModbus() {
1111
console.log("Connecting to", IP, port, unitID);
1212
let portNum = port as number;
13-
Connect(IP, portNum, unitID)
14-
.then(() => notifications.show({
15-
title: "Connected",
16-
message: ""
17-
}))
18-
.catch((err) => notifications.show({
19-
title: "Failed to Connect",
20-
message: err,
21-
autoClose: 20000
22-
}))
13+
let numId = unitID as number;
14+
Connect(IP, portNum, numId)
15+
.then(() =>
16+
notifications.show({
17+
title: "Connected",
18+
message: "",
19+
})
20+
)
21+
.catch((err) =>
22+
notifications.show({
23+
title: "Failed to Connect",
24+
message: err,
25+
autoClose: 20000,
26+
})
27+
);
2328
}
2429
return (
2530
<>
26-
<TextInput label="IP Address" placeholder="192.168.1.100" onChange={(event) => setIP(event.currentTarget.value)} />
27-
<NumberInput label="Port" value={502} onChange={setPort} />
28-
{/* <NumberInput label="Unit ID" value={1} /> */}
29-
{
30-
!props.connected ?
31-
<Button onClick={ConnectModbus}>Connect</Button> : <Button onClick={Disconnect}>Disconnect</Button>
32-
}
31+
<TextInput
32+
label="IP Address"
33+
placeholder="192.168.1.100"
34+
onChange={(event) => setIP(event.currentTarget.value)}
35+
disabled={props.connected}
36+
/>
37+
<NumberInput
38+
label="Port"
39+
value={port}
40+
onChange={setPort}
41+
disabled={props.connected}
42+
/>
43+
<NumberInput
44+
label="Unit ID"
45+
value={unitID}
46+
onChange={(v) => {
47+
setUnitID(v);
48+
SetUnitId(v as number)
49+
.then(() =>
50+
notifications.show({
51+
title: "Set Unit ID",
52+
message: "",
53+
})
54+
)
55+
.catch((err) =>
56+
notifications.show({
57+
title: "Failed to Set ID",
58+
message: err,
59+
})
60+
);
61+
}}
62+
/>
63+
{!props.connected ? (
64+
<Button onClick={ConnectModbus}>Connect</Button>
65+
) : (
66+
<Button onClick={Disconnect}>Disconnect</Button>
67+
)}
3368
</>
34-
)
35-
}
69+
);
70+
}

frontend/wailsjs/go/main/App.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ export function Read(arg1:string,arg2:number,arg3:number):Promise<Array<main.mod
1010

1111
export function SetConnected(arg1:boolean):Promise<void>;
1212

13+
export function SetUnitId(arg1:number):Promise<void>;
14+
1315
export function Write(arg1:string,arg2:number,arg3:Array<number>):Promise<void>;

frontend/wailsjs/go/main/App.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export function SetConnected(arg1) {
1818
return window['go']['main']['App']['SetConnected'](arg1);
1919
}
2020

21+
export function SetUnitId(arg1) {
22+
return window['go']['main']['App']['SetUnitId'](arg1);
23+
}
24+
2125
export function Write(arg1, arg2, arg3) {
2226
return window['go']['main']['App']['Write'](arg1, arg2, arg3);
2327
}

0 commit comments

Comments
 (0)