Skip to content

Commit ae77241

Browse files
committed
Well actually return coil values when reading them.. + Minor touch-ups
1 parent 7d51012 commit ae77241

File tree

5 files changed

+40
-12
lines changed

5 files changed

+40
-12
lines changed

src-tauri/src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ struct ModbusState {
3535

3636
#[tauri::command]
3737
fn update_coil(app_handle: AppHandle, switch_id: u16, state: bool) -> Result<bool, String> {
38-
println!("Updating coil: {} to {}", switch_id, state);
38+
// TODO: Handle errors.
3939
let modbus_state = app_handle.state::<ModbusState>();
40-
4140
let mut coils = modbus_state.coils.lock().unwrap();
4241
coils.insert(switch_id, state);
4342
let value = coils.get(&switch_id).unwrap();
@@ -68,6 +67,9 @@ async fn main() {
6867

6968

7069
fn setup(app_handle: AppHandle, fan_ids: Vec<u8>, server: ModbusServer) {
70+
/*
71+
TODO: Grab state from the app handle instead.
72+
*/
7173
let fan_ids_shared = Arc::new(Mutex::new(fan_ids));
7274

7375
let evt_clone = fan_ids_shared.clone();

src-tauri/src/modbus/server.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl tokio_modbus::server::Service for ModbusServer {
3333
}
3434
Request::ReadCoils(addr, cnt) => {
3535
match coil_read(&self.coils.lock().unwrap(), addr, cnt) {
36-
Ok(values) => future::ready(Ok(Response::ReadCoils(Vec::new()))),
36+
Ok(values) => future::ready(Ok(Response::ReadCoils(values))),
3737
Err(err) => future::ready(Err(err)),
3838
}
3939
}
@@ -128,6 +128,22 @@ impl ModbusServer {
128128
let mut input_registers = HashMap::new();
129129
let mut holding_registers = HashMap::new();
130130
let mut coils = HashMap::new();
131+
132+
// Initialize coils
133+
for i in 0..1000 {
134+
coils.insert(i, false);
135+
}
136+
137+
// Initialize holding registers
138+
for i in 0..1000 {
139+
holding_registers.insert(i, 0);
140+
}
141+
142+
// Initialize input registers
143+
for i in 0..1000 {
144+
input_registers.insert(i, 0);
145+
}
146+
131147
Self {
132148
input_registers: Arc::new(Mutex::new(input_registers)),
133149
holding_registers: Arc::new(Mutex::new(holding_registers)),

src-tauri/tauri.conf.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"package": {
99
"productName": "modbus-sim",
10-
"version": "0.0.0"
10+
"version": "0.0.1"
1111
},
1212
"tauri": {
1313
"allowlist": {

src/App.vue

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import Coil from "./components/Coil.vue";
55

66
<template>
77
<div class="container">
8-
<SimulatedFan id="1" />
9-
<SimulatedFan id="2" />
8+
<div class="simulation-container">
9+
<div v-for="x in 3">
10+
<SimulatedFan :id="x-1" />
11+
</div>
12+
</div>
1013

1114
<div class="simulation-container">
1215
<div v-for="x in 10">
13-
<Coil :id="x" />
16+
<Coil :id="x-1" />
1417
</div>
1518
</div>
1619

src/styles.css

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
-webkit-font-smoothing: antialiased;
1313
-moz-osx-font-smoothing: grayscale;
1414
-webkit-text-size-adjust: 100%;
15+
/* no-select */
16+
user-select: none;
17+
-webkit-user-select: none;
1518
}
1619

1720
.container {
@@ -74,6 +77,7 @@ button {
7477
button:hover {
7578
border-color: #396cd8;
7679
}
80+
7781
button:active {
7882
border-color: #396cd8;
7983
background-color: #e8e8e8;
@@ -103,6 +107,7 @@ button {
103107
color: #ffffff;
104108
background-color: #0f0f0f98;
105109
}
110+
106111
button:active {
107112
background-color: #0f0f0f69;
108113
}
@@ -112,6 +117,7 @@ button {
112117
0% {
113118
transform: rotate(0deg);
114119
}
120+
115121
100% {
116122
transform: rotate(360deg);
117123
}
@@ -129,10 +135,9 @@ button {
129135
flex-direction: column;
130136
justify-content: center;
131137
align-items: start;
132-
133-
background-color: #184fb5;
138+
background-color: #494d55;
134139
border-width: 0.25rem;
135-
140+
136141
padding: 0.5rem;
137142
box-shadow: 0 0 0.1rem 0.05rem #2f2f2f;
138143
}
@@ -153,7 +158,7 @@ button {
153158
width: fit-content;
154159
height: fit-content;
155160
overflow: hidden;
156-
background-color: #1b51b6;
161+
background-color: #494d55;
157162
margin: 0.25rem;
158163
}
159164

@@ -176,7 +181,7 @@ button {
176181
justify-content: center;
177182
align-items: start;
178183

179-
background-color: #184fb5;
184+
background-color: #494d55;
180185
border-width: 0.25rem;
181186
border-radius: 0.25rem;
182187
padding: 0.5rem;
@@ -192,13 +197,15 @@ button {
192197
width: 15px;
193198
height: 15px;
194199
border-radius: 50%;
200+
border: 2px solid #000000;
195201
background-color: #fa0303;
196202
margin: 0.25rem;
197203
display: inline-block;
198204
}
199205

200206
.coil-state {
201207
display: flex;
208+
align-items: center;
202209
}
203210

204211
.coil_active {

0 commit comments

Comments
 (0)