Imports System.Globalization
Imports System.IO.Ports
Imports System.Runtime.Remoting.Messaging

Module SerialPortMoudule
    Private Const FormName As String = "DUALSONIC Product Manager Serial Port Ver " & MainForm.FormVersion
    Public UseSerial_Ch1 As SerialPort

    Private rxMessage As String = String.Empty
    Private cmdMessage As String = String.Empty

    Public Sub UseSerialSetting(UseSerial As SerialPort)
        UseSerial_Ch1 = UseSerial
    End Sub

    Public Function OpenSerial(SelectPort As String) As Boolean
        Try
            If UseSerial_Ch1.IsOpen = False Then
                With UseSerial_Ch1
                    .PortName = SelectPort
                    .BaudRate = 230400
                    .DataBits = 8
                    .Parity = Parity.None
                    .StopBits = StopBits.One
                    .Handshake = Handshake.None

                    .Open()
                End With

                Return True
            Else
                MsgBox("해당 포트는 Open 상태입니다. 연결을 끊고 다시 연결하여 주십시오.", vbExclamation, FormName)
                Return False
            End If

        Catch ex As Exception
            MsgBox("COM 포트 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            Return False
        End Try
    End Function

    Public Function SendData(SendMessage As String) As Boolean
        Try
            If UseSerial_Ch1.IsOpen = True Then

                UseSerial_Ch1.DiscardInBuffer()
                UseSerial_Ch1.DiscardOutBuffer()
                UseSerial_Ch1.WriteTimeout = -1
                UseSerial_Ch1.WriteLine(SendMessage)
                Return False

            Else

                MsgBox("통신 포트가 열리지 않았습니다.", vbCritical, FormName)
                Return True

            End If

        Catch ex As Exception
            MsgBox("COM 포트 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            Return True
        End Try
    End Function

    Public Function PollingReceiveData(RdTimeout As Long) As String
        Try
            If UseSerial_Ch1.IsOpen = True Then
                rxMessage = ""
                UseSerial_Ch1.ReadTimeout = RdTimeout
                rxMessage = UseSerial_Ch1.ReadLine

                Return ParsingData()

            Else
                MsgBox("통신 포트가 열리지 않았습니다.", vbCritical, FormName)
                Return False
            End If
        Catch ex As System.TimeoutException
            If MainForm.lowPrdCmdFlag Then
                Return "timeout"
            End If
            MsgBox("COM 포트 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            Return False
        Catch ex As Exception
            MsgBox("COM 포트 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            Return False
        Finally
            MainForm.lowPrdCmdFlag = False
        End Try

    End Function

    Private Function ParsingData() As String
        Dim cmdStr As String
        Dim cmdVal(0 To 7) As String
        Dim i As Integer

        Try
            cmdStr = "0"

            If rxMessage <> "" Then
                cmdMessage = ""

                If Mid(rxMessage, 1, 1) = "[" Then
                    cmdMessage = Mid(rxMessage, 2, Len(rxMessage))

                    cmdStr = Mid(cmdMessage, 1, (InStr(rxMessage, " ") - 2))
                    cmdMessage = Mid(rxMessage, InStr(rxMessage, " ") + 1, Len(cmdMessage))

                    For i = 1 To 7
                        If InStr(cmdMessage, ",") <> 0 Then
                            cmdVal(i) = Mid(cmdMessage, 1, InStr(cmdMessage, ",") - 1)
                            cmdMessage = Mid(cmdMessage, InStr(cmdMessage, ",") + 1, Len(cmdMessage))
                        ElseIf InStr(cmdMessage, "]") <> 0 Then
                            cmdVal(i) = Mid(cmdMessage, 1, InStr(cmdMessage, "]") - 1)
                            'cmdMessage = Mid(cmdMessage, InStr(cmdMessage, "]") + 1, Len(cmdMessage))
                        Else
                            Exit For
                        End If
                    Next i

                    rxMessage = ""

                    Select Case cmdStr

                        '' DUALSONIC
                        Case "cartauth"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "cartmanu"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "ctmodel"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "carttype"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "cartsn"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "cartmanu"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "ctabroad"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "ctagency"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "cartfreq"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "ctpower"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "frontsen"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "rshotcnt"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "tshotcnt"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "mainauth"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "rdstconf"
                            cmdStr = cmdVal(1) & "," & cmdVal(2) & "," & cmdVal(3) & "," & cmdVal(4) & "," & cmdVal(5)
                            Return cmdStr
                        Case "wrstconf"
                            cmdStr = cmdVal(1)
                            Return cmdStr


                        '' MAXIMUM
                        Case "rdsconf1"
                            If MainForm.SelectedModelInfo.Code.Length = 2 Then     '16자리 시리얼번호
                                cmdStr = cmdVal(1) & "," & cmdVal(2) & "," & cmdVal(3) & "," & cmdVal(4) & "," & cmdVal(5) & "," & cmdVal(6) & "," & cmdVal(7)
                            Else                                            '12자리 시리얼번호
                                cmdStr = cmdVal(1) & "," & cmdVal(2) & "," & cmdVal(3) & "," & cmdVal(4) & "," & cmdVal(5)
                            End If
                            Return cmdStr
                        Case "wrsconf1"
                            If MainForm.SelectedModelInfo.Code.Length = 2 Then     '16자리 시리얼번호
                                cmdStr = cmdVal(1) & "," & cmdVal(2) & "," & cmdVal(3) & "," & cmdVal(4) & "," & cmdVal(5) & "," & cmdVal(6) & "," & cmdVal(7)
                            Else                                            '12자리 시리얼번호
                                cmdStr = cmdVal(1) & "," & cmdVal(2) & "," & cmdVal(3) & "," & cmdVal(4) & "," & cmdVal(5)
                            End If
                            Return cmdStr
                        Case "shotconf"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "rdstinfo"
                            cmdStr = cmdVal(1) & "," & cmdVal(2) & "," & cmdVal(3) & "," & cmdVal(4) & "," & cmdVal(5)
                            Return cmdStr
                        Case "wrstinfo"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "rdsinfo4"
                            cmdStr = cmdVal(1) & "," & cmdVal(2) & "," & cmdVal(3) & "," & cmdVal(4) & "," & cmdVal(5) & "," & cmdVal(6) & "," & cmdVal(7)
                            Return cmdStr
                        Case "rdsinfo5"
                            cmdStr = cmdVal(1) & "," & cmdVal(2) & "," & cmdVal(3) & "," & cmdVal(4)
                            Return cmdStr
                        Case "wrsinfo4"
                            cmdStr = cmdVal(1) & "," & cmdVal(2) & "," & cmdVal(3) & "," & cmdVal(4) & "," & cmdVal(5)
                            Return cmdStr

                        Case "deviceon"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "entermft"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "mftmode"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "runmc"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "runrf"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "runep"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "endmft"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "runhifu"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "dsfwver"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "rdbattlv"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "rdtemp"
                            cmdStr = cmdVal(1) & "," & cmdVal(2)
                            Return cmdStr
                        Case "resetinf"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "settdsn"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "settdcnf"
                            cmdStr = cmdVal(1) & "," & cmdVal(2)
                            Return cmdStr
                        Case "runled"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "setmotor"
                            cmdStr = cmdVal(1) & "," & cmdVal(2) & "," & cmdVal(3) & "," & cmdVal(4) & "," & cmdVal(5)
                            Return cmdStr
                        Case "runbuzz"
                            cmdStr = cmdVal(1)
                            Return cmdStr

                            ''ALTO
                        Case "rdsinfo7"
                            cmdStr = cmdVal(1) & "," & cmdVal(2) & "," & cmdVal(3) & "," & cmdVal(4) & "," & cmdVal(5)
                            Return cmdStr

                        Case "rdsinfo8"
                            cmdStr = cmdVal(1) & "," & cmdVal(2) & "," & cmdVal(3) & "," & cmdVal(4) & "," & cmdVal(5)
                            Return cmdStr

                                 ''ldm
                        Case "setldmsn"
                            cmdStr = cmdVal(1)
                            Return cmdStr
                        Case "setldmcnf"
                            cmdStr = cmdVal(1) & "," & cmdVal(2) & "," & cmdVal(3) & "," & cmdVal(4) & "," & cmdVal(5) & "," & cmdVal(6)
                            Return cmdStr
                        Case "rdsinfo1"
                            cmdStr = cmdVal(1) & "," & cmdVal(2)
                            Return cmdStr
                    End Select

                End If

            End If

            Return False

        Catch ex As Exception
            rxMessage = ""
            MsgBox("Receive 에러 발생!" & vbCrLf & ex.Message, vbCritical, FormName)
            Return False
        End Try
    End Function

End Module
