Results 1 to 9 of 9

Thread: Virtual Serial Port Problem

  1. #1
    dimos8enis is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Feb 2009
    Posts
    5
    Reputation
    0

    Virtual Serial Port Problem

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

  2. #2
    Robert_Zenz's Avatar
    Robert_Zenz is offline VB.NET Forum Idol
    .NET Framework
    .NET 2.0
    Join Date
    Jun 2008
    Location
    Vienna, Austria
    Posts
    503
    Reputation
    340
    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) COM port Access
    detect serial ports
    VB2005 Serial Port
    Don't give TypeCasting Errors a chance, turn ON Option Strict!

  3. #3
    dimos8enis is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Feb 2009
    Posts
    5
    Reputation
    0
    _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...

  4. #4
    Robert_Zenz's Avatar
    Robert_Zenz is offline VB.NET Forum Idol
    .NET Framework
    .NET 2.0
    Join Date
    Jun 2008
    Location
    Vienna, Austria
    Posts
    503
    Reputation
    340
    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?
    Don't give TypeCasting Errors a chance, turn ON Option Strict!

  5. #5
    dimos8enis is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Feb 2009
    Posts
    5
    Reputation
    0
    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.

  6. #6
    Robert_Zenz's Avatar
    Robert_Zenz is offline VB.NET Forum Idol
    .NET Framework
    .NET 2.0
    Join Date
    Jun 2008
    Location
    Vienna, Austria
    Posts
    503
    Reputation
    340
    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).
    Don't give TypeCasting Errors a chance, turn ON Option Strict!

  7. #7
    dimos8enis is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Feb 2009
    Posts
    5
    Reputation
    0
    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.

  8. #8
    Robert_Zenz's Avatar
    Robert_Zenz is offline VB.NET Forum Idol
    .NET Framework
    .NET 2.0
    Join Date
    Jun 2008
    Location
    Vienna, Austria
    Posts
    503
    Reputation
    340
    Quote Originally Posted by dimos8enis View Post
    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...

    Quote Originally Posted by dimos8enis View Post
    It doesn't throw any exception, it is just not executing the comport.Open() command and all the commands below(!!!)
    Quote Originally Posted by dimos8enis View Post
    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?
    Don't give TypeCasting Errors a chance, turn ON Option Strict!

  9. #9
    dimos8enis is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Feb 2009
    Posts
    5
    Reputation
    0
    The message shown is from my code and it is in case the port cannot be opened. It doesn't throw any other exception!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking