Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,228 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,293 people online right now. Registration is fast and FREE... Join Now!




How to open and read from multiple serial ports at the same time

 
Reply to this topicStart new topic

How to open and read from multiple serial ports at the same time

Noora
12 Oct, 2008 - 07:21 AM
Post #1

New D.I.C Head
*

Joined: 12 Oct, 2008
Posts: 8

I’m a beginner in vb.net and I have no knowledge in it, but I have to use it to do my project. I found a tutorial about Serial Port Communication in VB.Net develped by Psychocoder it was very helpful.

I tested the code, it works great to open and read from one comport, but unfortunately when I tried to modify the code to open all the available comports in the system by adding a loop to get all available ports and read from them simultaneously it doesn’t work well!! In The open port method, it opens the first available port and when it gets the second it closes the opened one and then opens the next port and so on.

I modify this sub routine in the frmMain class:
CODE
Private Sub cmdOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOpen.Click
        comm.Parity = cboParity.Text
        comm.StopBits = cboStop.Text
        comm.DataBits = cboData.Text
        comm.BaudRate = cboBaud.Text
        comm.DisplayWindow = rtbDisplay
  '''''''''''''''''''''''''''''''''''''''''''''''''
''These lines I add in the code:


        For Each str As String In SerialPort.GetPortNames()
            comm.PortName = str
            comm.OpenPort()
        Next str
  ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        cmdOpen.Enabled = False
        cmdClose.Enabled = True
        cmdSend.Enabled = True
    End Sub


I tried to check where is the fault, so I tried to remove the command that close the opened port but still it doesn’t work, another issue I faced, only one port is opened.

In the CommManager class:

CODE
Public Function OpenPort() As Boolean
        Try
'first check if the port is already open
            'if its open then close it

''I removed these lines that close the opened port before openning the next one :

           If comPort.IsOpen = True Then
                comPort.Close()
            End If


How can I open all available ports at the same time and read from them as well???? Do I have to change the whole code?? Or maybe use another library by importing it?? Or is it possible to use the same program and modify it??

Thank you and waiting for a help soon
User is offlineProfile CardPM
+Quote Post

Noora
RE: How To Open And Read From Multiple Serial Ports At The Same Time
18 Oct, 2008 - 11:53 AM
Post #2

New D.I.C Head
*

Joined: 12 Oct, 2008
Posts: 8

Help!! Anyone!!!!! blink.gif
User is offlineProfile CardPM
+Quote Post

dbasnett
RE: How To Open And Read From Multiple Serial Ports At The Same Time
18 Oct, 2008 - 03:31 PM
Post #3

D.I.C Head
**

Joined: 1 Oct, 2008
Posts: 161



Thanked: 5 times
My Contributions
CODE
        'open all the ports
        Dim comm As New List(Of IO.Ports.SerialPort) 'list of comm ports
        For Each PNstr As String In IO.Ports.SerialPort.GetPortNames() 'get a list of Com Port Names
            comm.Add(New IO.Ports.SerialPort) 'insert new port into list
            comm(comm.Count - 1).PortName = PNstr 'set the name
            'set other items code here
            Try
                comm(comm.Count - 1).Open() 'open it
                If Not (comm(comm.Count - 1).IsOpen) Then 'did it really open
                    comm.RemoveAt(comm.Count - 1) 'no, remove from list
                Else
                    Debug.WriteLine(comm(comm.Count - 1).PortName & " opened")
                End If
            Catch
            End Try
        Next PNstr
        For z As Integer = 0 To comm.Count - 1 'close em
            Try
                Debug.WriteLine(comm(z).PortName & " closed")
                If comm(z).IsOpen Then comm(z).Close() 'close it
            Catch
            End Try
        Next


This post has been edited by dbasnett: 18 Oct, 2008 - 03:45 PM
User is offlineProfile CardPM
+Quote Post

Noora
RE: How To Open And Read From Multiple Serial Ports At The Same Time
20 Oct, 2008 - 02:02 AM
Post #4

New D.I.C Head
*

Joined: 12 Oct, 2008
Posts: 8

Thanks dbasnett the code was helpful smile.gif

But I didn't understand why do you write the lines that close the ports after opening them?! I need to keep all the ports open to read from all of them all the time, when I remove these lines all the ports open successfully but still can't read or receive data from any one of them!! blink.gif can you help me in that also

CODE
For z As Integer = 0 To comm.Count - 1 'close em
            Try
                Debug.WriteLine(comm(z).PortName & " closed")
                If comm(z).IsOpen Then comm(z).Close() 'close it
            Catch
            End Try


Please I need your help, I work in a project deals with RFID Readers and tags, I just recently learn how to program using vb.net to do this project
User is offlineProfile CardPM
+Quote Post

dbasnett
RE: How To Open And Read From Multiple Serial Ports At The Same Time
20 Oct, 2008 - 02:45 AM
Post #5

D.I.C Head
**

Joined: 1 Oct, 2008
Posts: 161



Thanked: 5 times
My Contributions
i closed them as an example, and because they should be closed when finished using them.

how often do you have to read from the RFID readers?

let me be frank. there are a lot of experienced VB programmers that have difficulties reading / writing to one serial port properly.

here SerialPort

is an example of a project I did that reads one port. it should not be used in a commercial application!

User is offlineProfile CardPM
+Quote Post

Noora
RE: How To Open And Read From Multiple Serial Ports At The Same Time
20 Oct, 2008 - 12:08 PM
Post #6

New D.I.C Head
*

Joined: 12 Oct, 2008
Posts: 8

When a tag passes near to one of the RFID readers that connected to computer serial ports (including the virtual comports), I have to receive the Tag ID and then store it in an access database , that's why I have to keep all the ports open all the time

please I need your help also in reading from all the serial ports
User is offlineProfile CardPM
+Quote Post

magicmonkey
RE: How To Open And Read From Multiple Serial Ports At The Same Time
20 Oct, 2008 - 12:59 PM
Post #7

D.I.C Regular
***

Joined: 12 Sep, 2008
Posts: 413



Thanked: 68 times
My Contributions
Maybe this can get you started??? It is based on dbnasnet post but I added an event handler for datareceived and seperated the open and close routines.

vb

Private MonitorSP As New List(Of IO.Ports.SerialPort)

Private Sub OpenSP()
CloseSP()

For Each PortName As String In IO.Ports.SerialPort.GetPortNames()
Try
Dim newSP As New IO.Ports.SerialPort(PortName)
newSP.Open()
If newSP.IsOpen Then
AddHandler newSP.DataReceived, AddressOf MonitorSP_DataReceived
MonitorSP.Add(newSP)
End If
Catch ex As Exception
'Failed
End Try
Next
End Sub

Private Sub CloseSP()
For Each selSP As IO.Ports.SerialPort In MonitorSP
selSP.Close()
Next
MonitorSP.Clear()
End Sub

Private Sub MonitorSP_DataReceived(ByVal sender As Object, ByVal e As IO.Ports.SerialDataReceivedEventArgs)
Dim SP As IO.Ports.SerialPort = DirectCast(sender, IO.Ports.SerialPort)
Dim Data As String = SP.ReadLine
'Remember this is in a background thread so you can not update the UI from here without using Me.Invoke
End Sub

User is offlineProfile CardPM
+Quote Post

dbasnett
RE: How To Open And Read From Multiple Serial Ports At The Same Time
20 Oct, 2008 - 02:47 PM
Post #8

D.I.C Head
**

Joined: 1 Oct, 2008
Posts: 161



Thanked: 5 times
My Contributions
what is the format of the data?
User is offlineProfile CardPM
+Quote Post

Noora
RE: How To Open And Read From Multiple Serial Ports At The Same Time
21 Oct, 2008 - 03:27 AM
Post #9

New D.I.C Head
*

Joined: 12 Oct, 2008
Posts: 8

The data format is ASCII but i have to convert it to text format
User is offlineProfile CardPM
+Quote Post

dbasnett
RE: How To Open And Read From Multiple Serial Ports At The Same Time
21 Oct, 2008 - 05:04 AM
Post #10

D.I.C Head
**

Joined: 1 Oct, 2008
Posts: 161



Thanked: 5 times
My Contributions
i meant the protocol like this (BTW - byte = char)
<byte 1><byte 2><byte 3><byte 4>....<byte x>

example:
byte 1 =
byte 2 -3 =
byte 4 =
byte 5-8 =
User is offlineProfile CardPM
+Quote Post

Noora
RE: How To Open And Read From Multiple Serial Ports At The Same Time
22 Oct, 2008 - 07:37 AM
Post #11

New D.I.C Head
*

Joined: 12 Oct, 2008
Posts: 8

I don't know the format for the received data, I have to ask the company that I bought the RFID readers from

Thanks dbasnett and magicmonkey your both codes help me alot in my project I'm now able to open and read from multiple serial ports at the same time biggrin.gif biggrin.gif biggrin.gif icon_up.gif biggrin.gif biggrin.gif


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 03:10AM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month