Guest User

Untitled

a guest
Sep 19th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 11.54 KB | None | 0 0
  1.  
  2. Imports System.Net
  3. Imports System.Net.Sockets
  4. Imports System.Text
  5. Imports System.Threading
  6. Imports System.Net.NetworkInformation
  7.  
  8. Public Class runTestFrma
  9.  
  10.     'Counter used to compare step's run time against its wait time
  11.     Dim StepTimerCounter As Integer = 0
  12.  
  13.     'current step variables
  14.     Dim StepCommand As String = ""
  15.     Dim StepPrompt As String = ""
  16.     Dim StepArg1 As String = ""
  17.     Dim StepArg2 As String = ""
  18.     Dim StepArg3 As String = ""
  19.     Dim StepArg4 As String = ""
  20.     Dim StepWait As String = ""
  21.     Dim StepOnPass As String = ""
  22.     Dim StepOnFail As String = ""
  23.  
  24.     Dim currentstep As Integer = 0
  25.  
  26.     'Each procedure hands off to another procedure, this variable tracks which we're currently on.
  27.     Dim currentprocedure As String = ""
  28.  
  29.     Dim threadcontinue As Boolean = False
  30.  
  31.     Dim port As Int32 = 1111
  32.     Dim client As New TcpClient("192.168.1.127", port)
  33.     Dim stream As NetworkStream = client.GetStream()
  34.     Private Sub RunTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  35.         partIdTxt.Focus()
  36.         partIdTxt.SelectAll()
  37.         If stream.CanRead Then
  38.             GetData.Start()
  39.         Else
  40.             MsgBox("ERROR 1")
  41.         End If
  42.     End Sub
  43.     Private Sub RunTest_Close() Handles MyBase.FormClosed
  44.         mainMenuFrm.Show()
  45.     End Sub
  46.     Private Sub GetData_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetData.Tick
  47.         Connect("192.168.1.127")
  48.     End Sub
  49.     Private Sub Connect(ByVal server As [String])
  50.         Try
  51.             ' Translate the passed message into ASCII and store it as a Byte array.
  52.             Dim data(1) As Byte
  53.             data(0) = 0
  54.             data(1) = 1
  55.  
  56.  
  57.             ' Get a client stream for reading and writing.
  58.             '  Stream stream = client.GetStream();
  59.  
  60.  
  61.             ' Send the message to the connected TcpServer.
  62.             stream.Write(data, 0, data.Length)
  63.  
  64.             ' Receive the TcpServer.response.
  65.             ' Buffer to store the response bytes.
  66.             data = New [Byte](115) {}
  67.  
  68.             ' String to store the response ASCII representation.
  69.             Dim responseData As [String] = [String].Empty
  70.  
  71.             ' Read the first batch of the TcpServer response bytes.
  72.             Dim bytes As Int32 = stream.Read(data, 0, data.Length)
  73.  
  74.             'PARSE DATA HERE
  75.             calLbl.Text = CDec(data(1)).ToString
  76.  
  77.             doboardvoltages(data)
  78.  
  79.             Dim bytesa(1) As Byte
  80.             'Ref 02 Voltage
  81.             bytesa(0) = data(66)
  82.             bytesa(1) = data(67)
  83.             ref02Lbl.Text = GetVoltageFromBytes(bytesa).ToString("0.00")
  84.             '3.3 Voltage
  85.             bytesa(0) = data(68)
  86.             bytesa(1) = data(69)
  87.             v33Lbl.Text = GetVoltageFromBytes(bytesa).ToString("0.00")
  88.             '5 Voltage
  89.             bytesa(0) = data(70)
  90.             bytesa(1) = data(71)
  91.             v5Lbl.Text = GetVoltageFromBytes(bytesa).ToString("0.00")
  92.             '12v Voltage
  93.             bytesa(0) = data(72)
  94.             bytesa(1) = data(73)
  95.             v12Lbl.Text = GetVoltageFromBytes(bytesa).ToString("0.00")
  96.             'VEH Voltage
  97.             bytesa(0) = data(74)
  98.             bytesa(1) = data(75)
  99.             vehPwrOutLbl.Text = GetVoltageFromBytes(bytesa).ToString("0.00")
  100.             'Ground Voltage
  101.             bytesa(0) = data(80)
  102.             bytesa(1) = data(81)
  103.             groundLbl.Text = GetVoltageFromBytes(bytesa).ToString("0.00")
  104.  
  105.  
  106.         Catch e As ArgumentNullException
  107.             MsgBox("ArgumentNullException: {0}" & e.ToString())
  108.         Catch e As SocketException
  109.             MsgBox("SocketException: {0}" & e.ToString())
  110.         End Try
  111.  
  112.  
  113.     End Sub 'Connect
  114.     Private Sub doboardvoltages(ByVal bytes() As Byte)
  115.         For i As Integer = 1 To 32
  116.  
  117.             Dim bytesa(1) As Byte
  118.             bytesa(0) = bytes(i * 2)
  119.             bytesa(1) = bytes(i * 2 + 1)
  120.             CType(Panel2.Controls("channelactive" & i), Label).Text = GetVoltageFromBytes(bytesa).ToString("0.00")
  121.  
  122.             If CDec(CType(Panel2.Controls("channelmin" & i), Label).Text) > CDec(CType(Panel2.Controls("channelactive" & i), Label).Text) Then
  123.                 CType(Panel2.Controls("channelmin" & i), Label).Text = CType(Panel2.Controls("channelactive" & i), Label).Text
  124.             End If
  125.  
  126.             If CDec(CType(Panel2.Controls("channelmax" & i), Label).Text) < CDec(CType(Panel2.Controls("channelactive" & i), Label).Text) Then
  127.                 CType(Panel2.Controls("channelmax" & i), Label).Text = CType(Panel2.Controls("channelactive" & i), Label).Text
  128.             End If
  129.  
  130.             Dim setvalue As String = ""
  131.  
  132.             Select Case bytes(81 + i).ToString()
  133.                 Case "0"
  134.                     setvalue = "No"
  135.                 Case "1"
  136.                     setvalue = "G"
  137.                 Case "2"
  138.                     setvalue = "N"
  139.                 Case "6"
  140.                     setvalue = "F"
  141.                 Case "7"
  142.                     setvalue = "P"
  143.                 Case "11"
  144.                     setvalue = "H"
  145.             End Select
  146.             CType(Panel2.Controls("channelbias" & i), Label).Text = setvalue
  147.  
  148.         Next
  149.     End Sub
  150.     Private Function GetVoltageFromBytes(ByVal bytes() As Byte) As Double
  151.  
  152.         Dim voltage As Double = 0.0
  153.  
  154.         voltage = (((bytes(0) * 256.0) + bytes(1)))
  155.         voltage -= 32768 ' 0x8000 = 32768
  156.         voltage = (voltage * 2.0) * (20.0 / 65535.0)
  157.  
  158.         Return voltage
  159.     End Function
  160.     'Loads required procedure by argument into datagrid
  161.     'THIS FUNCTION APPEARS TO BE WORKING FINE
  162.     Private Sub loadcsvtodatagrid(ByVal filename As String)
  163.         'we have to stop the timer just so it cannot tick while we're trying to load the file!
  164.         StepTimer.Stop()
  165.  
  166.         'Load in CSV File
  167.         StepGrid.Rows.Clear()
  168.         If System.IO.File.Exists("c:\csv\" & filename & ".csv") Then
  169.             Try
  170.                 For Each line As String In System.IO.File.ReadAllLines("c:\csv\" & filename & ".csv")
  171.  
  172.                     StepGrid.Rows.Add(line.Split(","))
  173.                 Next
  174.             Catch ex As Exception
  175.                 MessageBox.Show("Error occured while reading " & filename & " file." & vbNewLine + ex.ToString())
  176.             End Try
  177.             populatearray()
  178.             StepTimer.Start()
  179.         Else
  180.             'At this point, the program effectively locks down until you close and restart.
  181.             MsgBox("File " & filename & " not found! Close This software and ensure the file is added to your c:\csv\ folder.")
  182.         End If
  183.     End Sub
  184.     'Loads data from datagrid row into
  185.     'THIS FUNCTION APPEARS TO BE WORKING FINE
  186.     Private Sub populatearray()
  187.         'Ensures whole line is selected to populate
  188.         Dim selectRow As DataGridViewRow = StepGrid.CurrentRow
  189.         ' Move the Glyph arrow to the first cell
  190.         StepGrid.CurrentCell = selectRow.Cells(0)
  191.         ' Select the row
  192.         selectRow.Selected = True
  193.  
  194.         'populate an csv string with selected row contents
  195.         Dim firstvalue As Boolean = True
  196.         Dim tempstring As String = ""
  197.         For Each cell In StepGrid.SelectedCells
  198.             If Not firstvalue Then
  199.                 tempstring &= ","
  200.             End If
  201.             Try
  202.                 tempstring &= cell.Value.ToString()
  203.             Catch ex As Exception
  204.                 tempstring &= ""
  205.             End Try
  206.  
  207.             firstvalue = False
  208.         Next
  209.         'Split string based on commas
  210.         Dim words As String() = tempstring.Split(New Char() {","c})
  211.         'load row values into current step variables
  212.         StepCommand = words(0)
  213.         StepPrompt = words(1)
  214.         StepArg1 = words(2)
  215.         StepArg2 = words(3)
  216.         StepArg3 = words(4)
  217.         StepArg4 = words(5)
  218.  
  219.         StepWait = words(6)
  220.  
  221.         If StepWait = "" Then
  222.             StepWait = "1000"
  223.         End If
  224.  
  225.         StepOnPass = words(7)
  226.         StepOnFail = words(8)
  227.         'populatesteplabels()
  228.     End Sub
  229.     'This function allows for key presses to trigger special events.
  230.     'THIS FUNCTION WORKS, BUT IS SUBJECT TO ADDITIONAL KEY PRESS HANDLERS
  231.     Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
  232.         Select Case keyData
  233.             Case Keys.F12
  234.  
  235.             Case Keys.F11
  236.             Case Keys.F2
  237.                 'F10 Will show / hide Datagrid / test block tools
  238.                 If datagridPanel.Visible Then
  239.                     datagridPanel.Visible = False
  240.  
  241.  
  242.                 Else
  243.                     datagridPanel.Visible = True
  244.  
  245.                 End If
  246.  
  247.  
  248.             Case Keys.F1
  249.                 'F10 Will show / hide Datagrid / test block tools
  250.                 If Panel2.Visible Then
  251.  
  252.                     Panel2.Visible = False
  253.                     Panel3.Visible = False
  254.  
  255.                 Else
  256.  
  257.                     Panel2.Visible = True
  258.                     Panel3.Visible = True
  259.                 End If
  260.             Case Else
  261.                 Return MyBase.ProcessCmdKey(msg, keyData)
  262.         End Select
  263.         Return True
  264.     End Function
  265.     Private Sub ComboBox1_SelectedIndexChanged() Handles ComboBox1.SelectedIndexChanged
  266.         For i As Integer = 1 To 32
  267.             CType(Panel2.Controls("channelbias" & i), Label).BackColor = Color.Transparent
  268.         Next
  269.         If ComboBox1.Text = "All" Then
  270.             'populate with selected byte value for all
  271.             For i As Integer = 1 To 32
  272.                 CType(Panel2.Controls("channelbias" & i), Label).BackColor = Color.Yellow
  273.             Next
  274.         Else
  275.             CType(Panel2.Controls("channelbias" & ComboBox1.Text), Label).BackColor = Color.Yellow
  276.         End If
  277.     End Sub
  278.     Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
  279.         Dim data(34) As Byte
  280.         data(0) = 1
  281.         data(1) = 1
  282.  
  283.         'populate with current values
  284.         For i As Integer = 0 To 31
  285.             data(2 + i) = getbiasfromletter(CType(Panel2.Controls("channelbias" & i + 1), Label).Text)
  286.         Next
  287.  
  288.         'get byte value of bias selection
  289.         Dim setvalue As Byte
  290.         Select Case ComboBox2.Text
  291.             Case "No"
  292.                 setvalue = CByte(0)
  293.             Case "G"
  294.                 setvalue = CByte(1)
  295.             Case "N"
  296.                 setvalue = CByte(2)
  297.             Case "F"
  298.                 setvalue = CByte(6)
  299.             Case "P"
  300.                 setvalue = CByte(7)
  301.             Case "H"
  302.                 setvalue = CByte(11)
  303.         End Select
  304.  
  305.         If ComboBox1.Text = "All" Then
  306.             'populate with selected byte value for all
  307.             For i As Integer = 0 To 31
  308.                 data(2 + i) = setvalue
  309.             Next
  310.         Else
  311.             data(1 + CInt(ComboBox1.Text)) = setvalue
  312.         End If
  313.  
  314.         stream.Write(data, 0, data.Length)
  315.         'resetminmax()
  316.     End Sub
  317.     Private Function getbiasfromletter(ByVal txt As String) As Byte
  318.         Dim setvalue As Byte
  319.         Select Case txt
  320.             Case "No"
  321.                 setvalue = CByte(0)
  322.             Case "G"
  323.                 setvalue = CByte(1)
  324.             Case "N"
  325.                 setvalue = CByte(2)
  326.             Case "F"
  327.                 setvalue = CByte(6)
  328.             Case "P"
  329.                 setvalue = CByte(7)
  330.             Case "H"
  331.                 setvalue = CByte(11)
  332.         End Select
  333.  
  334.         Return setvalue
  335.     End Function
  336. End Class
Add Comment
Please, Sign In to add comment