Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27044

VS 2010 Dealing with CommPort device identy. Help!

$
0
0
Hi,

The code below should do the following:
Finds all available CommPorts.
Checks if they are open.
Opens the free Commport one by one and interrogates them to get the idendity of the DMM.
Finds the COmmport of the DMM.
Assigns a CommPort to DMM instrument.

The problem I have is that when the code askeds the DMM for the ID string, and there is no response because it is not connected to that port,
i get the standard Timeout message.

I need a way to continue the loop until the code gets to the DMM Commport.

Can you help please. Thank you :)

Regards

Francesco

HTML Code:



Option Strict On
Imports System
Imports System.IO.Ports

Public Class Main

    Dim freeports(9) As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       
        '================== Get Com Ports ========================================================== 
        Dim i As Integer
        i = 1

        '---------- Detect available ports and store them in the freeorts array.-------------

        For Each Port As String In IO.Ports.SerialPort.GetPortNames
            Dim myPort As New IO.Ports.SerialPort(Port)

            If myPort.IsOpen = False Then      ' check if Serial port is free to use

                freeports(i) = (Port)              ' Assign Port string (Com1 Com2.... Com10) to array
                ComboBox1.Items.Add(freeports(i))
                i = i + 1
            End If
        Next

        ComboBox1.SelectedIndex = 0  ' set to show the first item (for testing only)

        ' ------------- check if there are at least 2 com port free to use.-------------------
        If i < 2 Then
            MsgBox("CANNOT RUN TEST! System needs at least 2 free ComPorts.")
        End If


        '=================== Establish Communication and assign Com Ports ==================================

        Dim DMMstr As String = "THURLBY-THANDAR,1906,0,1.19"  ' Idendity of string transmitted by  Instruments
     
        Dim IDstr As String
        For count As Integer = 1 To i
            DMM_Port.PortName = freeports(count)

            DMM_Port.Open()
            DMM_Port.WriteLine(CStr(&H2 + &HA))  ' Set Address Mode (SAM)
            DMM_Port.WriteLine("*IDN?" + Chr(&HA)) 'Ask for identity string
            IDstr = DMM_Port.ReadLine()
         
            DMM_Port.Close()

            If Mid(IDstr, 1, (IDstr.Length()) - 1) = DMMstr Then  ' remove newline char
                TextBox1.Text = DMM_Port.PortName  ' Show me the port where the instrument is connected
                TextBox3.Text = IDstr                    ' Show the ID string from the instrument
                Exit For
            End If

        Next

     
    End Sub


Viewing all articles
Browse latest Browse all 27044

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>