10 Replies - 22587 Views - Last Post: 22 October 2008 - 08:37 AM Rate Topic: -----

#1 Noora  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 12-October 08

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

Post icon  Posted 12 October 2008 - 08:21 AM

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:
 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:

 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

Is This A Good Question/Topic? 1

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

#2 Noora  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 12-October 08

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

Posted 18 October 2008 - 12:53 PM

Help!! Anyone!!!!! :blink:
Was This Post Helpful? 0
  • +
  • -

#3 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 117
  • View blog
  • Posts: 646
  • Joined: 01-October 08

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

Posted 18 October 2008 - 04:31 PM

		'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 October 2008 - 04:45 PM

Was This Post Helpful? 1
  • +
  • -

#4 Noora  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 12-October 08

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

Posted 20 October 2008 - 03:02 AM

Thanks dbasnett the code was helpful :)

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: can you help me in that also

 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
Was This Post Helpful? 0
  • +
  • -

#5 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 117
  • View blog
  • Posts: 646
  • Joined: 01-October 08

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

Posted 20 October 2008 - 03:45 AM

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!
Was This Post Helpful? 0
  • +
  • -

#6 Noora  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 12-October 08

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

Posted 20 October 2008 - 01:08 PM

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
Was This Post Helpful? 0
  • +
  • -

#7 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

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

Posted 20 October 2008 - 01:59 PM

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.

  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


Was This Post Helpful? 1
  • +
  • -

#8 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 117
  • View blog
  • Posts: 646
  • Joined: 01-October 08

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

Posted 20 October 2008 - 03:47 PM

what is the format of the data?
Was This Post Helpful? 0
  • +
  • -

#9 Noora  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 12-October 08

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

Posted 21 October 2008 - 04:27 AM

The data format is ASCII but i have to convert it to text format
Was This Post Helpful? 0
  • +
  • -

#10 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 117
  • View blog
  • Posts: 646
  • Joined: 01-October 08

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

Posted 21 October 2008 - 06:04 AM

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 =
Was This Post Helpful? 0
  • +
  • -

#11 Noora  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 12-October 08

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

Posted 22 October 2008 - 08:37 AM

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 :D :D :D :^: :D :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1