Imports System.IO.Ports Imports System.IO Public Class Form1 Public Port_Com As String Public Port_Baudrate As String Public Port_Parity As String Public ADDR As New List(Of String) Dim WithEvents MBus As New Modbus_S.Modbus Dim SPort As New SerialPort() Public Delegate Sub MsgReceive(ByVal b() As Byte) Private Sub btConeck_Click() Handles btConeck.Click 'Open COM port using provided settings: Dim iParity As Integer Dim StopBit As String Port_Com = txtCom.Text Port_Baudrate = Convert.ToInt32(txtBaudRate.Text) iParity = Parity.None StopBit = StopBits.One MBus.Connect(Port_Com, Port_Baudrate, 8, iParity, StopBit) Label1.Text = MBus.modbusStatus End Sub Private Sub btBaca_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btBaca.Click Timer2.Enabled = True Dim i = 1 Dim iRegStart Dim lAddr As Long = Val(dgTags.Rows(i).Cells(0).Value) Dim iSlaveID As Integer = Val(ID.Text) Dim iRegLength = txtADDRL.Text iRegStart = lAddr - 40001 Do Until MBus.SendFc3(iSlaveID, iRegStart, iRegLength) Application.DoEvents() Loop End Sub Public Sub MsgReceived(ByVal b() As Byte) For i = 3 To (b(2) + 2) Dim _bytes(3) As Byte _bytes(0) = b(i + 3) _bytes(1) = b(i + 2) _bytes(2) = b(i + 1) _bytes(3) = b(i) Dim _val = BitConverter.ToSingle(_bytes, 0) ' konversi float 2 word dgTags.Rows((i / 4) - 1).Cells(2).Value = _val ' tulis data ke data grid i += 3 Next Timer2.Enabled = False statPolling.Text = "Polling: OK" Button1_Click() End Sub Private Sub MBus_MessageRecived(ByVal b() As Byte) Handles MBus.MessageRecived Invoke(New MsgReceive(AddressOf MsgReceived), b) End Sub Private Sub btDisconeck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btDisconeck.Click MBus.Close() Label1.Text = MBus.modbusStatus End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Timer2.Enabled = True Dim iRegStart Dim lAddr As Long = Val(txtADDR.Text) Dim iSlaveID As Integer = Val(ID.Text) Dim iRegLength = txtADDRL.Text iRegStart = lAddr - 40001 Do Until MBus.SendFc3(iSlaveID, iRegStart, iRegLength) Application.DoEvents() Loop Button1_Click() End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load dgTags.Rows.Clear() btConeck_Click() Dim x As Integer x = (txtADDRL.Text / 2) - 1 For i = 0 To x dgTags.Rows.Add() dgTags.Rows(i).Cells(1).Value = txtADDR.Text + (i * 2) ' dgTags.Rows(i).Cells(1).Value = Tags_SlaveID(i) ' dgTags.Rows(i).Cells(2).Value = Tags_Address(i) 'dgTags.Rows(i).Cells(3).Value = Tags_RegType(i) 'dgTags.Rows(i).Cells(4).Value = Tags_DataType(i) 'dgTags.Rows(i).Cells(5).Value = Tags_Multiplier(i) 'dgTags.Rows(i).Cells(6).Value = Tags_RoundDigit(i) Next End Sub Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick statPolling.Text = "Time Out...." End Sub Private Sub Button1_Click() Handles Button1.Click Dim objStreamWriter As StreamWriter 'Pass the file path and the file name to the StreamWriter constructor. objStreamWriter = New StreamWriter("C:\wamp/www/SIDP_UPDK/RTU/M2.txt") 'Write a line of text. objStreamWriter.WriteLine(dgTags.Rows(0).Cells(1).Value & "," & dgTags.Rows(0).Cells(2).Value) 'Write a second line of text. ' objStreamWriter.WriteLine("From the StreamWriter class") 'Close the file. objStreamWriter.Close() End Sub End Class