YHGW/serial_init.go

96 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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])
}
// 解析串口配置
if conf.OPENSERIAL {
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:",SERIAL_PORT)
}
// 遍历设备对应的串口对应的端口并放到相应的run_port_device方法
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 {}
}
// 轮询相同串口的设备放到对应的worker
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)
}
}