Question Please Help - Access to COM1 denied

vbcom

New member
Joined
Oct 5, 2011
Messages
4
Programming Experience
Beginner
Hello

I don't know where the Problem is but i can't connect to COM1 with VB.Net
With Hyperterminal i can connect so the port insn't in use.
Can you help me correcting this code?

Thank you vbcom
PS: Full Code in Attachment

VB.NET:
    Private Sub SendData()
        On Error GoTo hilfe
        Dim tmp(9) As String
        tmp(0) = temp_da
        tmp(1) = temp_j
        tmp(2) = sh_da
        tmp(3) = sh_j
        tmp(4) = fsh_da
        tmp(5) = fsh_j
        tmp(6) = zeit
        tmp(7) = datum
        tmp(8) = jahr
        Dim COM As New Ports.SerialPort
        With COM
            .PortName = "COM1"
            .BaudRate = 9600
            .DataBits = 8
            .StopBits = Ports.StopBits.One
            .Parity = Ports.Parity.None
            .ReadTimeout = 10000
            .WriteTimeout = 10000
        End With
        COM.Open()
        Dim errnum As Integer = 0
retry:
        COM.Write("*")
        Dim i As Integer = 0
        While i <= 8
            If COM.ReadByte().ToString = "a" Then
                COM.Write(tmp(i))
            Else
                COM.Write("eeee")
                errnum = errnum + 1
                If errnum < 3 Then
                    GoTo retry
                Else
                    GoTo hilfe
                End If
            End If
            i = i + 1
        End While
        If COM.IsOpen Then COM.Close()
        Threading.Thread.Sleep(2000)
        COM.Dispose()
        Threading.Thread.Sleep(2000)
        Return

hilfe:
        Call mail(ErrorToString)
        Return
    End Sub

View attachment snowinfo.txt
 
Usually, if access is denied to a port then the port might be in use.
try closing the port then re-open it again.
 
Alternatively, if a program crashed without necessarily closing the COM port, the port *may* still believe it is in use. Any further access to the port will be denied.

Always handle any program errors VERY carefully when dealing with COM ports - lots of Try..Catch..End Try required !
 
First get rid of the unstructured Goto statements, then make sure to always close the port after use, as code is now it may not always do that.
 
I found out that you were correct. The Port didn't close after first try and the i of course couldn't open it again.

Also I did remove the gotos and added Try.

Now i get the Message "Read: The operation has timed out."
So it must be an error with COM.ReadByte.tostring()

Isn't a timeout of 100'000 enough?

I know that I need to find better Error Strings but it works for the test ;)

Thank you for your hints.
vbcom
VB.NET:
Private Sub SendData()
        Dim tmp(9) As String
        tmp(0) = temp_da
        tmp(1) = temp_j
        tmp(2) = sh_da
        tmp(3) = sh_j
        tmp(4) = fsh_da
        tmp(5) = fsh_j
        tmp(6) = zeit
        tmp(7) = datum
        tmp(8) = jahr
        Dim COM As New Ports.SerialPort
        With COM
            .PortName = "COM1"
            .BaudRate = 9600
            .DataBits = 8
            .StopBits = Ports.StopBits.One
            .Parity = Ports.Parity.None
            .ReadTimeout = 100000
            .WriteTimeout = 100000
        End With
        Dim errnum As Integer = 0

        While (errnum < 3)
            Try
                COM.Open()
            Catch ex As Exception
                Call mail("open: " & ErrorToString())
            End Try

            Try
                COM.Write("*")
            Catch ex As Exception
                Call mail("Write *: " & ErrorToString())
            End Try

            Dim respa As String = ""
            Dim i As Integer = 0
            While i <= 8
                Try
                    respa = COM.ReadByte.ToString()
                Catch ex As Exception
                    Call mail("Read: " & ErrorToString())
                End Try
                If respa = "a" Then
                    Try
                        COM.Write(tmp(i))
                    Catch ex As Exception
                        Call mail("Write tmp: " & ErrorToString())
                    End Try
                Else
                    Try
                        COM.Write("eeee")
                        errnum = errnum + 1
                        If errnum >= 3 Then
                            COM.Close()
                            Threading.Thread.Sleep(2000)
                            COM.Dispose()
                            Threading.Thread.Sleep(2000)
                            Call mail("Write eeee" & ErrorToString())
                        End If
                    Catch ex As Exception
                        Call mail("Try Write eeee" & ErrorToString())
                    End Try

                End If
                i = i + 1
            End While
            Try
                COM.Close()
                Threading.Thread.Sleep(2000)
                COM.Dispose()
                Threading.Thread.Sleep(2000)
            Catch ex As Exception
                Call mail(ErrorToString)
            End Try

        End While
        Return
    End Sub
 
Back
Top