Virtual Serial Port Problem

dimos8enis

Member
Joined
Feb 25, 2009
Messages
5
Programming Experience
1-3
Hello, I am new here, so I apologize if this is not the right place to open this thread.
I wrote a program that uses the following class from that site: VB.NET Tutorials - Serial Port Communication In VB.Net | DreamInCode.net

The problem is that I cannot open a virtual serial port, and the second is that I want to make the communication asynchronous. Are there any ideas?

This is the subroutine which reads from the port:
VB.NET:
#Region "comPort_DataReceived"
''' <summary>
''' method that will be called when theres data waiting in the buffer
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub comPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
    'determine the mode the user selected (binary/string)
    Select Case CurrentTransmissionType
        Case TransmissionType.Text
            'user chose string
            'read data waiting in the buffer
            Dim msg As String = comPort.ReadExisting()
            'display the data to the user
            _type = MessageType.Incoming
            _msg = msg
            DisplayData(MessageType.Incoming, msg + "" + Environment.NewLine + "")
            Exit Select
        Case TransmissionType.Hex
            'user chose binary
            'retrieve number of bytes in the buffer
            Dim bytes As Integer = comPort.BytesToRead
            'create a byte array to hold the awaiting data
            Dim comBuffer As Byte() = New Byte(bytes - 1) {}
            'read the data and store it
            comPort.Read(comBuffer, 0, bytes)
            'display the data to the user
            _type = MessageType.Incoming
            _msg = ByteToHex(comBuffer) + "" + Environment.NewLine + ""
            DisplayData(MessageType.Incoming, ByteToHex(comBuffer) + "" + Environment.NewLine + "")
            Exit Select
        Case Else
            'read data waiting in the buffer
            Dim str As String = comPort.ReadExisting()
            'display the data to the user
            _type = MessageType.Incoming
            _msg = str + "" + Environment.NewLine + ""
            DisplayData(MessageType.Incoming, str + "" + Environment.NewLine + "")
            Exit Select
    End Select
End Sub
#End Region

and this is the subroutine that opens the port:
VB.NET:
#Region "OpenPort"
Public Function OpenPort() As Boolean
    Try
        'first check if the port is already open
        'if its open then close it
        If comPort.IsOpen = True Then
            comPort.Close()
        End If

        'set the properties of our SerialPort Object
        comPort.BaudRate = Integer.Parse(_baudRate)
        'BaudRate
        comPort.DataBits = Integer.Parse(_dataBits)
        'DataBits
        comPort.StopBits = DirectCast([Enum].Parse(GetType(StopBits), _stopBits), StopBits)
        'StopBits
        comPort.Parity = DirectCast([Enum].Parse(GetType(Parity), _parity), Parity)
        'Parity
        comPort.PortName = _portName
        'PortName
        'now open the port
        comPort.Open()
        'display message
        _type = MessageType.Normal
        _msg = "Port opened at " + DateTime.Now + "" + Environment.NewLine + ""
        DisplayData(_type, _msg)
        'return true
        Return True
    Catch ex As Exception
        DisplayData(MessageType.[Error], ex.Message)
        Return False
    End Try
End Function
#End Region
 
A little more information would be great.
F.e. what _portName contains, what you mean with virtual and what's the exact issue (it crashes while the Open() Sub, it does not read anything...)

Also what you mean with asynchronous. The SerialPort class calls down into the Windows API which is already working asynchronous.

Bobby

P.s.: Another topic on this (maybe useful) http://www.vbdotnetforums.com/vb-net-general-discussion/32464-com-port-access.html
http://www.vbdotnetforums.com/vb-net-general-discussion/32236-detect-serial-ports.html
http://www.vbdotnetforums.com/vb-net-general-discussion/31972-vb2005-serial-port.html
 
_portName contains the name of the port for example COM1 etc.
about virtual port:
I have a RS232 to Ethernet converter (Adam 4571) whose serial port I give an IP adress and map it with tools provided with the device. So, finally I have a virtual serial port which ends to a real one through ethernet. I just cannot open that, despite the fact that opening this virtual port using the Hyper Terminal is possible.

about sync/async:
I've read somewhere that using Thread for the incoming data is async communication and not that I'm using now. I'm not sure about that though...
 
As far as I know is DataReceived already ASync.

What does the exception say if you try to open the port? Does it show up in the Collection My.Computer.Ports or SerialPort.PortNames?
 
It doesn't throw any exception, it is just not executing the comport.Open() command and all the commands below(!!!). This is for sure, I checked it, all the code before comport.Open() is executed but not the code after it.
Although the comport.IsOpen property seems to have "True" value, I cannot read from the port. It is more than weird...
I have to mention that it works just fine with normal physical serial ports.
 
Step through the code line by line and watch what happens...

I've worked with a similar device (a NPort Server with 6 Serial Ports, with the same connection type) and the only problem I had was that DiscardInBuffer/DiscardOutBuffer was taking extreme long (250ms on a Windows Server 2003 machine).
 
Yes but that was because of the Windows API and you couldn't change it right?
I've tried debugging line by line, what is happening is that I mentioned previously. It says it cannot open the port.
 
Yes but that was because of the Windows API and you couldn't change it right?
Right, I haven't found away around it either...

It doesn't throw any exception, it is just not executing the comport.Open() command and all the commands below(!!!)
It says it cannot open the port.
There's a big difference here. So it does occur an exception which tells you that it failed? What does the message exactly say?
 
Back
Top