diff --git a/tcp_server.go b/tcp_server.go index a376666..5965566 100644 --- a/tcp_server.go +++ b/tcp_server.go @@ -61,6 +61,17 @@ func handleConnection(conn net.Conn) { clientConns[remoteAddr] = conn clientsMutex.Unlock() + // 设备刚连接时立即发送一次查询指令 + go func() { + time.Sleep(2 * time.Second) // 等待2秒让连接稳定 + command := "@1602301014A*0!\n" + if _, err := conn.Write([]byte(command)); err != nil { + Logger.Printf("发送连接后查询指令到客户端 %s 失败: %v", remoteAddr, err) + } else { + TCPDataLogger.Printf("发送连接后查询指令到客户端 %s: %s", remoteAddr, strings.TrimSpace(command)) + } + }() + // 启动定时发送指令的goroutine go sendPeriodicCommand(conn, remoteAddr) @@ -113,9 +124,9 @@ func handleConnection(conn net.Conn) { } } -// sendPeriodicCommand 每30分钟发送一次查询指令 +// sendPeriodicCommand 每5分钟发送一次查询指令 func sendPeriodicCommand(conn net.Conn, remoteAddr string) { - ticker := time.NewTicker(30 * time.Minute) + ticker := time.NewTicker(5 * time.Minute) defer ticker.Stop() for {