85 lines
2.6 KiB
Go
85 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"github.com/tarm/serial"
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
func serial_run() {
|
|
SERIAL_PORT := make(map[string]*serial.Port)
|
|
ports_arr := make([]string, 0)
|
|
port_arr := make([]string, 0)
|
|
for _, i := range conf.DEVICES_LIST {
|
|
ports_arr = append(ports_arr, i[1])
|
|
}
|
|
for i := 0; i < len(ports_arr); i++ {
|
|
if (i > 0 && ports_arr[i-1] == ports_arr[i]) || len(ports_arr[i]) == 0 {
|
|
continue
|
|
}
|
|
port_arr = append(port_arr, ports_arr[i])
|
|
}
|
|
for _, i := range port_arr {
|
|
var PARITY serial.Parity
|
|
switch conf.SERIALPORTS_LIST[i].PARITY {
|
|
case "N":
|
|
PARITY = 'N'
|
|
case "E":
|
|
PARITY = 'E'
|
|
}
|
|
SERIAL_PORT[i], err = serial.OpenPort(&serial.Config{
|
|
Name: conf.SERIALPORTS_LIST[i].COM,
|
|
Baud: conf.SERIALPORTS_LIST[i].BAUDS,
|
|
Parity: PARITY,
|
|
ReadTimeout: time.Millisecond * time.Duration(conf.SERIALPORTS_LIST[i].READTIMEOUT),
|
|
// ReadTimeout: time.Second * time.Duration(conf.SERIALPORTS_LIST[i].READTIMEOUT),
|
|
})
|
|
check(err)
|
|
defer SERIAL_PORT[i].Close()
|
|
}
|
|
if conf.DEBUG {
|
|
log.Println(SERIAL_PORT)
|
|
}
|
|
for j, k := range SERIAL_PORT {
|
|
var the_same_port_devices [][]string
|
|
for _, i := range conf.DEVICES_LIST {
|
|
if j == i[1] {
|
|
the_same_port_devices = append(the_same_port_devices, i)
|
|
}
|
|
}
|
|
go run_port_device(k, the_same_port_devices)
|
|
}
|
|
for i, k := range SERIAL_PORT {
|
|
if conf.SERIALPORTS_LIST[i].NOTE == "lorawan" {
|
|
yunhorn_lorawan_l_v1(k)
|
|
}
|
|
}
|
|
select {}
|
|
}
|
|
|
|
func run_port_device(port *serial.Port, the_same_port_devices [][]string) {
|
|
for {
|
|
for _, i := range the_same_port_devices {
|
|
switch i[2] {
|
|
case "yunhorn_sb_c_v1":
|
|
yunhorn_sb_c_v1(port, i[0], conf.RS485DEVICES[i[0]].COMMAND, conf.RS485DEVICES[i[0]].NUM)
|
|
case "yunhorn_db_c_v1":
|
|
yunhorn_db_c_v1(port, i[0], conf.RS485DEVICES[i[0]].COMMAND, conf.RS485DEVICES[i[0]].NUM)
|
|
case "yunhorn_kq_c_v1":
|
|
yunhorn_kq_c_v1(port, i[0], conf.RS485DEVICES[i[0]].COMMAND, conf.RS485DEVICES[i[0]].NUM)
|
|
case "yunhorn_lhq_c_v1":
|
|
yunhorn_lhq_c_v1(port, i[0], conf.RS485DEVICES[i[0]].COMMAND, conf.RS485DEVICES[i[0]].NUM)
|
|
case "yunhorn_aq_c_v1":
|
|
yunhorn_aq_c_v1(port, i[0], conf.RS485DEVICES[i[0]].COMMAND, conf.RS485DEVICES[i[0]].NUM)
|
|
case "yunhorn_kgl_c8_v1":
|
|
yunhorn_kgl_c8_v1(port, i[0], conf.RS485DEVICES[i[0]].COMMAND, conf.RS485DEVICES[i[0]].NUM)
|
|
case "yunhorn_kgl_c16_v1":
|
|
yunhorn_kgl_c16_v1(port, i[0], conf.RS485DEVICES[i[0]].COMMAND, conf.RS485DEVICES[i[0]].NUM)
|
|
case "yunhorn_xsy_l_v1":
|
|
yunhorn_xsy_l_v1(port, i[0], conf.RS485DEVICES[i[0]].COMMAND, conf.RS485DEVICES[i[0]].NUM)
|
|
}
|
|
}
|
|
time.Sleep(time.Duration(1) * time.Millisecond)
|
|
}
|
|
}
|