Virtual Serial Port Component

Mosteck

Active member
Joined
Nov 23, 2008
Messages
31
Location
Toronto
Programming Experience
3-5
Hi,

I've been struggling with writing my own Ethernet to serial program and just about to give up since I'm down to the wire on this project. I keep getting intermittent errors and I just can't get around them.

Are there any drivers/third-party components or external programs that anyone has used to be able to map two serial ports? I'm hoping for freeware (my budget is near empty), but at least something that I could trial and if works would be more then happy to purchase.

Thanks....
 
I just re-read my post and saw that it might not be clear.

I've got two external RS232 to Ethernet converters that I'm reading data from whenever they spit out information. I'm looking for something to emulate the serial port on my PC based on the IP address/Port of the external converter and then I'd just write some standard Serial communications code in my existing application.
 
Hello.

SerialPorts are normally added to the system by drivers.
If you want to emulate a Serial Port and patch information through it to a specified IP, you'll have to write a driver for this.

If you just want to communicate with something that has an IP, have a look at the TCPClient class.

Bobby
 
Maybe I'm doing soemthing wrong, but the code that I'm using is as follows:

VB.NET:
    ' Station #10 Torque RS232/Ethernet Logic

    Private Sub Stn10TorqueConnectCallback(ByVal ar As IAsyncResult)

        Try

            Stn10TorqueClientSocket.EndConnect(ar)
            Dim Stn10TorqueBytes(4095) As Byte
            Stn10TorqueClientSocket.BeginReceive(Stn10TorqueBytes, 0, Stn10TorqueBytes.Length, SocketFlags.None, AddressOf Stn10TorqueReceiveCallback, Stn10TorqueBytes)

        Catch sx As SocketException
            MsgBox("Stn10 Socket Exception: " & vbCrLf & sx.Message & vbCrLf & sx.SocketErrorCode.ToString)
        Catch ax As ArgumentException
            MsgBox("Stn10 Argument Exception: " & vbCrLf & ax.Message)
        Catch iox As InvalidOperationException
            MsgBox("Stn10 Invalid Operation Exception: " & vbCrLf & iox.Message)
        Catch ex As Exception
            MsgBox("Stn10 General Exception: " & vbCrLf & ex.Message)
        End Try


    End Sub

    Private Sub Stn10TorqueReceiveCallback(ByVal ar As IAsyncResult)

        Dim Stn10TorqueBytes() As Byte = CType(ar.AsyncState, Byte())
        Dim Stn10TorqueNumBytes As Int32

        Try
            Stn10TorqueNumBytes = Stn10TorqueClientSocket.EndReceive(ar)
        Catch ex As Exception
            Stn10EthernetDropCnt = Stn10EthernetDropCnt + 1

            Try
                Stn10TorqueClientSocket.Shutdown(SocketShutdown.Both)
                Stn10TorqueClientSocket.Close()
            Catch
            End Try

            Stn10TorqueEthernetStart()

            Dim InitializeText As String = Chr(32)
            Dim InitializeBytes() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(InitializeText)

            If Stn10TorqueClientSocket.Connected = True Then

                SyncLock Stn10TorqueClientSocket
                    Try
                        Stn10TorqueClientSocket.Send(InitializeBytes, InitializeBytes.Length, SocketFlags.None)
                    Catch
                    End Try
                End SyncLock

            Else

                Stn10TorqueEthernetStart()

            End If

        End Try

        If Stn10TorqueNumBytes = 0 Then

            Try
                Stn10TorqueClientSocket.Shutdown(SocketShutdown.Both)
                Stn10TorqueClientSocket.Close()
            Catch
            End Try

            Stn10TorqueEthernetStart()

        Else

            Dim Stn10TorqueRecv As String = Stn10TorqueASCII.GetString(Stn10TorqueBytes, 0, Stn10TorqueNumBytes)

            'Clear the buffer
            Array.Clear(Stn10TorqueBytes, 0, Stn10TorqueBytes.Length)

            'Store data
            Dim Stn10TorqueDlg As New Stn10TorqueOneStringDelegate(AddressOf Stn10TorqueStoreReceivedData)
            Dim Args() As Object = {Stn10TorqueRecv}
            Me.Invoke(Stn10TorqueDlg, Args)

            'Begin Receive again
            Stn10TorqueClientSocket.BeginReceive(Stn10TorqueBytes, 0, Stn10TorqueBytes.Length, SocketFlags.None, AddressOf Stn10TorqueReceiveCallback, Stn10TorqueBytes)

        End If
    End Sub

    Private Sub Stn10TorqueStoreReceivedData(ByVal Data As String)

        ' Coded String:
        ' 1086 Sp:01-01    X.XX Nm          XX
        '                  Torque           Angle

        Dim DecodeTorque As String
        Dim DecodeAngle As String
        Dim DecodeLenght As Integer

        DecodeLenght = Data.Length

        If Mid(Data, 1, 1) <> "#" And DecodeLenght >= 31 Then

            DecodeTorque = Mid(Data, (DecodeLenght - 30), 15)
            DecodeAngle = Mid(Data, (DecodeLenght - 15), 15)

            If Stn10ValveTorqueLH = True Then

                Stn10LHCoverScrewTextBox.Text = "Torque=" & RemoveSpaces(DecodeTorque) & " Angle=" & RemoveSpaces(DecodeAngle) & " deg"
                Stn10ValveTorqueLH = False

                Dim TorqueDecodeCnt As Integer
                Dim TorqueDecode As String = ""
                Dim AngleDecodeCnt As Integer
                Dim AngleDecode As String = ""

                For TorqueDecodeCnt = 1 To DecodeTorque.Length

                    If Mid(DecodeTorque, TorqueDecodeCnt, 1) >= Chr(48) And Mid(DecodeTorque, TorqueDecodeCnt, 1) <= Chr(57) Then
                        TorqueDecode = TorqueDecode + Mid(DecodeTorque, TorqueDecodeCnt, 1)
                    ElseIf Mid(DecodeTorque, TorqueDecodeCnt, 1) = Chr(46) Then
                        TorqueDecode = TorqueDecode + Mid(DecodeTorque, TorqueDecodeCnt, 1)
                    End If

                Next

                For AngleDecodeCnt = 1 To DecodeAngle.Length

                    If Mid(DecodeAngle, AngleDecodeCnt, 1) >= Chr(48) And Mid(DecodeAngle, AngleDecodeCnt, 1) <= Chr(57) Then
                        AngleDecode = AngleDecode + Mid(DecodeAngle, AngleDecodeCnt, 1)
                    ElseIf Mid(DecodeAngle, AngleDecodeCnt, 1) = Chr(46) Then
                        AngleDecode = AngleDecode + Mid(DecodeAngle, AngleDecodeCnt, 1)
                    End If

                Next

                ' Updates the data base with the information

                CmdProdDBStn10 = New OleDbCommand("UPDATE ProductionData SET Stn10LHCoverTorque = @Stn10LHCoverTorque, Stn10LHCoverAngle = @Stn10LHCoverAngle WHERE SerialNumber = @SerialNumber", ConProdDBStn10)
                CmdProdDBStn10.Parameters.AddWithValue("@Stn10LHCoverTorque", (TorqueDecode))
                CmdProdDBStn10.Parameters.AddWithValue("@Stn10LHCoverAngle", (AngleDecode))
                CmdProdDBStn10.Parameters.AddWithValue("@SerialNumber", Stn10CurrentWorkingSerialNumber)

                Try
                    ConProdDBStn10.Open()
                Catch
                Finally
                    ConProdDBStn10.Close()
                    ConProdDBStn10.Open()
                End Try

                Try
                    CmdProdDBStn10.ExecuteNonQuery()
                    PC2PLCReply("LHCSGood", "S10")
                Catch ex As Exception
                    MsgBox("Error updating the production database....", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error Updating")
                End Try

                ConProdDBStn10.Close()

            ElseIf Stn10ValveTorqueRH = True Then

                Stn10RHCoverScrewTextBox.Text = "Torque=" & RemoveSpaces(DecodeTorque) & " Angle=" & RemoveSpaces(DecodeAngle) & " deg"
                Stn10ValveTorqueRH = False

                Dim TorqueDecodeCnt As Integer
                Dim TorqueDecode As String = ""
                Dim AngleDecodeCnt As Integer
                Dim AngleDecode As String = ""

                For TorqueDecodeCnt = 1 To DecodeTorque.Length

                    If Mid(DecodeTorque, TorqueDecodeCnt, 1) >= Chr(48) And Mid(DecodeTorque, TorqueDecodeCnt, 1) <= Chr(57) Then
                        TorqueDecode = TorqueDecode + Mid(DecodeTorque, TorqueDecodeCnt, 1)
                    ElseIf Mid(DecodeTorque, TorqueDecodeCnt, 1) = Chr(46) Then
                        TorqueDecode = TorqueDecode + Mid(DecodeTorque, TorqueDecodeCnt, 1)
                    End If

                Next

                For AngleDecodeCnt = 1 To DecodeAngle.Length

                    If Mid(DecodeAngle, AngleDecodeCnt, 1) >= Chr(48) And Mid(DecodeAngle, AngleDecodeCnt, 1) <= Chr(57) Then
                        AngleDecode = AngleDecode + Mid(DecodeAngle, AngleDecodeCnt, 1)
                    ElseIf Mid(DecodeAngle, AngleDecodeCnt, 1) = Chr(46) Then
                        AngleDecode = AngleDecode + Mid(DecodeAngle, AngleDecodeCnt, 1)
                    End If

                Next

                ' Updates the data base with the information

                CmdProdDBStn10 = New OleDbCommand("UPDATE ProductionData SET Stn10RHCoverTorque = @Stn10RHCoverTorque, Stn10RHCoverAngle = @Stn10RHCoverAngle WHERE SerialNumber = @SerialNumber", ConProdDBStn10)
                CmdProdDBStn10.Parameters.AddWithValue("@Stn10RHCoverTorque", (TorqueDecode))
                CmdProdDBStn10.Parameters.AddWithValue("@Stn10RHCoverAngle", (AngleDecode))
                CmdProdDBStn10.Parameters.AddWithValue("@SerialNumber", Stn10CurrentWorkingSerialNumber)

                Try
                    ConProdDBStn10.Open()
                Catch
                Finally
                    ConProdDBStn10.Close()
                    ConProdDBStn10.Open()
                End Try

                Try
                    CmdProdDBStn10.ExecuteNonQuery()
                    PC2PLCReply("RHCSGood", "S10")
                Catch ex As Exception
                    MsgBox("Error updating the production database....", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error Updating")
                End Try

                ConProdDBStn10.Close()

            End If

        End If

    End Sub

    Private Delegate Sub Stn10TorqueOneStringDelegate(ByVal Data As String)
    ' Thread occupying private sub

Am I doing something wrong? My main error lately has been

"The IAsyncResult object was not returned from the corresponding asynchronous method on thsi class. Parameter name: asyncResult

Maybe you could possibly help...
 
Back
Top