Serial port selection problem

I'm not exactly sure how to phrase this question, but let me describe the problem....

I have a very simple little kit box which when connected to a serial port and RTS is turned on, it lights up an LED.

 I have a ComboBox which when selected, goes out and enumerates the available serial ports:

Module One

Public cSelect = My.Settings.serSelect.ToString
Public cPort As New IO.Ports.SerialPort(cSelect.ToString)
For Each portName As String In My.Computer.Ports.SerialPortNames
Form1.ComboBox1.Items.Add(portName)
Next

End Module

Public Class Form1

Try
cSelect = ComboBox1.SelectedText.ToString()
Catch ex As Exception
MsgBox(ex.Message)
End Try

cPort.Open()
cPort.RtsEnable = True
Thread.Sleep(100)
cPort.RtsEnable = False
cPort.Close()

End Class

 

This isn't my code exactly, it's a summary.  What I want to do is allow the user to select the COM port from the list.  Test it by pressing a button to light up the LED a few times, if there's nothing connected to the port which was selected, throw an error.

Here's my problem.  I'm trying to test to see if the ports are VALID.  No matter WHICH port I select, turning on and off RTS always works on any serial port selected in the combobox1 no matter which port my serial device is connected (a simple LED light that turns on when RTS is on and off when RTS is off).

 

Make sense  

 

Summary:

Connect my LED serial device to COM1

Select ComboBox1, it enumerates:

COM1, COM3

select COM1 and test turning on RTS and turning it off.  Works ok

If I select COM3 the same test works.  However, I need to figure a way out to test this condition.  First, why would RTS on and RTS off work on both when a device is physically connected to a specific serial port

Actually, I'm not sure that I have one problem or two.  I've been working on this for the past 4 days and it's driving me nuts.  ;)

Thanks

Ted



Answer this question

Serial port selection problem

  • goh6613

    Each COM port has its own modem control signals, so RTS on COM1 will not affect COM3 and visa versa. If you only want to test if the right COM port has been selected, you can alternately turn RTS on and off on the selected port. When the box is connected to the right port, the LED will flash.

    It still seems to me that your selection is not working. When the user selects an item from ComboBox1, you just set the variable cSelect, but in the code you have shown you do not use this to open the right port. You miss a cPort.PortName = cSelect statement.

    I also do not understand why you are using ToString commands. When you load ComboBox1 by means of:

    For Each portName As String In My.Computer.Ports.SerialPortNames
    Form1.ComboBox1.Items.Add(portName)
    Next

    then ComboBox1 will contain a list of text strings with the name of all available serial ports, so when the user selects a port, you can open that port by means of:

    cPort.PortName = ComboBox1.Text

    Try

    cPort.Open()

    .....

    You do not need to convert anything.

    Remember to close any open port and give it time to do that before you open the new port! One instance of SerialPort can only handle one COM port at a time. Our small sample program shows how to change the COM port and how to sort the strings in the ComboBox in the right order.


  • Intelliorbit

    Sorry, but I am not quite sure what your problem is.

    On a standard 16C550 PC UART without auto flow control, DTR and RTS are just two general purpose binary outputs, which are intended for modem control, but may be used for any other purpose (even power supply), so it doesn't matter if a device is connected to the port! If you want to test whether a device is connected, you must use DSR or CTS, which are acknowledge signals from the connected device.

    Besides, are you sure that you actually select COM3 You have not shown any subroutine that handles ComboBox1.SelectedIndexChanged, so my guess is that COM1 is still selected. In the knowledgebase on our homepage you may find a small sample program for serial port communication including source code and documentation. This program also loads the available COM ports into a ComboBox, sort them and let the user select the port at any time. It also has "lamps", which shows the status of the modem control lines.

    The URL is: http://www.innovatic.dk/knowledg/SerialCOM/SerialCOM.htm

    By the way, it may be a good idea to put an "Application.DoEvents()" statement before any Sleep command, so that all applications may finish their jobs before the application goes to sleep (completely dead for the specified time).


  • ssfftt

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    Try

    cSelect = ComboBox1.SelectedItem.ToString()

    Catch ex As Exception

    MsgBox(ex.Message)

    End Try

    End Sub

    Also, when I call my module, I'm using the following:

    My.Application.DoEvents()

    This occurs in the main form prior to calling the module with the sleep commands.

    I'm not very familiar at all with serial programming. But, are you saying that if I turn on RTS it turns it on for ALL serial devices Or are you saying that whether or not I have anything collected RTS will always work

    If that's true, then you are correct, what I tried will not work. I just want the users to be able to test to see if they have selected the right serial port when connecting their little box.... i.e., after they connect the device, test the presence of the device on that com port by operating the LED.

    Ted


  • USJOHN

    Perhaps this isn't as hard as I'm making this out to be....

    So, this is more simple... the serial port can either be open or closed, correct

    What if when the combobox is being selected, after I grab the value of the selected item and use it in the following manner:

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    cSelect = ComboBox1.SelectedItem.ToString()

    Try
    cPort.Open()
    Catch ex As Exception
    MsgBox(ex.Message)
    End
    Try
    End Sub

    Note: for the value of cSelect and cPort in my serial module:

    Public cSelect = My.Settings.serSelect.ToString
    Public cPort As New IO.Ports.SerialPort(cSelect.ToString)

     

    Would this not be enough to test, in a simple manner, whether or not something is properly connected to the serial port

    Ted


  • John Oliver (UK)MSP, VSIP

    Ah, ok. Makes complete sense now. I over-complicated a bit I think.

    Thanks a bunch! Nice program you have there. Enjoyed going through it.

    Ted


  • Serial port selection problem