Imports System.IO.Ports
Imports System.Threading

Public Class CommuniNode
    Public G_SerialPort As SerialPort
    Public G_TXTcolor As Color
    Public G_msgqueue As Queue(Of (String, Color, String))
    Public G_aliasname As String
    Public G_ShowHex As Boolean = False
    Public G_SendHex As Boolean = False
    Public G_ReadFlag As Boolean = True

    Public Sub New(serialname As String, baudrate As Integer, aliasname As String, TXTcolor As Color, msgqueue As Queue(Of (String, Color, String)))
        '创建串口(serialname, baudrate)
        G_SerialPort = New SerialPort(serialname, baudrate)
        '设置串口颜色
        G_aliasname = aliasname
        G_TXTcolor = TXTcolor
        G_msgqueue = msgqueue
        recvBufferli = New List(Of Byte)()
        '关联接收事件()
        AddHandler G_SerialPort.DataReceived, AddressOf SerialPort_DataReceived


    End Sub

    '设置别名和颜色
    Public Sub SetAliasAndColor(aliasname As String, TXTcolor As Color)
        G_aliasname = aliasname
        G_TXTcolor = TXTcolor

    End Sub

    '设置串口开关
    Public Sub SetSerialPortOpen(isOpen As Boolean)
        If IsNothing(G_SerialPort) Then Return
        If isOpen Then
            If Not G_SerialPort.IsOpen Then
                G_SerialPort.Open()
            End If

        Else
            G_SerialPort.Close()
        End If
        recvBufferli.Clear()
    End Sub

    '设置G_ShowHex
    Public Sub SetShowHex(isShowHex As Boolean)
        G_ShowHex = isShowHex
    End Sub
    '设置G_SendHex
    Public Sub SetSendHex(isSendHex As Boolean)
        G_SendHex = isSendHex
    End Sub

    Private _recvBuffer(4095) As Byte
    Public recvBufferli As List(Of Byte)
    Public recvBufferliindex As Integer = 0
    Private Sub SerialPort_DataReceived(sender As Object, e As SerialDataReceivedEventArgs)
        Static bytes As Integer = 0
        Dim sp As SerialPort = DirectCast(sender, SerialPort)
        Dim dstr As String
        Do
            bytes = sp.BytesToRead
            'If bytes <= 0 Then
            '    isListen = False
            '    Exit Sub
            'End If
            'If bytes + _recvOffset >= 4096 Then

            '    'ShowPortReceData(_recvBuffer)

            '    sp.Read(_recvBuffer, _recvOffset, 4096 - _recvOffset)

            '    _recvOffset = 0
            'Else
            '    sp.Read(_recvBuffer, _recvOffset, bytes)
            '    _recvOffset += bytes
            'End If

            Dim buf(bytes - 1) As Byte
            sp.Read(buf, 0, bytes)
            recvBufferli.AddRange(buf)
            Thread.Sleep(5)
        Loop While sp.BytesToRead > 0
        If recvBufferli.Count > 0 Then

            If G_ReadFlag Then
                If G_ShowHex Then
                    dstr = publicMode.ByteToHex(recvBufferli.ToArray)
                Else
                    dstr = publicMode.ByteToString(recvBufferli.ToArray)
                End If
                '发送要不要添加到表格、时间差怎么算
                SyncLock G_msgqueue
                    G_msgqueue.Enqueue((G_aliasname, G_TXTcolor, $"{Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}_{dstr}"))
                End SyncLock
            End If

            recvBufferli.Clear()
            'RuningLog.OutputLogsToTheControl(m_Control, New RuningLogConfig($"RX:{DataProcessing.ByteToString2(recvBufferli.ToArray)}", Color.Black), 1)

        End If
    End Sub

    '设置接受标志
    Public Sub SetReadFlag(isRead As Boolean)
        G_ReadFlag = isRead
    End Sub

    Public Sub SendData(datastr As String)

        '判断串口是否打开
        If Not G_SerialPort.IsOpen Then
            MsgBox("串口未打开")
            Return
        End If

        If String.IsNullOrEmpty(datastr) Then
            MsgBox("发送的数据为空")
            Return
        End If


        Dim Data As Byte()
        If G_SendHex Then
            Data = publicMode.HexStringToByte(datastr)
            If IsNothing(Data) Then
                MsgBox("发送的数据异常")
                Return
            End If
            datastr = publicMode.ByteToHexString(Data)
            'SyncLock G_msgqueue
            '    G_msgqueue.Enqueue((G_aliasname, G_TXTcolor, $"{Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}_TX:{datastr}"))
            'End SyncLock
            'Data = publicMode.HexToByte(datastr)
        Else
            'SyncLock G_msgqueue
            '    G_msgqueue.Enqueue((G_aliasname, G_TXTcolor, $"{Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}_TX:{datastr}"))
            'End SyncLock
            Data = publicMode.StringToByte(datastr)
        End If
        If IsNothing(Data) Then
            MsgBox("发送的数据异常")
            Return
        End If


        G_SerialPort.Write(Data, 0, Data.Length)

    End Sub


End Class
