HMI modbus

P

Thread Starter

petrusha

plz help me with vb code for write and read function of modbus via serial port. also tell me how to send a packet via serial port using modbus

<pre>Imports System.Array
Imports System.IO.Ports
Imports System

Public Class GUI_amey1
Dim crc As Byte
Dim c As Byte
'Dim strHMI As String
'public bool SendFc16(byte address, ushort start,
''ushort registers, short[] values)
'{
'Ensure port is open:
'if (sp.IsOpen)
'{
'Clear in/out buffers:
' sp.DiscardOutBuffer();
' sp.DiscardInBuffer();

'Message is 1 addr + 1 fcn + 2 start + 2 reg + 1 count +
' 2 * reg vals + 2 CRC
'Dim byte[] message = new byte[9 + 2 * registers];

'Function 16 response is fixed at 8 bytes
'byte[] response = new byte[8];

'Add bytecount to message:
'message[6] = (byte)(registers * 2);

'Put write values into message prior to sending:
'for (int i = 0; i < registers; i++)
'{
' message[7 + 2 * i] = (byte)(values >> 8);
' message[8 + 2 * i] = (byte)(values);
'}

'Build outgoing message:
' BuildMessage(address, (byte)16, start, registers, ref message);
'
' 'Send Modbus message to Serial Port:
' try
'{
' rs232.Write(message, 0, message.Length);
'GetResponse(ref response);
'}
'catch (Exception err)
'{
' modbusStatus = "Error in write event: " + err.Message;
' return false;
'}

'//Evaluate message:
'if (CheckResponse(response))
'{
' modbusStatus = "Write successful";
' return true;
'}
'else
'{
' modbusStatus = "CRC error";
'' return false;
' }
'}
'else
'{
'modbusStatus = "Serial port not open";
'return false;
'}
'}
'}
'Private Sub CommPortSetup()
' With SerialPort1
'.PortName = "COM8"
'.BaudRate = 34800
'.DataBits = 8
'.Parity = Parity.None
'.StopBits = StopBits.One
'.Handshake = Handshake.None
'End With
'Try
'SerialPort1.Open()
'Catch ex As Exception
'MessageBox.Show(ex.Message)
'End Try
'Dim myPortNames() As String
'myPortNames = SerialPort.GetPortNames
'Dim buffer As Byte()
'Dim offset As Integer
'Dim count As Integer
' Dim returnValue As Integer
'returnValue = SerialPort1.Read(buffer, offset, count)
'Dim instance As SerialPort
'Dim text As String
'instance.Write(text)
'End Sub
'Private Sub SendSlave(ByVal sendCmd As Byte, ByVal sendData As Byte)
'send command to slave PICmicro in form of cmd, data
'Dim buffer(2) As Byte
'buffer(0) = sendCmd
'buffer(1) = sendData
'SerialPort1.Write(buffer, 0, 2)
'End Sub
'Private Sub mySerialPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
'Handles serial port data received events
'Dim n As Integer = SerialPort1.BytesToRead
'Dim comBuffer As Byte() = New Byte(n - 1) {}
'SerialPort1.Read(comBuffer, 0, n)
'Console.WriteLine(comBuffer(0))

'End Sub

'Dim myPort As Array 'COM Ports detected on the system will be stored here
'Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data


Private Sub Gui_1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box.
'myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available
ComboBox2.Items.Add(9600) 'Populate the cmbBaud Combo box to common baud rates used
ComboBox2.Items.Add(19200)
ComboBox2.Items.Add(38400)
ComboBox2.Items.Add(57600)
ComboBox2.Items.Add(115200)
For Each port_name As String In IO.Ports.SerialPort.GetPortNames
Dim myPort As New IO.Ports.SerialPort(port_name)
If myPort.IsOpen = True Then
ComboBox1.Items.Add(port_name)
ComboBox1.SelectedIndex = 0 'set cmbComPort text to the first COM port detected
ComboBox2.SelectedIndex = 0 'set cmbBaudRate text to the first Baud rate on the list
ComboBox4.SelectedIndex = 0 'set cmbParity text to the first Baud rate on the list
ComboBox5.SelectedIndex = 0 'set cmbStopBit text to the first Baud rate on the list
End If
Next
End Sub
'For i = 0 To UBound(myPort)
' ComboBox1.Items.Add(myPort(i))
'Next
'ComboBox1.Text = ComboBox1.Items.Item(0) 'Set cmbPort text to the first COM port detected
'ComboBox1.Text = ComboBox1.Items.Item(0) 'Set cmbBaud text to the first Baud rate on the list
'End Sub

'Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Private Sub connect1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles connect1.Click
'SerialPort1.PortName = ComboBox1.Text 'set Serial Port to the selected COM port
'SerialPort1.BaudRate = ComboBox2.Text 'set Baud rate to the selected value Baud rate
'SerialPort1.Parity = ComboBox4.Text 'set parity setting to the selected value
'SerialPort1.StopBits = ComboBox5.Text 'set stop bit setting to the selected value

'SerialPort1.DataBits = ComboBox3.Text 'use the default 8 bit for data bit length

' SerialPort1.Open() 'open the chosen serial port
'connect1.Enabled = False 'disable the Connect button
'disconnect1.Enabled = True 'enable the Disconnect button
rs232.PortName = ComboBox1.Text 'set Serial Port to the selected COM port
'rs232.BaudRate = ComboBox2.Text 'set Baud rate to the selected value Baud rate
'rs232.Parity = ComboBox4.Text 'set parity setting to the selected value
'rs232.StopBits = ComboBox5.Text 'set stop bit setting to the selected value

'rs232.DataBits = ComboBox3.Text 'use the default 8 bit for data bit length

rs232.Open() 'open the chosen serial port
connect1.Enabled = False 'disable the Connect button
disconnect1.Enabled = True 'enable the Disconnect button
End Sub

'Private Sub disconnect1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles disconnect1.Click
'SerialPort1.Close() 'close the used Serial Port

'connect1.Enabled = True 'esable the Connect button
'disconnect1.Enabled = False 'disable the Disconnect button
'rs232.Close() 'close the used Serial Port

' connect1.Enabled = True 'esable the Connect button
' disconnect1.Enabled = False 'disable the Disconnect button
'End Sub


' Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged

'End Sub

Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
ComboBox3.Items.Add("8 bits")
ComboBox3.Items.Add("7 bits")
End Sub

'Private Sub ComboBox4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox4.SelectedIndexChanged

' End Sub

'Private Sub TableLayoutPanel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs)




'End Sub


'Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

' End Sub

' Private Sub Label19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label19.Click

' End Sub

' Private Sub GUI_amey1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

' End Sub



' Private Sub GroupBox3_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox3.Enter


'End Sub

' Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

' End Sub

'End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clear_1.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
RadioButton1.Checked = False
RadioButton2.Checked = False
CheckBox1.Checked = False
End Sub

' Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked
' terminate the project
' End Sub

Private Sub FileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileToolStripMenuItem.Click
' Call Start button
'Start_Click()
'Call to clear
'Clear_1.Show()






End Sub

Private Sub Exit_1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Exit_1.Click
Me.Close()

End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub



Private Sub HelpToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelpToolStripMenuItem.Click
AboutBox1.Show()
Hide()


End Sub



'
'Private Sub connect1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles connect1.Click
'Try



'SerialPort1.PortName = ComboBox1.Text() 'Set SerialPort1 to the selected COM port at startup
'SerialPort1.BaudRate = ComboBox2.Text() 'Set Baud rate to the selected value on
'Other Serial Port Property
' SerialPort1.Parity = IO.Ports.Parity.None
'' SerialPort1.StopBits = IO.Ports.StopBits.One
' SerialPort1.DataBits = 8 'Open our serial port
' SerialPort1.Open()
'Catch ex As Exception
'connect1.Enabled = False 'Disable Connect button
' disconnect1.Enabled = True
'End Try 'and Enable Disconnect button
'End Sub

' Private Sub disconnect1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles disconnect1.Click
'Try



'Catch ex As Exception
' SerialPort1.Close() 'Close our Serial Port
' connect1.Enabled = True
'disconnect1.Enabled = False
' End Try
' End Sub

' Private Sub trans1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trans1.Click
'Try

'Catch ex As Exception
' SerialPort1.Write(TBtrans.Text & vbCr) 'The text contained in the txtText will be sent to the serial port as ascii
'TBtrans.Text = "Data transmission started"


' End Try 'plus the carriage return (Enter Key) the carriage return can be ommitted if the other end does not need it
' End Sub


' Private Sub recv1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TBrecv1.Click
'TBrecv.Text = TBtrans.Text
'End Sub
'If RadioButton1.Checked = True Then
Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Write1.Click
Dim k As Integer = Integer.Parse(TextBox1.Text)
If TextBox1.Text = " " Then
TextBox1.Text = 0
Else
k += 1
TextBox1.Text = k.ToString()

End If

Dim l As Integer = Integer.Parse(TextBox3.Text)
If TextBox3.Text = "" Then
TextBox3.Text = 1
Else
l += 1
TextBox3.Text = l.ToString()
End If

Dim m As Integer = Integer.Parse(TextBox4.Text)
If TextBox4.Text = "" Then
TextBox4.Text = 1
Else
m += 1
TextBox4.Text = m.ToString()
End If

Dim n As Integer = Integer.Parse(TextBox5.Text)
If TextBox5.Text = "" Then
TextBox5.Text = 1
Else
n += 1
TextBox4.Text = n.ToString()
End If
Dim o As Integer = Integer.Parse(TextBox6.Text)
If TextBox6.Text = "" Then
TextBox6.Text = 16
End If

'Dim Result As WSMBS.Result
'WsmbsControl1.Mode = WSMBS.Mode.RTU 'Use standard Modbus RTU
'WsmbsControl1.PortName = "COM10"
'WsmbsControl1.BaudRate = 9600
'WsmbsControl1.StopBits = 1
'WsmbsControl1.Parity = WSMBS.Parity.None
'WsmbsControl1.ResponseTimeout = 1000 '1000ms
'Result = WsmbsControl1.Open()
'If Result <> WSMBS.Result.SUCCESS Then
'MessageBox.Show(WsmbsControl1.GetLastErrorString())
'End If

'If RadioButton2.Checked = True Then
' Button3_Click_1(sender, New System.EventArgs)

'Else
' MsgBox("Wrong function selected")
'End If

'Dim i As Integer
'Dim str1 As String
'str1 = "2C"
Dim DeviceAdd As Byte
Dim FuncAdd As Byte
Dim RegAddHI As Byte
Dim RegAddLW As Byte
Dim NoRegHI As Byte
Dim NoRegLW As Byte
Dim CRCHI As Byte
Dim CRCLW As Byte
DeviceAdd = &H5
FuncAdd = &H3
RegAddHI = &H1
'RegAddLW = System.Convert.ToString(str1)
RegAddLW = &H2C
NoRegHI = &H0
NoRegLW = &H12
CRCHI = &H4
CRCLW = &H76
'Dim DeviceAdd1 As Byte
'Dim FuncAdd1 As Byte
'Dim RegAddHI1 As Byte
'Dim RegAddLW1 As Byte
'Dim NoRegHI1 As Byte
'Dim NoRegLW1 As Byte
'Dim CRCHI1 As Byte
'Dim CRCLW1 As Byte
'DeviceAdd1 = System.Convert.ToByte(DeviceAdd)
'FuncAdd1 = System.Convert.ToByte(FuncAdd)
'RegAddHI1 = System.Convert.ToByte(RegAddHI)
'RegAddLW1 = System.Convert.ToByte(RegAddLW)
'NoRegHI1 = System.Convert.ToByte(NoRegHI)
'NoRegLW1 = System.Convert.ToByte(NoRegLW)
''CRCHI1 = System.Convert.ToByte(CRCHI)
'CRCLW1 = System.Convert.ToByte(CRCLW)

'System.Text.Encoding.Unicode.GetBytes(DeviceAdd)
'System.Text.Encoding.Unicode.GetBytes(FuncAdd)
'System.Text.Encoding.Unicode.GetBytes(RegAddHI)
'System.Text.Encoding.Unicode.GetBytes(RegAddLW)
'System.Text.Encoding.Unicode.GetBytes(NoRegHI)
'System.Text.Encoding.Unicode.GetBytes(NoRegLW)
'System.Text.Encoding.Unicode.GetBytes(CRCHI)
'System.Text.Encoding.Unicode.GetBytes(CRCLW)
' SendBuff(0) = &H5
'SendBuff(1) = &H3
'SendBuff(2) = &H1
'SendBuff(3) = &H2C
' SendBuff(4) = &H0
'SendBuff(5) = &H12
'SendBuff(6) = &H4
'SendBuff(7) = &H76
'For i = 0 To 7
'System.Text.Encoding.Unicode.GetBytes(SendBuff(i))
'Dim SendBuff() As Byte = {&H1, &H10, &H0, &H12, &H0, &H3, &H6, &H4, &HCB, &H34, &H56, &H1, &H53, &H4D, &H6} '&H52, &H7C}
'rs232.Write(SendBuff, 0, 15) 'SendBuff(i) + " ")
' NumericUpDown1.Value = NumericUpDown1.Value + 1
'Dim registers As Byte
'Dim message(15) As Byte
'message(6) = (registers * 2)
'Dim i As Integer
'Dim values() As Byte
'Dim X As Byte
'Dim Y As Byte
'Dim lbuf As Integer
'Dim k As Byte
'Dim CRC As Integer
'Dim buf() As Byte

'catch (Exception err)
'{
' modbusStatus = "Error in write event: " + err.Message;
' return false;
'}
'registers = 3
'For i = 0 To registers
'Next i

'X = message(6 + 2 * i) '= (values(i) >> 8)
'Y = message(7 + 2 * i) '= (values(i))
'Try
'Dim newcrc As String
'Dim j As Integer
'Dim CRCH As Byte
'Dim CRCLo As Byte

'newcrc = &HFFFF
'For j = 0 To 14

'Next j
'newcrc = updcrc(newcrc, AscW(ChrW(Message(j))))
'CRCH = (newcrc & &HFF00) >> 8
'CRCLo = (newcrc & &HFF)

' Public Sub int updcrc(integer crc,char c)
'End Sub
'Dim CRC1 As Integer


'Catch ex As Exception

'Try
' For i = 0 To lbuf - 1 'Step 1 ' for each byte
'CRC1 = CRC1 Xor buf(i)
'For j = 0 To 7 Step 1 ' for each bit
'k = CRC1 And 1 ' memo bit 0 state
'CRC1 = ((CRC1 And &HFFFE) / 2) 'And H7FFF ' Shift right with 0 at left
'If k > 0 Then CRC1 = CRC1 Xor &HA001 ' Bocuse
'Next j
'Next i
'CRC = CRC1
'End Sub
'Try
'CRC16(Crc, c)
'Message(14) = CRCH
'Message(15) = CRCLo
'rs232.Write(Message, 0, Message.Length)
'rs232.Write(Y)
'message(14) = CRCH
'message(15) = CRCLo
'rs232.Write(message(14))
'rs232.Write(message(15))
'Dim instance As StackOverflowException
Dim NUMDATA As Byte

Dim data(NUMDATA + 2) As Char ' Message buffer
Dim Crc_HByte As Char
Dim Crc_LByte As Char
Dim Crc As Integer
Dim i As Integer
Crc = &HFFFF
For i = 0 To NUMDATA - 1

Next i
Crc = CRC16(AscW(Crc), Val(data(i)))

Crc_LByte = (Crc & &HFF) ' Low byte calculation
Crc_HByte = ChrW(((Crc & &HFF00) >> 8))
'TextBox7.Text = Message(0)
'TextBox8.Text = Message(1)
'TextBox9.Text = Message(2)
'TextBox10.Text = Message(3)
'TextBox11.Text = Message(4)
'TextBox12.Text = Message(5)
'TextBox13.Text = Message(6)
'TextBox14.Text = Message(7)
'TextBox15.Text = Message(8)
'TextBox16.Text = Message(9)
'TextBox17.Text = Message(10)
'TextBox18.Text = Message(11)
'TextBox19.Text = Message(12)
'TextBox20.Text = Message(13)
'TextBox21.Text = Message(14)
'TextBox22.Text = Message(15)
' Catch ex As Exception
'MsgBox("Error in write function")
rs232.Write(data, 0, data.Length)
NumericUpDown2.Value = NumericUpDown2.Value + 1

'End Try

End Sub


'Label21.Text = TextBox7.Text
'Label22.Text = TextBox8.Text
'Label23.Text = TextBox9.Text
'Label24.Text = TextBox10.Text

'Next i

'Dim Registers(10) As Short
'Dim Result As WSMBS
'Result = WsmbsControl1.ReadHoldingRegisters(1, 0, 10, Registers)
'Dim RegData(7) As UShort ' array for receiving holding register values
'Dim Status As SMRN.SMRNControl.MB_STATUS ' variable for receiving return status
'Status = SmrnControl1.MbReadHoldingRegs(2, 0, 7, RegData)
'If (Status = SMRN.SMRNControl.MB_STATUS.MB_NO_ERROR) Then
' MbReadHoldingRegs succeeded. Process the holding register data.
' ....
'End If

'Start_Click(sender, New System.EventArgs)




Private Sub Open1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Open1.Click
Try
rs232.Open()
Catch ex As Exception
MsgBox(Err.Description)
End Try
If rs232.IsOpen = False Then
MsgBox("This port cannot be opened")
End If
End Sub

Private Sub Read1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Read1.Click

'rs232.ReadLine()
End Sub

'Private Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click
'Public Function WriteData(ByVal DeviceAdd As Byte, ByVal FuncAdd As Byte, ByVal RegAddHI As Byte, ByVal RegAddLW As Byte, ByVal NoRegHI As Byte, ByVal NoRegLW As Byte, ByVal CRCHI As Byte, ByVal CRCLW As Byte) As Boolean
'im InBuff() As Byte
'Dim Array(7) As Byte
'DeviceAdd = "05"
'FuncAdd = "03"
'RegAddHI = "01"
'RegAddLW = "2C"
'NoRegHI = "00"
'NoRegLW = "04"
'RCHI = "04"
'RCLW = "76"
'Array(0) = 5
'Array(1) = 3
''Array(2) = 1
'Array(3) = 2C
'Array(4) = 0
'Array(5) = 12
''Array(6) = 4
'Array(7) = 76




'End Function
'End Sub

Private Sub ExitToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()

End Sub

Private Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click


' Dim instance As StackOverflowException
'<SerializableAttribute()> _
'<ComVisibleAttribute(True)> _
'Public NotInheritable StackOverflowException _
'Inherits SystemException
' End
'Dim Registers(10) As Short
'Dim i As Integer
'Dim Result As WSMBS.Result
'For i = 0 To 9
'Registers(i) = i
'Next i
'Result = WsmbsControl1.WriteMultipleRegisters(1, 0, 10, Registers)
'If (Result <> WSMBS.Result.SUCCESS) Then
'MessageBox.Show(WsmbsControl1.GetLastErrorString())
'End If



'o += 1
'TextBox6.Text = o.ToString()
'Dim p As Integer = Integer.Parse(TextBox6.Text)
'p += 1
'TextBox6.Text = p.ToString()
Dim value As String
Label2.Text = value
If RadioButton2.Checked = True And TextBox5.Text = 16 Then
'Button3_Click_1(sender, New System.EventArgs)

Else
MsgBox("Wrong function selected")
End If
'TextBox7.Text = 12.26
'TextBox8.Text = 34.56
'TextBox9.Text = 1.5
'TextBox11.Text = 0
'TextBox12.Text = 3
'TextBox13.Text = 6
'TextBox14.Text = 1
'TextBox15.Text = 25
'TextBox16.Text = 34
'TextBox17.Text = 56
'TextBox18.Text = 1
'TextBox19.Text = 50
'TextBox20.Text = 5
'TextBox21.Text = 45
'TextBox7.Text = SendBuff(0)
'Dim i As Integer
' Dim str As String


'i = Hex(44)

'Dim str As String Conversion.Hex(i)

'str = i.ToString("X")
'Console.WriteLine(str)

'TextBox10.Text = 12
'TextBox10.Text = str

'Console.WriteLine(str)
'str = 44
' str = 12625.ToString("2C")
'str = System.Uri.HexEscape((Char)64);
'TextBox10.Text = "str"
'Dim SendBuff(7) As String
' Dim DeviceAdd As String
'Dim FuncAdd As String
' Dim RegAddHI As String
'Dim RegAddLW As String
'Dim NoRegHI As String
'Dim NoRegLW As String
' Dim CRCHI As String
' Dim CRCLW As String
'DeviceAdd = "05"
' 'FuncAdd = "03"
'RegAddHI = "01"
' RegAddLW = "2C"
' NoRegHI = "00"
' NoRegLW = "12"
' CRCHI = "04"
'CRCLW = "76"
'SendBuff(0) = DeviceAdd
'SendBuff(1) = FuncAdd
'SendBuff(2) = RegAddHI
'SendBuff(3) = RegAddLW
'SendBuff(4) = NoRegHI
'SendBuff(5) = NoRegLW
'SendBuff(6) = CRCHI
' SendBuff(7) = CRCLW


'rs232.WriteLine("05,03,01,2C,00,12,04,76")
'Dim SendBuff(7) As Byte

' Using com9 As IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort("COM9")
'Dim DeviceAdd As String
'Dim FuncAdd As String
'Dim RegAddHI As String
'Dim RegAddLW As String
'Dim NoRegHI As String
'Dim NoRegLW As String
'Dim CRCHI As String
'Dim CRCLW As String
'DeviceAdd = "05"
' FuncAdd = "03"
'RegAddHI = "01"
'RegAddLW = System.Convert.ToString(str1)
'RegAddLW = "2C"
'NoRegHI = "00"
'NoRegLW = "12"
'CRCHI = "04"
'CRCLW = "76"
' com9.WriteLine(DeviceAdd)
'com9.WriteLine(FuncAdd)
'com9.WriteLine(RegAddHI)
'com9.WriteLine(RegAddLW)
'com9.WriteLine(NoRegHI)
'com9.WriteLine(NoRegLW)
'com9.WriteLine(CRCHI)
'com9.WriteLine(CRCLW)
' End Using
'For i = 0 To 7
'System.Text.Encoding.Unicode.GetBytes(SendBuff(i))
'rs232.WriteLine(SendBuff(i) + " ")
'Next i
'End Sub
'Sub SendSerialData(ByVal data As String)
'Button3_Click_1(sender, New System.EventArgs)

End Sub
Private Sub TextBox7_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox7.TextChanged
'TextBox7.Text = &H5
'TextBox7.Update()
End Sub

Private Sub TextBox8_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox8.TextChanged
'TextBox8.Text = &H3
End Sub

Private Sub TextBox9_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox9.TextChanged
'TextBox9.Text = &H1
End Sub

Private Sub TextBox10_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox10.TextChanged
'TextBox10.Text = &H2C
End Sub

Private Sub TextBox11_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox11.TextChanged
'TextBox11.Text = &H0
End Sub

Private Sub TextBox12_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox12.TextChanged
'TextBox12.Text = &H12
End Sub

Private Sub TextBox13_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox13.TextChanged
'TextBox13.Text = &H4
End Sub

Private Sub TextBox14_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox14.TextChanged
'TextBox14.Text = &H76
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
'If Port.IsOpen = True Then

'End If
End Sub


Private Sub NumericUpDown1_ValueChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged

End Sub

Private Sub Label21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label21.Click

End Sub
' Public Function updcrc(ByVal crc As Byte, ByVal c As Byte)
'Dim Bit_Shift As Byte
' For Bit_Shift = 0 To 8
' Next Bit_Shift
' If (crc ^ c & 1) Then
' crc = ((crc >> 1) ^ (&HA001))
'Else
' c >>= 1
' Return crc
'End If
'End Function
'' Private Sub Calcul_CRC(ByVal snd)
'Dim crc1 As Integer
'Dim result As String
'Dim flag As Byte

' ***** Calcul CRC *****
'Dim CRC11 As Integer
'Dim CRC22 As Integer
' CRC11 = 0
'CRC22 = 0
'crc1 = &HFFFF

'For j = 1 To Len(snd)
' crc1 = crc1 Xor Asc(Mid(snd, j, 1))
'For i = 1 To 16
' If ((crc1 Mod 2) = 1) Or ((crc1 Mod 2) = -1) Then flag = 1 Else flag = 0
' crc1 = Int(crc1 / 2)
'crc1 = crc1 And &H7FFF
'If flag Then crc1 = crc1 Xor &HA001
' 'Next i
'Next j

'result = Format(Hex(crc1), "@@@@") ' Formatte sur 4 car
'Debug.Print(result)

'result = Right(result, 4)
'CRC11 = Val("&h" & Left((result), 2))
'CRC22 = Val("&h" & Right((result), 2))
'Debug.Print(CRC11, CRC22)
' Text9.Text = Hex(CRC2) & Hex(CRC1)

'End Sub


' Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged

'End Sub
Public Function CRC16(ByVal crc As Integer, ByVal data As Integer)

Const Poly16 As Integer = &HA001
Dim LSB As Integer
Dim i As Integer
crc = ((crc ^ data) Or &HFF00) & (crc Or &HFF)
For i = 0 To 8

Next i
LSB = (crc & &H1)
crc = crc / 2
If (LSB) Then

End If
crc = crc ^ Poly16

Return (crc)
End Function

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
' Dim i As Integer
'For i = 1 To 100
'TextBox1.Text = i
'Next i
End Sub

Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
'Dim i As Integer
'For i = 1 To 100
' TextBox6.Text = i
' Next i
End Sub

Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
'If RadioButton2.Text = "Write_Multiple_Register" Then
'Start_Click.enabled = True

'End If
'Dim value As String
'value = "Write_Multiple_Register "
End Sub

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
'Dim i As Integer
'For i = 0 To 95
'TextBox3.Text = i

'Next i

End Sub

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged

End Sub

Private Sub GroupBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub
End Class</pre>
 
Top