From ae228d78e26624a551b2b057e68dfec74b09c76f Mon Sep 17 00:00:00 2001 From: CCXXXI Date: Mon, 11 Jul 2022 17:22:17 +0800 Subject: [PATCH] feat: something after test --- TcpClient.vb | 18 +++++++++++++++++- frmMain.vb | 12 ++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/TcpClient.vb b/TcpClient.vb index 2baac97..33362d3 100644 --- a/TcpClient.vb +++ b/TcpClient.vb @@ -1,10 +1,13 @@ -Imports System.Net.Sockets +Imports System.Linq +Imports System.Net.Sockets Public Class TcpClient Const Host As String = "localhost" Const Port As Integer = 8000 ReadOnly _readCmd As Byte() = New Byte() {&HFE, &H2, &H0, &H0, &H0, &H8, &H6D, &HC3} Const YesCode As Byte = &H1 + ReadOnly _report As Byte() = New Byte() {&HFE, &H5, &H0, &H0, &HFF, &H0, &H98, &H35} + ReadOnly _expectResponse As Byte() = New Byte() {&HFE, &H5, &H0, &H0, &HFF, &H0, &H98, &H35} ReadOnly _tcpClient As New Net.Sockets.TcpClient() Dim _networkStream As NetworkStream @@ -34,4 +37,17 @@ Public Class TcpClient ' check Return bytes(3) = YesCode End Function + + Public Function Report() As Boolean + _networkStream.Write(_report, 0, _report.Length) + + Dim bytes(_tcpClient.ReceiveBufferSize) As Byte + _networkStream.Read(bytes, 0, CInt(_tcpClient.ReceiveBufferSize)) + + Return bytes.SequenceEqual(_expectResponse) + End Function + + Public Function Connected() As Boolean + Return _tcpClient.Connected + End Function End Class diff --git a/frmMain.vb b/frmMain.vb index 1a6fceb..ba6cfda 100644 --- a/frmMain.vb +++ b/frmMain.vb @@ -2096,6 +2096,11 @@ LocalErrorHandler: 'finally, set up the focus appropriately on the user interface Me.Cursor = mnOldMousePointer ConfigButtonsBeforeRun() + + If _client.Connected Then + FixBox.BackColor = IIf(_client.Report(), Color.Red, Color.Yellow) + End If + InitSerialInput() Exit Sub LocalErrorHandler: @@ -2320,16 +2325,15 @@ LocalErrorHandler: txtSerialNumber.Text = "" End Sub + ReadOnly _client As New TcpClient() Private Sub BackgroundThread() - Dim client As New TcpClient() - - If Not client.Connect() Then + If Not _client.Connect() Then Return End If SysBox.BackColor = Color.Green While True - If client.Check() Then + If _client.Check() Then FixBox.BackColor = Color.Green Invoke(Sub() cmdRun_Click(cmdRun, New System.EventArgs)