Skip to content

Commit 27ca918

Browse files
committed
Initial commit
0 parents  commit 27ca918

4 files changed

+147
-0
lines changed

bin/jan_modbus_ip_mac_0.01.2

2.67 MB
Binary file not shown.

bin/jan_modbus_ip_mac_0.01.3

2.86 MB
Binary file not shown.

bin/jan_modbus_ip_rpi_0.01.3

2.67 MB
Binary file not shown.

main.go

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package main
2+
3+
import (
4+
"encoding/binary"
5+
"flag"
6+
"fmt"
7+
"github.com/goburrow/modbus"
8+
"math"
9+
"time"
10+
)
11+
12+
//type a map.s.int32
13+
14+
/*
15+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
16+
!!!!!!!!!!!! VERSION !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
17+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
18+
*/
19+
const version = "0.01.3"
20+
21+
/*
22+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
23+
!!!!!!!!!!!! VERSION !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
24+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
25+
*/
26+
27+
func main() {
28+
paramName := [...]string{
29+
"19000_G_ULN[0]",
30+
"19002_G_ULN[1]",
31+
"19004_G_ULN[2]",
32+
"19006_G_ULL[0]",
33+
"19008_G_ULL[1]",
34+
"19010_G_ULL[2]",
35+
"19012_G_ILN[0]",
36+
"19014_G_ILN[1]",
37+
"19016_G_ILN[2]",
38+
"19018_G_I_SUM3",
39+
"19020_G_PLN[0]",
40+
"19022_G_PLN[1]",
41+
"19024_G_PLN[2]",
42+
"19026_G_P_SUM3",
43+
"19028_G_SLN[0]",
44+
"19030_G_SLN[1]",
45+
"19032_G_SLN[2]",
46+
"19034_G_S_SUM3",
47+
"19036_G_QLN[0]",
48+
"19038_G_QLN[1]",
49+
"19040_G_QLN[2]",
50+
"19042_G_Q_SUM3",
51+
"19044_G_COS_PHI[0]",
52+
"19046_G_COS_PHI[1]",
53+
"19048_G_COS_PHI[2]",
54+
"19050_G_FREQ",
55+
"19052_G_PHASE_SEQ",
56+
"19054_G_WH[0]",
57+
"19056_G_WH[1]",
58+
"19058_G_WH[2]",
59+
"19060_G_WH_SUML13",
60+
"19062_G_WH_V[0]",
61+
"19064_G_WH_V[1]",
62+
"19066_G_WH_V[2]",
63+
"19068_G_WH_V_HT_SUML13",
64+
"19070_G_WH_Z[0]",
65+
"19072_G_WH_Z[1]",
66+
"19074_G_WH_Z[2]",
67+
"19076_G_WH_Z_SUML13",
68+
"19078_G_WH_S[0]",
69+
"19080_G_WH_S[1]",
70+
"19082_G_WH_S[2]",
71+
"19084_G_WH_S_SUML13",
72+
"19086_G_QH[0]",
73+
"19088_G_QH[1]",
74+
"19090_G_QH[2]",
75+
"19092_G_QH_SUML13",
76+
"19094_G_IQH[0]",
77+
"19096_G_IQH[1]",
78+
"19098_G_IQH[2]",
79+
"19100_G_IQH_SUML13",
80+
"19102_G_CQH[0]",
81+
"19104_G_CQH[1]",
82+
"19106_G_CQH[2]",
83+
"19108_G_CQH_SUML13",
84+
"19110_G_THD_ULN[0]",
85+
"19112_G_THD_ULN[1]",
86+
"19114_G_THD_ULN[2]",
87+
"19116_G_THD_ILN[0]",
88+
"19118_G_THD_ILN[1]",
89+
"19120_G_THD_ILN[2]",
90+
}
91+
var data []float32
92+
93+
addressIP := flag.String("ip", "localhost", "a string")
94+
tcpPort := flag.String("port", "502", "a string")
95+
slaveID := flag.Int("id", 1, "an int")
96+
regQuantity := flag.Uint("q", 61, "an uint")
97+
flag.Parse()
98+
serverParam := fmt.Sprint(*addressIP, ":", *tcpPort)
99+
s := byte(*slaveID)
100+
101+
// fmt.Println(serverParam)
102+
103+
handler := modbus.NewTCPClientHandler(serverParam)
104+
handler.SlaveId = s
105+
handler.Timeout = 2 * time.Second
106+
// Connect manually so that multiple requests are handled in one session
107+
err := handler.Connect()
108+
defer handler.Close()
109+
client := modbus.NewClient(handler)
110+
111+
results, err := client.ReadInputRegisters(19000, uint16(*regQuantity)*2)
112+
if err != nil {
113+
fmt.Printf("{\"status\":\"error\", \"error\":\"%s\"}", err)
114+
//fmt.Printf("%s\n", err)
115+
}
116+
117+
//fmt.Println(len(results))
118+
//fmt.Println(results)
119+
i := 0
120+
for i < len(results) {
121+
a := Float32frombytes(results[i : i+4])
122+
if math.IsNaN(float64(a)) {
123+
data = append(data, 0)
124+
} else {
125+
data = append(data, a)
126+
}
127+
i += 4
128+
}
129+
130+
for l := 0; l < len(data); l++ {
131+
if l == 0 {
132+
fmt.Printf("{ \"%s\": ", paramName[l])
133+
} else {
134+
fmt.Printf(", \"%s\": ", paramName[l])
135+
}
136+
fmt.Print(data[l])
137+
}
138+
if len(results) != 0 {
139+
fmt.Printf(", \"version\": \"%s\"}", version)
140+
}
141+
}
142+
143+
func Float32frombytes(bytes []byte) float32 {
144+
bits := binary.BigEndian.Uint32(bytes)
145+
float := math.Float32frombits(bits)
146+
return float
147+
}

0 commit comments

Comments
 (0)