
Imports M2M_RTU_NET

Public Class Form1

    Dim stationN As Integer = 0

    '== split module name list
    Private Function GetModbus_Module_Name(ByVal bSrc As Byte(), ByVal n As Integer) As String
        Dim name As String
        name = System.Text.Encoding.Default.GetString(bSrc, n * 24, 24)
        name = name.Trim(Chr(0))
        Return name
    End Function


    Private Function getAPIVersion() As String
        Dim ver As Integer, verMain As Integer, verSub As Integer
        ver = M2M_RTU.GetDllVersion()
        verMain = (ver And &HFF00) >> 8
        verSub = ver And &HFF
        Dim ver_string As String = verMain.ToString("X") + "." + verSub.ToString("X02")

        Dim bVerDate(32) As Byte
        M2M_RTU.GetDllDate(bVerDate)
        ver_string = "V" + ver_string + " " + System.Text.Encoding.Default.GetString(bVerDate).Trim(Chr(0))
        Return ver_string

    End Function

    Private Sub btn_start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_start.Click

        Dim res As Integer
        '-- get version
        txtVersion.Text = getAPIVersion()

        '-- step 1, get station number
        res = M2M_RTU.GET_Total_Station_NUM(stationN)
        If (res = 0) Then
            txtStationN.Text = stationN.ToString()
            txtStatus.Text = "Initial Success"
        Else
            txtStatus.Text = "Initial Fails" + ", Error: " + res.ToString()
        End If

        '-- step 2, get station information (can be skip)
        Dim staInfo As M2M_RTU.OPC_Total_Info = New M2M_RTU.OPC_Total_Info()
        Dim i As Integer, j As Integer
        txtInformation.AppendText("========" + vbNewLine)
        For i = 0 To stationN - 1
            res = M2M_RTU.GET_StationID_Total_Info(i, staInfo)

            If (res = 0) Then
                txtInformation.AppendText("----" + vbNewLine)
                txtInformation.AppendText("Station ID = " + staInfo.uiStationID.ToString() + vbNewLine)
                'module type: 0=G-4500, 1=GT-540
                txtInformation.AppendText("  device type = " + staInfo.uiModule.ToString() + vbNewLine)
                Dim moduleName As String = System.Text.Encoding.Unicode.GetString(staInfo.bModule_Name).Trim(Chr(0))
                txtInformation.AppendText("  Module Name = " + moduleName + vbNewLine)
                txtInformation.AppendText("  Modbus Number = " + staInfo.uiModbus_Num.ToString() + vbNewLine)
                For j = 0 To staInfo.uiModbus_Num - 1
                    txtInformation.AppendText(vbTab + "-- Modbus(" + j.ToString() + ") --" + vbNewLine)
                    Dim Modbus_name As String = GetModbus_Module_Name(staInfo.bModbus_Module_Name, j)
                    txtInformation.AppendText(vbTab + "Name = " + Modbus_name + vbNewLine)
                    txtInformation.AppendText(vbTab + "sID = " + staInfo.bSlave_ID(j).ToString() + vbNewLine)
                    txtInformation.AppendText(vbTab + "DI x" + staInfo.bDI_Num(j).ToString() + vbNewLine)
                    txtInformation.AppendText(vbTab + "DO x" + staInfo.bDO_Num(j).ToString() + vbNewLine)
                    txtInformation.AppendText(vbTab + "AI x" + staInfo.bAI_Num(j).ToString() + vbNewLine)
                    txtInformation.AppendText(vbTab + "AO x" + staInfo.bAO_Num(j).ToString() + vbNewLine)
                    txtInformation.AppendText(vbTab + "Count x" + staInfo.bCounter_Num(j).ToString() + vbNewLine)
                    txtInformation.AppendText(vbTab + "enGPS = " + staInfo.usEnGPS(j).ToString() + vbNewLine)
                    txtInformation.AppendText(vbNewLine)
                Next
            Else
                txtInformation.AppendText("----" + vbNewLine)
                txtInformation.AppendText("!!!! cannot get Staintion Information !!!!, Error: " + res.ToString() + vbNewLine)
                '- 沒連線，仍要讀資料
                txtInformation.AppendText("Station ID = " + staInfo.uiStationID.ToString() + vbNewLine)
                'module type: 0=G-4500, 1=GT-540
                txtInformation.AppendText("  device type = " + staInfo.uiModule.ToString() + vbNewLine)
                Dim moduleName As String = System.Text.Encoding.Unicode.GetString(staInfo.bModule_Name).Trim(Chr(0))
                txtInformation.AppendText("  Module Name = " + moduleName + vbNewLine)
                txtInformation.AppendText("  Modbus Number = " + staInfo.uiModbus_Num.ToString() + vbNewLine)
                For j = 0 To staInfo.uiModbus_Num - 1
                    txtInformation.AppendText(vbTab + "-- Modbus(" + j.ToString() + ") --" + vbNewLine)
                    Dim Modbus_name As String = GetModbus_Module_Name(staInfo.bModbus_Module_Name, j)
                    txtInformation.AppendText(vbTab + "Name = " + Modbus_name + vbNewLine)
                    txtInformation.AppendText(vbTab + "sID = " + staInfo.bSlave_ID(j).ToString() + vbNewLine)
                    txtInformation.AppendText(vbTab + "DI x" + staInfo.bDI_Num(j).ToString() + vbNewLine)
                    txtInformation.AppendText(vbTab + "DO x" + staInfo.bDO_Num(j).ToString() + vbNewLine)
                    txtInformation.AppendText(vbTab + "AI x" + staInfo.bAI_Num(j).ToString() + vbNewLine)
                    txtInformation.AppendText(vbTab + "AO x" + staInfo.bAO_Num(j).ToString() + vbNewLine)
                    txtInformation.AppendText(vbTab + "Count x" + staInfo.bCounter_Num(j).ToString() + vbNewLine)
                    txtInformation.AppendText(vbTab + "enGPS = " + staInfo.usEnGPS(j).ToString() + vbNewLine)
                    txtInformation.AppendText(vbNewLine)
                Next
            End If
            Application.DoEvents()
        Next

    End Sub

    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        '-- get local data
        Dim SID As UInt32 = Convert.ToUInt32(txtStationID.Text)
        Dim mbSID As Byte = 255
        Dim value As ULong = 0
        Dim strValue As String
        'Please don't modify this string, or you will get wrong data
        Dim mbName As String = "Local IO"
        Dim bMbName As Byte() = System.Text.Encoding.Default.GetBytes(mbName)
        Dim result As Integer
        Dim YY As Integer, Mon As Integer, DD As Integer, hh As Integer, mm As Integer, ss As Integer

        txtIOData.AppendText("----------" + vbNewLine)
        M2M_RTU.GET_DateTime(SID, YY, Mon, DD, hh, mm, ss)
        txtIOData.AppendText("---" + YY.ToString() + "/" + Mon.ToString() + "/" + DD.ToString() + " " _
                                    + hh.ToString() + ":" + mm.ToString() + ":" + ss.ToString() + vbNewLine)
        txtIOData.AppendText("Station ID:" + SID.ToString() + ", mbSlave ID:" + mbSID.ToString() + ", Modbus Name=" + mbName + vbNewLine)

        ' although, you will get a Error code, but function still return the last value
        '- DI, single channel
        result = M2M_RTU.GET_Modbus_DI_Channel(SID, bMbName, mbSID, 0, value)
        If (result = 0) Then
            txtIOData.AppendText("DI ch0=" + value.ToString() + vbNewLine)
        Else
            txtIOData.AppendText("DI ch0=" + value.ToString() + ", Error=" + result.ToString() + vbNewLine)
        End If
        '- DIs
        result = M2M_RTU.GET_Modbus_DI(SID, bMbName, mbSID, value)
        strValue = Convert.ToString(Convert.ToInt32(value), 2)
        If (result = 0) Then
            txtIOData.AppendText("DIs=" + strValue + vbNewLine)
        Else
            txtIOData.AppendText("DIs=" + strValue + " ,Error = " + result.ToString() + vbNewLine)
        End If


        '- DO, single channel
        result = M2M_RTU.GET_Modbus_DO_Channel(SID, bMbName, mbSID, 0, value)
        If (result = 0) Then
            txtIOData.AppendText("DO ch0=" + value.ToString() + vbNewLine)
        Else
            txtIOData.AppendText("DO ch0=" + value.ToString() + ", Error=" + result.ToString() + vbNewLine)
        End If
        '- DOs
        result = M2M_RTU.GET_Modbus_DO(SID, bMbName, mbSID, value)
        strValue = Convert.ToString(Convert.ToInt32(value), 2)
        If (result = 0) Then
            txtIOData.AppendText("DOs=" + strValue + vbNewLine)
        Else
            txtIOData.AppendText("DOs=" + strValue + " ,Error = " + result.ToString() + vbNewLine)
        End If

        '- counter, single channel
        result = M2M_RTU.GET_Modbus_Counter_Channel(SID, bMbName, mbSID, 0, value)
        If (result = 0) Then
            txtIOData.AppendText("Counter ch0=" + value.ToString() + vbNewLine)
        Else
            txtIOData.AppendText("Counter ch0=" + value.ToString() + ", Error=" + result.ToString() + vbNewLine)
        End If


        '- don't care these two variable, just pass two variable to the function
        Dim bDataFormat As Byte = 0
        Dim bType As Byte = 0
        '- AI
        result = M2M_RTU.GET_Modbus_AI_Channel(SID, bMbName, mbSID, 0, bDataFormat, bType, value)
        If (result = 0) Then
            txtIOData.AppendText("AI ch0 Hex=" + value.ToString() + vbNewLine)
            Dim AI_value As Double = value / 4095.0 * 20.0 'for GT-540, AI is 12 bits, 0~20mA
            txtIOData.AppendText("  Hex --> mA :" + AI_value.ToString("F3") + " mA" + vbNewLine)
        Else
            txtIOData.AppendText("AI ch0 Hex=" + value.ToString() + ", Error=" + result.ToString() + vbNewLine)
        End If

        '- AO
        result = M2M_RTU.GET_Modbus_AO_Channel(SID, bMbName, mbSID, 0, bDataFormat, bType, value)
        If (result = 0) Then
            txtIOData.AppendText("AO ch0=" + value.ToString() + vbNewLine)
        Else
            txtIOData.AppendText("AO ch0=" + value.ToString() + ", Error=" + result.ToString() + vbNewLine)
        End If


        Dim bGPS(65535) As Byte
        result = M2M_RTU.GET_Modbus_GPS(SID, bMbName, mbSID, bGPS)
        If (result = 0) Then
            Dim GPS_String As String = System.Text.Encoding.Default.GetString(bGPS).Trim(Chr(0))
            txtIOData.AppendText("GPS :" + vbNewLine + GPS_String + vbNewLine)
        Else
            txtIOData.AppendText("GPS :Error = " + result.ToString() + vbNewLine)
        End If

        txtIOData.AppendText("----------" + vbNewLine)

    End Sub

    Private Sub btnWriteDo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteDo.Click
        Dim SID As UInt32 = Convert.ToUInt32(txtStationID.Text)
        Dim mbSID As Byte = 255
        Dim value As ULong = 0
        Dim bMbName As Byte() = System.Text.Encoding.Default.GetBytes("Local IO")
        Dim result As Integer

        M2M_RTU.GET_Modbus_DO_Channel(SID, bMbName, mbSID, 0, value)
        If value = 0 Then
            value = 1
        Else
            value = 0
        End If
        Dim i As Integer
        For i = 0 To 1000
            result = M2M_RTU.WRITE_Modbus_DO_Channel(SID, bMbName, mbSID, 0, value)
        Next


        If result = 0 Then
            'MsgBox("success to write DO as " + value.ToString() + vbNewLine + "the DO status will change later, because GPRS is slow...")
        Else
            MsgBox("fails to write DO")
        End If

    End Sub


    Private Sub btnWriteAO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteAO.Click
        Dim SID As UInt32 = Convert.ToUInt32(txtStationID.Text)
        Dim mbSID As Byte = 255
        Dim value As ULong = 0
        Dim bMbName As Byte() = System.Text.Encoding.Default.GetBytes("Local IO")
        Dim result As Integer

        '- read AO
        M2M_RTU.GET_Modbus_AO_Channel(SID, bMbName, mbSID, 0, 0, 0, value)

        If value = 0 Then
            value = 1
        Else
            value = 0
        End If

        For i As Integer = 0 To 1000
            result = M2M_RTU.WRITE_Modbus_AO_Channel(SID, bMbName, mbSID, 0, value)
        Next

        If result = 0 Then
            'MsgBox("success to write AO_0 as " + value.ToString() + vbNewLine + "the AO status will change later, because GPRS is slow...")
        Else
            MsgBox("fails to write AO_0")
        End If
    End Sub

    Private Sub btnReadRemote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadRemote.Click

        '-- get data
        Dim SID As UInt32 = Convert.ToUInt32(txtStationID.Text)
        Dim mbSID As Byte = Convert.ToUInt32(txtMbID.Text) '--> depend on your modbus slave ID
        Dim mbName As String = txtMbName.Text   '-->depend on your module name (on RTU Center)
        Dim bMbName As Byte() = System.Text.Encoding.Default.GetBytes(mbName)
        Dim value As ULong = 0
        Dim strValue As String
        Dim result As Integer
        Dim YY As Integer, Mon As Integer, DD As Integer, hh As Integer, mm As Integer, ss As Integer

        txtRemoteIOData.AppendText("----------" + vbNewLine)
        M2M_RTU.GET_DateTime(SID, YY, Mon, DD, hh, mm, ss)
        txtRemoteIOData.AppendText("---" + YY.ToString() + "/" + mm.ToString() + "/" + DD.ToString() + " " _
                                    + hh.ToString() + ":" + mm.ToString() + ":" + ss.ToString() + vbNewLine)
        txtRemoteIOData.AppendText("Station ID:" + SID.ToString() + ", mbSlave ID:" + mbSID.ToString() + ", Modbus Name=" + mbName + vbNewLine)

        ' although, you will get a Error code, but function still return the last value
        '- DI, single channel
        result = M2M_RTU.GET_Modbus_DI_Channel(SID, bMbName, mbSID, 0, value)
        If (result = 0) Then
            txtRemoteIOData.AppendText("DI ch0=" + value.ToString() + vbNewLine)
        Else
            txtRemoteIOData.AppendText("DI ch0=" + value.ToString() + ", Error=" + result.ToString() + vbNewLine)
        End If
        '- DIs
        result = M2M_RTU.GET_Modbus_DI(SID, bMbName, mbSID, value)
        strValue = Convert.ToString(Convert.ToInt32(value), 2)
        If (result = 0) Then
            txtRemoteIOData.AppendText("DIs=" + strValue + vbNewLine)
        Else
            txtRemoteIOData.AppendText("DIs=" + strValue + " ,Error = " + result.ToString() + vbNewLine)
        End If


        '- DO, single channel
        result = M2M_RTU.GET_Modbus_DO_Channel(SID, bMbName, mbSID, 0, value)
        If (result = 0) Then
            txtRemoteIOData.AppendText("DO ch0=" + value.ToString() + vbNewLine)
        Else
            txtRemoteIOData.AppendText("DO ch0=" + value.ToString() + ", Error=" + result.ToString() + vbNewLine)
        End If
        '- DOs
        result = M2M_RTU.GET_Modbus_DO(SID, bMbName, mbSID, value)
        strValue = Convert.ToString(Convert.ToInt32(value), 2)
        If (result = 0) Then
            txtRemoteIOData.AppendText("DOs=" + strValue + vbNewLine)
        Else
            txtRemoteIOData.AppendText("DOs=" + strValue + " ,Error = " + result.ToString() + vbNewLine)
        End If

        '- counter, single channel
        result = M2M_RTU.GET_Modbus_Counter_Channel(SID, bMbName, mbSID, 0, value)
        If (result = 0) Then
            txtRemoteIOData.AppendText("Counter ch0=" + value.ToString() + vbNewLine)
        Else
            txtRemoteIOData.AppendText("Counter ch0=" + value.ToString() + ", Error=" + result.ToString() + vbNewLine)
        End If


        '- don't care these two variable, just pass two variable to the function
        Dim bDataFormat As Byte = 0
        Dim bType As Byte = 0
        '- AI
        result = M2M_RTU.GET_Modbus_AI_Channel(SID, bMbName, mbSID, 0, bDataFormat, bType, value)
        If (result = 0) Then
            txtRemoteIOData.AppendText("AI ch0 Hex=" + value.ToString() + vbNewLine)
            Dim AI_value As Double = value / 4095.0 * 20.0 'for GT-540, AI is 12 bits, 0~20mA
            txtRemoteIOData.AppendText("  Hex --> mA :" + AI_value.ToString("F3") + " mA" + vbNewLine)
        Else
            txtRemoteIOData.AppendText("AI ch0 Hex=" + value.ToString() + ", Error=" + result.ToString() + vbNewLine)
        End If

        '- AO
        result = M2M_RTU.GET_Modbus_AO_Channel(SID, bMbName, mbSID, 0, bDataFormat, bType, value)
        If (result = 0) Then
            txtRemoteIOData.AppendText("AO ch0=" + value.ToString() + vbNewLine)
        Else
            txtRemoteIOData.AppendText("AO ch0=" + value.ToString() + ", Error=" + result.ToString() + vbNewLine)
        End If

        txtRemoteIOData.AppendText("----------" + vbNewLine)

    End Sub

    Private Sub btnWriteDoRemote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteDoRemote.Click
        Dim SID As UInt32 = Convert.ToUInt32(txtStationID.Text)
        Dim mbSID As Byte = Convert.ToUInt32(txtMbID.Text) '--> depend on your modbus slave ID
        Dim mbName As String = txtMbName.Text   '-->depend on your module name (on RTU Center)
        Dim bMbName As Byte() = System.Text.Encoding.Default.GetBytes(mbName)
        Dim value As ULong = 0
        Dim result As Integer

        '-- Write Do
        M2M_RTU.GET_Modbus_DO_Channel(SID, bMbName, mbSID, 0, value)
        If value = 0 Then
            value = 1
        Else
            value = 0
        End If
        result = M2M_RTU.WRITE_Modbus_DO_Channel(SID, bMbName, mbSID, 0, value)


        If result = 0 Then
            MsgBox("success to write DO as " + value.ToString() + vbNewLine + "the DO status will change later, because GPRS is slow...")
        Else
            MsgBox("fails to write DO")
        End If

    End Sub


    Private Sub btnWriteDosRemote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteDosRemote.Click
        Dim SID As UInt32 = Convert.ToUInt32(txtStationID.Text)
        Dim mbSID As Byte = Convert.ToUInt32(txtMbID.Text) '--> depend on your modbus slave ID
        Dim mbName As String = txtMbName.Text   '-->depend on your module name (on RTU Center)
        Dim bMbName As Byte() = System.Text.Encoding.Default.GetBytes(mbName)
        Dim value As ULong = 0
        Dim result As Integer

        '-- Write DOs
        M2M_RTU.GET_Modbus_DO(SID, bMbName, mbSID, value)
        value = value + 1

        result = M2M_RTU.WRITE_Modbus_DO(SID, bMbName, mbSID, value)

        If result = 0 Then
            MsgBox("success to write DO as " + value.ToString() + vbNewLine + "the DO status will change later, because GPRS is slow...")
        Else
            MsgBox("fails to write DO")
        End If

    End Sub


    Private Sub btnWriteAoRemote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteAoRemote.Click
        Dim SID As UInt32 = Convert.ToUInt32(txtStationID.Text)
        Dim mbSID As Byte = Convert.ToUInt32(txtMbID.Text) '--> depend on your modbus slave ID
        Dim mbName As String = txtMbName.Text   '-->depend on your module name (on RTU Center)
        Dim bMbName As Byte() = System.Text.Encoding.Default.GetBytes(mbName)
        Dim value As ULong = 0
        Dim result As Integer

        '- read AO
        M2M_RTU.GET_Modbus_AO_Channel(SID, bMbName, mbSID, 0, 0, 0, value)
        value = value + 1

        result = M2M_RTU.WRITE_Modbus_AO_Channel(SID, bMbName, mbSID, 0, value)

        If result = 0 Then
            MsgBox("success to write AO_0 as " + value.ToString() + vbNewLine + "the AO status will change later, because GPRS is slow...")
        Else
            MsgBox("fails to write AO_0")
        End If

    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        txtInformation.Text = ""
    End Sub

    Private Sub btnWriteCounterRemote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteCounterRemote.Click
        Dim SID As UInt32 = Convert.ToUInt32(txtStationID.Text)
        Dim mbSID As Byte = Convert.ToUInt32(txtMbID.Text) '--> depend on your modbus slave ID
        Dim mbName As String = txtMbName.Text   '-->depend on your module name (on RTU Center)
        Dim bMbName As Byte() = System.Text.Encoding.Default.GetBytes(mbName)
        Dim result As Integer

        result = M2M_RTU.WRITE_Modbus_CountZero_Channel(SID, bMbName, mbSID, 0)

        If result = 0 Then
            MsgBox("success to write Counter_0 as 0\n change later, because GPRS is slow...")
        Else
            MsgBox("fails to write Counter_0")
        End If

    End Sub

    Private Sub btnWDT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWDT.Click
        Dim wdt As Integer
        wdt = M2M_RTU.Get_RTUWDT()
        txtInformation.AppendText("----" + vbNewLine)
        txtInformation.AppendText("Soft WDT Value = " + wdt.ToString() + vbNewLine)
        txtInformation.AppendText("----" + vbNewLine)

    End Sub

    Private Sub btnRunRTU_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRunRTU.Click
        M2M_RTU.RTU_WakeUpAP(0)
    End Sub

    Private Sub btnCloseRTU_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCloseRTU.Click
        M2M_RTU.RTU_CloseAP()
    End Sub
End Class
