Turn off and off RTS

I'm developing a simple program that uses the serial port to active a PCB that lights an LED.

I'm using the new Visual Studio Express VB 2005.

The PCB (printed circuit board) uses the toggling of RTS/GND on the serial port.

Until I understand how this works, I'm just trying to test this with a simple click of the mouse. Click inside the main form and the LED turns on and off.

Once I understand how it all works, I'll use the code to turn on and off RTS when a question is answered a particular way by the user.

For my testing, here is my code:

Private Sub Main_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseClick(My.Resources.sounderclick, AudioPlayMode.Background)

My.Computer.Ports.OpenSerialPort("COM1").RTSEnable = True

My.Computer.Ports.OpenSerialPort("COM1").RTSEnable = False

End Sub

Unfortunately, this isn't working. I thought it would be this simple. Perhaps it is and I'm missing something. COM1 is unused, so I should be ok...and I know it works as I've used a mouse and modem on that same port.

But, when I click in the form, during debug, I get the following:

UnauthorizedAccessException
Access to the port 'COM1' is denied.

If I enumerate the ports, I get COM1 and COM3. Using both COM1 and COM3 result in this same problem.

I'm new to VB programming so this error message is confusing me. When I read the help information it says that the operating system (in my case, XP Profesional with SP2) is denying access to the port.

I'm not sure how to fix this or where to look for answers. I looked several hours last night on MSDN and about everything I was finding was referring to files and not a serial port.

Please provide any assistance or guidance on where I should next proceed.

Thanks

Ted



Answer this question

Turn off and off RTS

  • NotMyName000

    AH! Hmmm, yes that eliminated my error. :) Thanks!

    Ted


  • Yousuf Khan

    This is just a wild guess, but if you step through your application, will it fail on the first or the second row in your MouseDown handler

    If it is the second row, I believe that it is because you are opening the same port twice, and you can only have it open once at a time.

    Try something like this instead:

    Using port As System.IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort("COM1")
    port.RtsEnable =
    True
    port.RtsEnable = False
    End Using

    Best regards,
    Johan Stenberg



  • hollander67

    Ooops, sorry, code snippet is incorrect:

    I am no longer using the AudoPlayMode. This is the correct code:

    Private Sub Main_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseClick

    My.Computer.Ports.OpenSerialPort("COM1").RTSEnable = True

    My.Computer.Ports.OpenSerialPort("COM1").RTSEnable = False

    End Sub


  • Turn off and off RTS