From 095e5e87f9f9c071ca8a291a6015ba3979070383 Mon Sep 17 00:00:00 2001 From: fengyarnom Date: Wed, 28 May 2025 17:00:03 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E8=BF=9E=E6=8E=A5=E5=90=8E?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=8F=91=E9=80=81=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tcp_server.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) 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 {