Controlling the program using the Serial to-USB switch
We want to control the program (recorded in VB) by pressing the switch connected to the serial port of the USB adapter. When pressed, a specific function in the program will be activated. To achieve this, we need to poll the corresponding COM port, continuously in the comm_event of the serial port, and call the specified function. We tried to connect a pushbutton switch between the transmit and ground pins of the adapter to change the state of the input buffer. It works sometimes, but misses most of the time. Therefore, it seems to us that we need to create a long train of pulses in order to maintain the state of the input buffer long enough to detect any changes. Is there any gadget for something like this? What simple electronics can solve this problem.
Any help would be much appreciated.
source to share
Here is the answer by Robert Greer (author of the Visual Basic Programming Guide for Serial Communications, 4th edition ISBN 1-890422-28-2 ) I am grateful for his help and therefore share this with everyone concerned.
Connect one side of the switch to the DTR output (pin 4 of the connector) and the other side of the switch to the DCD input (pin 1)
Remove the Comm control from your form toolbar and add one label. In code:
Private Sub Form_Load()
On Error Resume Next
With MSComm1
.CommPort = 3 'use the actual port number that is created when you install your USB adapter
.PortOpen = True
.DTREnable = True
If .PortOpen = False Then MsgBox "Unable to open serial port."
End With
End Sub
Private Sub MSComm1_OnComm()
With MSComm1
If .CDHolding = True Then
Label1.Caption = "Switch On"
Label1.BackColor = vbGreen
Else
Label1.Caption = "Switch Off"
Label1.BackColor = vbRed
End If
End With
End Sub
source to share
If I understand correctly, the computer does not have a serial port, so you plug the USB adapter into a USB port on the PC and then try to read some of the conditions you create on the RS-232 side of the adapter? I would look at DTR / DSR flow control pins, etc. Do you have an external power supply? RS232 usually expects +/- 12v. Depending on what you are trying to achieve and your circumstances, you might also want to look at adding a hardware serial port on a PC or using a USB hardware interface like this one: http://www.maplin.co.uk/usb-experiment -interface-board-42857
source to share
I haven't tested this, but some options ...
Private WithEvents com As New IO.Ports.SerialPort
Private Sub com_PinChanged(sender As Object, e As System.IO.Ports.SerialPinChangedEventArgs) Handles com.PinChanged
Dim pin As IO.Ports.SerialPinChange = e.EventType
If pin.DsrChanged Then
MsgBox("DSR changed")
End If
End Sub
... must work. Change pin 6 on the DB9 connector. You can also access other contacts using this method.
source to share