Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System
- Imports System.ComponentModel
- Imports System.Threading
- Imports System.IO.Ports
- Public Class Form1
- 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
- Dim high As Byte = 0
- Dim low As Byte = 0
- Dim HexString As String
- Dim CRCres As Byte()
- Dim Data As Byte()
- Dim ReceivedString As String
- Dim Received As Byte()
- Dim Fcode As Integer
- Dim Reset As String = "00"
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- TabControl1.SelectedTab = TabPage2
- '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
- For i = 0 To UBound(myPort)
- cmbPort.Items.Add(myPort(i))
- Next
- cmbPort.Text = cmbPort.Items.Item(0) 'Set cmbPort text to the first COM port detected
- End Sub
- Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
- If SerialPort1.IsOpen = False Then
- 'IBox1.Image = My.Resources.Status_GREEN
- SerialPort1.PortName = cmbPort.Text 'Set SerialPort1 to the selected COM port at startup
- 'Other Serial Port Property
- SerialPort1.BaudRate = 38400
- SerialPort1.Parity = IO.Ports.Parity.None
- SerialPort1.StopBits = IO.Ports.StopBits.One
- SerialPort1.DataBits = 8 'Open our serial port
- SerialPort1.Open()
- btnConnect.Text = "Disconnect"
- Timer1.Start()
- ElseIf SerialPort1.IsOpen = True Then
- IBox1.Image = My.Resources.Status_RED
- SerialPort1.Close()
- btnConnect.Text = "Connect"
- Timer1.Stop()
- End If
- End Sub
- Function ToHex(ByVal i As Integer) As String
- Return i.ToString("X2")
- End Function
- Function HexStringToByteArray(ByVal hex As String) As Byte()
- Dim NumChars = hex.Length
- Dim Bytes(NumChars / 2) As Byte
- For i As Integer = 0 To NumChars - 1 Step 2
- Bytes(i / 2) = Convert.ToByte(hex.Substring(i, 2), 16)
- Next
- Return Bytes
- End Function
- Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
- If SerialPort1.IsOpen = True Then
- CRCres = BitConverter.GetBytes(CRC16(HexStringToByteArray("010F0001000C02" + ToHex(low) + ToHex(high)), 9))
- HexString = "010F0001000C02" + ToHex(low) + ToHex(high) + ToHex(CRCres(0)) + ToHex(CRCres(1))
- Data = HexStringToByteArray(HexString)
- SerialPort1.Write(Data, 0, 11)
- 'Timer2.Start()
- 'Timer1.Stop()
- End If
- End Sub
- ' Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
- ' If SerialPort1.IsOpen = True Then
- ' HexString = "0102000100102806"
- ' Data = HexStringToByteArray(HexString)
- ' SerialPort1.Write(Data, 0, 8)
- ' Timer2.Stop()
- ' Timer1.Start()
- ' End If
- ' End Sub
- Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
- ReceivedString = SerialPort1.ReadExisting() 'Automatically called every time a data is received at the serialPort
- Received = System.Text.Encoding.UTF8.GetBytes(ReceivedString)
- 'If CRC16(Received, Received.Length) = 0 Then
- 'MessageBox.Show("Buo")
- ' End If
- If Received.Length >= 6 Then
- If Received(1) = 15 Then
- MessageBox.Show("hey")
- End If
- ReDim Received(0)
- End If
- End Sub
- ' Private Sub serialPort1_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
- 'Dim rx As Integer
- ' rx = SerialPort1.BytesToRead
- ' Dim comBuff As Byte() = New Byte(rx - 1) {}
- ' SerialPort1.Read(comBuff, 0, rx)
- ' MessageBox.Show(rx)
- ' txtRxData.Invoke(New DisplayDelegate(AddressOf DisplayCharacter), New Object() {BytetoHex(comBuff)})
- 'End Sub
- Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
- low = &HFF
- high = &HFF
- OBox1.Image = My.Resources.Status_GREEN
- OBox2.Image = My.Resources.Status_GREEN
- OBox3.Image = My.Resources.Status_GREEN
- OBox4.Image = My.Resources.Status_GREEN
- OBox5.Image = My.Resources.Status_GREEN
- OBox6.Image = My.Resources.Status_GREEN
- OBox7.Image = My.Resources.Status_GREEN
- OBox8.Image = My.Resources.Status_GREEN
- OBox9.Image = My.Resources.Status_GREEN
- OBox10.Image = My.Resources.Status_GREEN
- OBox11.Image = My.Resources.Status_GREEN
- OBox12.Image = My.Resources.Status_GREEN
- End Sub
- Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
- low = &H0
- high = &H0
- OBox1.Image = My.Resources.Status_RED
- OBox2.Image = My.Resources.Status_RED
- OBox3.Image = My.Resources.Status_RED
- OBox4.Image = My.Resources.Status_RED
- OBox5.Image = My.Resources.Status_RED
- OBox6.Image = My.Resources.Status_RED
- OBox7.Image = My.Resources.Status_RED
- OBox8.Image = My.Resources.Status_RED
- OBox9.Image = My.Resources.Status_RED
- OBox10.Image = My.Resources.Status_RED
- OBox11.Image = My.Resources.Status_RED
- OBox12.Image = My.Resources.Status_RED
- End Sub
- Private Sub OBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox1.Click
- If ((low And &H1) Xor &H1) = &H1 Then
- OBox1.Image = My.Resources.Status_GREEN
- low = low Or &H1
- Else
- OBox1.Image = My.Resources.Status_RED
- low = low Xor &H1
- End If
- End Sub
- Private Sub OBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox2.Click
- If ((low And &H2) Xor &H2) = &H2 Then
- OBox2.Image = My.Resources.Status_GREEN
- low = low Or &H2
- Else
- OBox2.Image = My.Resources.Status_RED
- low = low Xor &H2
- End If
- End Sub
- Private Sub OBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox3.Click
- If ((low And &H4) Xor &H4) = &H4 Then
- OBox3.Image = My.Resources.Status_GREEN
- low = low Or &H4
- Else
- OBox3.Image = My.Resources.Status_RED
- low = low Xor &H4
- End If
- End Sub
- Private Sub OBox4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox4.Click
- If ((low And &H8) Xor &H8) = &H8 Then
- OBox4.Image = My.Resources.Status_GREEN
- low = low Or &H8
- Else
- OBox4.Image = My.Resources.Status_RED
- low = low Xor &H8
- End If
- End Sub
- Private Sub OBox5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox5.Click
- If ((low And &H10) Xor &H10) = &H10 Then
- OBox5.Image = My.Resources.Status_GREEN
- low = low Or &H10
- Else
- OBox5.Image = My.Resources.Status_RED
- low = low Xor &H10
- End If
- End Sub
- Private Sub OBox6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox6.Click
- If ((low And &H20) Xor &H20) = &H20 Then
- OBox6.Image = My.Resources.Status_GREEN
- low = low Or &H20
- Else
- OBox6.Image = My.Resources.Status_RED
- low = low Xor &H20
- End If
- End Sub
- Private Sub OBox7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox7.Click
- If ((low And &H40) Xor &H40) = &H40 Then
- OBox7.Image = My.Resources.Status_GREEN
- low = low Or &H40
- Else
- OBox7.Image = My.Resources.Status_RED
- low = low Xor &H40
- End If
- End Sub
- Private Sub OBox8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox8.Click
- If ((low And &H80) Xor &H80) = &H80 Then
- OBox8.Image = My.Resources.Status_GREEN
- low = low Or &H80
- Else
- OBox8.Image = My.Resources.Status_RED
- low = low Xor &H80
- End If
- End Sub
- Private Sub OBox9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox9.Click
- If ((high And &H1) Xor &H1) = &H1 Then
- OBox9.Image = My.Resources.Status_GREEN
- high = high Or &H1
- Else
- OBox9.Image = My.Resources.Status_RED
- high = high Xor &H1
- End If
- End Sub
- Private Sub OBox10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox10.Click
- If ((high And &H2) Xor &H2) = &H2 Then
- OBox10.Image = My.Resources.Status_GREEN
- high = high Or &H2
- Else
- OBox10.Image = My.Resources.Status_RED
- high = high Xor &H2
- End If
- End Sub
- Private Sub OBox11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox11.Click
- If ((high And &H4) Xor &H4) = &H4 Then
- OBox11.Image = My.Resources.Status_GREEN
- high = high Or &H4
- Else
- OBox11.Image = My.Resources.Status_RED
- high = high Xor &H4
- End If
- End Sub
- Private Sub OBox12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox12.Click
- If ((high And &H8) Xor &H8) = &H8 Then
- OBox12.Image = My.Resources.Status_GREEN
- high = high Or &H8
- Else
- OBox12.Image = My.Resources.Status_RED
- high = high Xor &H8
- End If
- End Sub
- Function CRC16(ByVal MB_aray() As Byte, ByVal mNum As Integer) As UInteger
- Dim i As Long = 0
- Dim Temp As UInteger = 0
- Dim CRC As UInteger = 65535
- Dim j As Integer = 0
- For i = 0 To mNum - 1
- Temp = (CRC)
- CRC = Temp Xor (MB_aray(i))
- For j = 0 To 7
- If (CRC And 1) Then
- CRC = ((CRC >> 1) Xor 40961) '&H1021&)
- Else
- CRC = (CRC >> 1) 'And 65535
- End If
- Next j
- Next i
- CRC16 = CRC And 65535
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement