Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

May i ask how to use the same serial port in different forms can help me take look

$
0
0

in current situation my form 1 textbox can receive the data after scan but form 2 can't not below is my coding can someone help me take a look

form 1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

MySerial()

End Sub
Public Sub MySerial()

        SerialPort.PortName = "COM1"
        SerialPort.BaudRate = CInt("9600")
        SerialPort.Parity = Parity.None
        SerialPort.StopBits = StopBits.One
        SerialPort.Handshake = Handshake.None
        SerialPort.Open()
        SerialPort.ReadTimeout = 200

    End Sub

 Private Sub SerialPort_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived

        dataIn = SerialPort.ReadExisting
        SetText(dataIn)

    End Sub
    Delegate Sub SetTextCallback(ByVal text As String)

    Private Sub SetText(ByVal text As String)
        If Me.TextBox1.InvokeRequired Then
            Dim d As SetTextCallback = New SetTextCallback(AddressOf SetText)
            Me.Invoke(d, New Object() {text})
        Else
            Me.TextBox1.Text = text.ToString
        End If
    End Sub

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Form2.SerialPort1 = Me.SerialPort
        Me.SerialPort = Form2.SerialPort1
        Form2.ShowDialog()

        Me.Hide()
    End Sub

form 2

  Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.TextBox1.Text = Form1.TextBox1.Text
        Me.SerialPort1 = Form1.SerialPort
        Form1.SerialPort = Me.SerialPort1

    End Sub

 Private Sub SerialPort_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        dataIn = SerialPort1.ReadExisting
        SetText(dataIn)

    End Sub
    Delegate Sub SetTextCallback(ByVal text As String)

    Private Sub SetText(ByVal text As String)
        If Me.TextBox1.InvokeRequired Then
            Dim d As SetTextCallback = New SetTextCallback(AddressOf SetText)
            Me.Invoke(d, New Object() {text})
        Else
            Me.TextBox1.Text = text.ToString
        End If
    End Sub



Viewing all articles
Browse latest Browse all 2535

Trending Articles



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