com port as external button (RTS CTS)

Antrac1t

Member
Joined
Apr 28, 2014
Messages
13
Programming Experience
Beginner
Hello,
i want to ask you if is there somebody who have experience with com port as external button. I took one switch and com port cable where i connected port 4 with port 8 and now i want check if user pushed button. I checked google and find that i need use RTSHolding and created this code which not working :/. Thank you

VB.NET:
Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports
Public Class Form1
    Dim WithEvents serialPort As New IO.Ports.SerialPort
    Public Delegate Sub myDelegate()
    Private Sub SerialPort1_PinChanged(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialPinChangedEventArgs) Handles SerialPort1.PinChanged
        If SerialPort1.CtsHolding Then
            If Me.InvokeRequired Then
                Me.Invoke(New MethodInvoker(AddressOf TakePicture))
                Exit Sub
            End If
            TakePicture()
        End If
    End Sub
    Private Sub btnSend_Click_1(sender As System.Object, e As System.EventArgs) Handles btnSend.Click
            serialPort.DtrEnable = True
        serialPort.RtsEnable = True
    End Sub


    Private Sub btnDisconnect_Click_1(sender As System.Object, e As System.EventArgs) Handles btnDisconnect.Click
        Try
            serialPort.Close()
            lblMessage.Text = serialPort.PortName & " disconnected."
            btnconnect.Enabled = True
            btnDisconnect.Enabled = False
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub


    Private Sub btnconnect_Click(sender As System.Object, e As System.EventArgs) Handles btnconnect.Click
        If serialPort.IsOpen Then
            serialPort.Close()
        End If
        Try
            With serialPort
                .PortName = cbbCOMPorts.Text
                .BaudRate = 2400
                .Parity = IO.Ports.Parity.None
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.One
            End With
            serialPort.Open()
            serialPort.RtsEnable = True
            SerialPort1.RtsEnable = True
            serialPort.DtrEnable = True
            SerialPort1.DtrEnable = True
            lblMessage.Text = cbbCOMPorts.Text & " connected."
            btnconnect.Enabled = False
            btnDisconnect.Enabled = True
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Public Sub TakePicture()
        Call MsgBox("test2")
    End Sub
End Class

GetFileAttachment


 

Latest posts

Back
Top