开发者

Opening Multiple COM Ports and Send/Receive Data

I am trying to set up a program that allows you to open multiple COM ports and then select which COM port you send data down. The computer i am using has 18 COM ports. Also on the first code it shows the ports are open they just wont receive anything.

Dim Ports As New List(Of IO.Ports.SerialPort)

Ports.Add(New IO.Ports.SerialPort)

Ports(0).PortName = "COM1"
Ports(0).BaudRate = 9600
Ports(0).DataBits = 8
Ports(0).StopBits = IO.Ports.StopBits.One
Ports(0).Open()


'Data Received Interrupt to read and display anything coming in the RXD pin.
Private Sub SerialPort1_Datareceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    CheckForIllegalCrossThreadCalls = False
开发者_高级运维    Dim data As String

    'rxListBox.Items.Add(data)
    data = Ports(0).ReadExisting.ToString()

End Sub

Thats what i am trying to do but i cannot get it to receive data. If i change the code to the code listed below it works but i would like a way to switch between ports with an array.

'Setup serial Port parameters
SerialPort1.PortName = "COM1"
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.Open()

'Data Received Interrupt to read and display anything coming in the RXD pin.
Private Sub SerialPort1_Datareceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    CheckForIllegalCrossThreadCalls = False
    Dim data As String

    data = SerialPort1.ReadExisting.ToString()
    rxListBox.Items.Add(data)
End Sub

Thanks


You have to add the handler to each port you create

For each port in Ports
  AddHandler port.DataReceived, AddressOf SerialPort1_Datareceived
Next

Run that after all ports are created. You will have to check which port data came from in the SerialPort1_Datareceived event as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜