Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 11.04 KB | None | 0 0
  1. Imports System
  2. Imports System.ComponentModel
  3. Imports System.Threading
  4. Imports System.IO.Ports
  5.  
  6. Public Class Form1
  7.  
  8.     Dim myPort As Array  'COM Ports detected on the system will be stored here
  9.     Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
  10.     Dim high As Byte = 0
  11.     Dim low As Byte = 0
  12.     Dim HexString As String
  13.     Dim CRCres As Byte()
  14.     Dim Data As Byte()
  15.     Dim ReceivedString As String
  16.     Dim Received As Byte()
  17.     Dim Fcode As Integer
  18.     Dim Reset As String = "00"
  19.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  20.         TabControl1.SelectedTab = TabPage2
  21.         'When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box.
  22.         myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available
  23.  
  24.  
  25.         For i = 0 To UBound(myPort)
  26.             cmbPort.Items.Add(myPort(i))
  27.         Next
  28.         cmbPort.Text = cmbPort.Items.Item(0)    'Set cmbPort text to the first COM port detected
  29.  
  30.  
  31.  
  32.  
  33.     End Sub
  34.     Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
  35.         If SerialPort1.IsOpen = False Then
  36.  
  37.             'IBox1.Image = My.Resources.Status_GREEN
  38.  
  39.             SerialPort1.PortName = cmbPort.Text         'Set SerialPort1 to the selected COM port at startup
  40.             'Other Serial Port Property
  41.             SerialPort1.BaudRate = 38400
  42.             SerialPort1.Parity = IO.Ports.Parity.None
  43.             SerialPort1.StopBits = IO.Ports.StopBits.One
  44.             SerialPort1.DataBits = 8            'Open our serial port
  45.             SerialPort1.Open()
  46.  
  47.             btnConnect.Text = "Disconnect"
  48.             Timer1.Start()
  49.         ElseIf SerialPort1.IsOpen = True Then
  50.             IBox1.Image = My.Resources.Status_RED
  51.  
  52.             SerialPort1.Close()
  53.             btnConnect.Text = "Connect"
  54.             Timer1.Stop()
  55.         End If
  56.  
  57.     End Sub
  58.  
  59.  
  60.     Function ToHex(ByVal i As Integer) As String
  61.         Return i.ToString("X2")
  62.     End Function
  63.  
  64.     Function HexStringToByteArray(ByVal hex As String) As Byte()
  65.         Dim NumChars = hex.Length
  66.         Dim Bytes(NumChars / 2) As Byte
  67.         For i As Integer = 0 To NumChars - 1 Step 2
  68.             Bytes(i / 2) = Convert.ToByte(hex.Substring(i, 2), 16)
  69.         Next
  70.         Return Bytes
  71.     End Function
  72.  
  73.  
  74.  
  75.  
  76.  
  77.        
  78.  
  79.  
  80.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  81.         If SerialPort1.IsOpen = True Then
  82.  
  83.             CRCres = BitConverter.GetBytes(CRC16(HexStringToByteArray("010F0001000C02" + ToHex(low) + ToHex(high)), 9))
  84.  
  85.             HexString = "010F0001000C02" + ToHex(low) + ToHex(high) + ToHex(CRCres(0)) + ToHex(CRCres(1))
  86.  
  87.             Data = HexStringToByteArray(HexString)
  88.             SerialPort1.Write(Data, 0, 11)
  89.             'Timer2.Start()
  90.             'Timer1.Stop()
  91.  
  92.         End If
  93.  
  94.     End Sub
  95.  
  96.     ' Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
  97.     '     If SerialPort1.IsOpen = True Then
  98.     '          HexString = "0102000100102806"
  99.     '       Data = HexStringToByteArray(HexString)
  100.     '       SerialPort1.Write(Data, 0, 8)
  101.     '         Timer2.Stop()
  102.     '         Timer1.Start()
  103.     '     End If
  104.  
  105.     ' End Sub
  106.  
  107.  
  108.  
  109.     Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
  110.  
  111.         ReceivedString = SerialPort1.ReadExisting()    'Automatically called every time a data is received at the serialPort
  112.  
  113.         Received = System.Text.Encoding.UTF8.GetBytes(ReceivedString)
  114.  
  115.         'If CRC16(Received, Received.Length) = 0 Then
  116.         'MessageBox.Show("Buo")
  117.  
  118.         ' End If
  119.        
  120.         If Received.Length >= 6 Then
  121.  
  122.             If Received(1) = 15 Then
  123.                 MessageBox.Show("hey")
  124.             End If
  125.          
  126.             ReDim Received(0)
  127.  
  128.         End If
  129.     End Sub
  130.  
  131.     ' Private Sub serialPort1_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
  132.     'Dim rx As Integer
  133.     '     rx = SerialPort1.BytesToRead
  134.     ' Dim comBuff As Byte() = New Byte(rx - 1) {}
  135.     '    SerialPort1.Read(comBuff, 0, rx)
  136.     '    MessageBox.Show(rx)
  137.     '    txtRxData.Invoke(New DisplayDelegate(AddressOf DisplayCharacter), New Object() {BytetoHex(comBuff)})
  138.     'End Sub
  139.  
  140.  
  141.  
  142.  
  143.  
  144.     Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
  145.         low = &HFF
  146.         high = &HFF
  147.         OBox1.Image = My.Resources.Status_GREEN
  148.         OBox2.Image = My.Resources.Status_GREEN
  149.         OBox3.Image = My.Resources.Status_GREEN
  150.         OBox4.Image = My.Resources.Status_GREEN
  151.         OBox5.Image = My.Resources.Status_GREEN
  152.         OBox6.Image = My.Resources.Status_GREEN
  153.         OBox7.Image = My.Resources.Status_GREEN
  154.         OBox8.Image = My.Resources.Status_GREEN
  155.         OBox9.Image = My.Resources.Status_GREEN
  156.         OBox10.Image = My.Resources.Status_GREEN
  157.         OBox11.Image = My.Resources.Status_GREEN
  158.         OBox12.Image = My.Resources.Status_GREEN
  159.  
  160.     End Sub
  161.  
  162.     Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
  163.         low = &H0
  164.         high = &H0
  165.         OBox1.Image = My.Resources.Status_RED
  166.         OBox2.Image = My.Resources.Status_RED
  167.         OBox3.Image = My.Resources.Status_RED
  168.         OBox4.Image = My.Resources.Status_RED
  169.         OBox5.Image = My.Resources.Status_RED
  170.         OBox6.Image = My.Resources.Status_RED
  171.         OBox7.Image = My.Resources.Status_RED
  172.         OBox8.Image = My.Resources.Status_RED
  173.         OBox9.Image = My.Resources.Status_RED
  174.         OBox10.Image = My.Resources.Status_RED
  175.         OBox11.Image = My.Resources.Status_RED
  176.         OBox12.Image = My.Resources.Status_RED
  177.  
  178.     End Sub
  179.  
  180.  
  181.  
  182.  
  183.     Private Sub OBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox1.Click
  184.         If ((low And &H1) Xor &H1) = &H1 Then
  185.  
  186.             OBox1.Image = My.Resources.Status_GREEN
  187.             low = low Or &H1
  188.  
  189.         Else
  190.  
  191.             OBox1.Image = My.Resources.Status_RED
  192.             low = low Xor &H1
  193.         End If
  194.  
  195.     End Sub
  196.  
  197.  
  198.  
  199.     Private Sub OBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox2.Click
  200.         If ((low And &H2) Xor &H2) = &H2 Then
  201.             OBox2.Image = My.Resources.Status_GREEN
  202.             low = low Or &H2
  203.  
  204.         Else
  205.  
  206.             OBox2.Image = My.Resources.Status_RED
  207.             low = low Xor &H2
  208.         End If
  209.     End Sub
  210.  
  211.     Private Sub OBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox3.Click
  212.         If ((low And &H4) Xor &H4) = &H4 Then
  213.  
  214.             OBox3.Image = My.Resources.Status_GREEN
  215.             low = low Or &H4
  216.  
  217.         Else
  218.  
  219.             OBox3.Image = My.Resources.Status_RED
  220.             low = low Xor &H4
  221.         End If
  222.  
  223.     End Sub
  224.  
  225.     Private Sub OBox4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox4.Click
  226.         If ((low And &H8) Xor &H8) = &H8 Then
  227.  
  228.             OBox4.Image = My.Resources.Status_GREEN
  229.             low = low Or &H8
  230.  
  231.         Else
  232.  
  233.             OBox4.Image = My.Resources.Status_RED
  234.             low = low Xor &H8
  235.  
  236.         End If
  237.     End Sub
  238.  
  239.     Private Sub OBox5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox5.Click
  240.         If ((low And &H10) Xor &H10) = &H10 Then
  241.  
  242.             OBox5.Image = My.Resources.Status_GREEN
  243.             low = low Or &H10
  244.  
  245.         Else
  246.  
  247.             OBox5.Image = My.Resources.Status_RED
  248.             low = low Xor &H10
  249.  
  250.         End If
  251.     End Sub
  252.  
  253.     Private Sub OBox6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox6.Click
  254.         If ((low And &H20) Xor &H20) = &H20 Then
  255.  
  256.             OBox6.Image = My.Resources.Status_GREEN
  257.             low = low Or &H20
  258.  
  259.         Else
  260.  
  261.             OBox6.Image = My.Resources.Status_RED
  262.             low = low Xor &H20
  263.  
  264.         End If
  265.     End Sub
  266.  
  267.     Private Sub OBox7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox7.Click
  268.         If ((low And &H40) Xor &H40) = &H40 Then
  269.  
  270.             OBox7.Image = My.Resources.Status_GREEN
  271.             low = low Or &H40
  272.  
  273.         Else
  274.  
  275.             OBox7.Image = My.Resources.Status_RED
  276.             low = low Xor &H40
  277.  
  278.         End If
  279.     End Sub
  280.  
  281.     Private Sub OBox8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox8.Click
  282.         If ((low And &H80) Xor &H80) = &H80 Then
  283.  
  284.             OBox8.Image = My.Resources.Status_GREEN
  285.             low = low Or &H80
  286.  
  287.         Else
  288.  
  289.             OBox8.Image = My.Resources.Status_RED
  290.             low = low Xor &H80
  291.  
  292.         End If
  293.     End Sub
  294.  
  295.     Private Sub OBox9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox9.Click
  296.         If ((high And &H1) Xor &H1) = &H1 Then
  297.  
  298.             OBox9.Image = My.Resources.Status_GREEN
  299.             high = high Or &H1
  300.         Else
  301.  
  302.             OBox9.Image = My.Resources.Status_RED
  303.             high = high Xor &H1
  304.         End If
  305.     End Sub
  306.  
  307.     Private Sub OBox10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox10.Click
  308.         If ((high And &H2) Xor &H2) = &H2 Then
  309.             OBox10.Image = My.Resources.Status_GREEN
  310.             high = high Or &H2
  311.  
  312.         Else
  313.  
  314.             OBox10.Image = My.Resources.Status_RED
  315.             high = high Xor &H2
  316.  
  317.         End If
  318.     End Sub
  319.  
  320.     Private Sub OBox11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox11.Click
  321.         If ((high And &H4) Xor &H4) = &H4 Then
  322.  
  323.             OBox11.Image = My.Resources.Status_GREEN
  324.             high = high Or &H4
  325.         Else
  326.  
  327.             OBox11.Image = My.Resources.Status_RED
  328.             high = high Xor &H4
  329.         End If
  330.     End Sub
  331.  
  332.     Private Sub OBox12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OBox12.Click
  333.         If ((high And &H8) Xor &H8) = &H8 Then
  334.             OBox12.Image = My.Resources.Status_GREEN
  335.             high = high Or &H8
  336.  
  337.         Else
  338.  
  339.             OBox12.Image = My.Resources.Status_RED
  340.             high = high Xor &H8
  341.         End If
  342.     End Sub
  343.  
  344.     Function CRC16(ByVal MB_aray() As Byte, ByVal mNum As Integer) As UInteger
  345.  
  346.         Dim i As Long = 0
  347.         Dim Temp As UInteger = 0
  348.         Dim CRC As UInteger = 65535
  349.         Dim j As Integer = 0
  350.         For i = 0 To mNum - 1
  351.             Temp = (CRC)
  352.             CRC = Temp Xor (MB_aray(i))
  353.             For j = 0 To 7
  354.                 If (CRC And 1) Then
  355.                     CRC = ((CRC >> 1) Xor 40961) '&H1021&)
  356.                 Else
  357.                     CRC = (CRC >> 1) 'And 65535
  358.                 End If
  359.             Next j
  360.         Next i
  361.         CRC16 = CRC And 65535
  362.  
  363.     End Function
  364.  
  365.  
  366.  
  367.  
  368. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement