Results 1 to 12 of 12

Thread: [RESOLVED] Serialport error

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2016
    Posts
    25

    Resolved [RESOLVED] Serialport error

    Hello guys maybe a little problem for you guys. I want to communicate with the a usb to serial device. But I get when I type Imports System.IO. no ports with intellisense when I type it after IO.

    I used the tutorial from here

    Code:
    Imports System          'To Access Console.WriteLine()
    Imports System.IO.Ports 'To Access the SerialPort Object
    Module Program
        Sub Main(args As String())
            Dim AvailablePorts() As String = SerialPort.GetPortNames()
    
            Console.WriteLine("Available Ports ::")
    
            Dim Port As String
    
            For Each Port In AvailablePorts
                Console.WriteLine(Port)
            Next Port
        End Sub
    End Module
    Also SerialPort.GetPortNames() is red-underlined the error I get is :

    Severity Code Description Project File Line Suppression State
    Error BC30451 'SerialPort' is not declared. It may be inaccessible due to its protection level. Serial C:\Users\Lammert\source\repos\Serial\Serial\Program.vb 5 Active

    At this forum I found this https://www.vbforums.com/showthread....=1#post4924855
    after this command I get :
    IO.Ports.SerialPort.GetPortNames

    IO.Ports.SerialPort.GetPortNames
    Build Failed. For a list of build errors see the Error List. For detailed information about a build error select an item and press F1.

    Just to explain it to you guys it is a problem of visual studio not with my serial device because a vb6 program is working fine on it.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Serialport error

    ALWAYS read the documentation. If you had read the documentation for the SerialPort class then you'd know that it is declared in the System.IO.Ports.dll assembly. Have you referenced that assembly? If not then you can't access any types it contains. You can't import a namespace unless you have referenced an assembly that contains types that are members of that namespace. If you don't understand the concepts of referencing assemblies and importing namespaces then I suggest that you follow the Blog link in my signature below and check out my post on the subject.

  3. #3
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Serialport error

    the reference are good, the problem is elsewhere

    When you want to use a serial port like here
    Code:
    Dim AvailablePorts() As String = SerialPort.GetPortNames()
    you must first create one..

    You can have the list of all available serial ports (the ones connected) in
    Code:
    My.Computer.Ports.SerialPortNames
    Unless you know already the number of the serial port you use (it is of the type "COM#"), (and even so) I advice to show the list of the ports with the following code (the port number may change form a computer to another)

    Code:
     Private Sub B_findCOM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_findCOM.Click
            Dim portCOMname As String
            Dim n As Integer
            n = 0
            ListPortCOM.Controls.Clear()
            For Each portCOMname In My.Computer.Ports.SerialPortNames
                Dim rdo As New RadioButton
                rdo.Name = portCOMname
                rdo.Text = portCOMname
                n = n + 1
                rdo.Location = New Point(5, 20 * n)
                ListPortCOM.Controls.Add(rdo)
            Next
        End Sub
    I create one radio button for each port detected, I put each radiobutton in a group box
    ListPortCOM

    then select the port and open it. For that you need to create one. In the designer go to the component section and add a serialport object to your form (here it is called SerialPort_1)

    Add a button to do the connection :

    Code:
    Private Sub B_Connection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_Connection.Click
            Dim rButton As RadioButton
            rButton = ListePortCOM.Controls.OfType(Of RadioButton)().FirstOrDefault(Function(r) r.Checked = True)
            SerialPort_1.PortName = rButton.Name  ' if you know already the port name, you can put it here
            SerialPort_1.BaudRate = 9600
            SerialPort_1.DataBits = 8
            SerialPort_1.Parity = Parity.None
            SerialPort_1.StopBits = StopBits.One
            SerialPort_1.Handshake = Handshake.None
            SerialPort_1.Encoding = System.Text.Encoding.Default 'very important!
    
            'do the connection
            Try
                SerialPort_1.Open()
                SerialPort_1.ReadTimeout = 100000
                SerialPort_1.WriteTimeout = 100000
    
                If SerialPort_1.IsOpen Then
                    MsgBox("Connection successful")
    End If
                
    Label_port_valeur.Text = SerialPort_1.PortName.ToString ' just to show the port connected
    
    Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    Last edited by Delaney; Apr 19th, 2021 at 05:49 AM.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Serialport error

    Quote Originally Posted by Delaney View Post
    the reference are good, the problem is elsewhere
    Nope. There may be problems elsewhere too but I suspect that you are not correctly differentiating between an assembly reference and a namespace import. This:
    vb.net Code:
    1. Imports System          'To Access Console.WriteLine()
    2. Imports System.IO.Ports 'To Access the SerialPort Object
    does not contain any references. That is a pair of namespace imports and the OP specifically stated that Intellisense doesn't offer the option for the Ports child namespace of the System.IO namespace, which would only happen if the appropriate assembly had not been referenced.

    The comments in that code are bad too. As I state in the blog post I mentioned, importing a namespace doesn't give you access to anything extra. The one and only thing it does is allow you to refer to types in that namespace without qualifying their names. For instance, importing the System namespace does provide access to the Console.WriteLine method. It means that you can refer to the System.Console class as simply Console. Without the import, you can still use System.Console.WriteLine in your code. That said, the System namespace is almost certainly already imported at the project level, so doing so at the file level is probably pointless.

    Similarly, importing System.IO.Ports means that you can refer to the System.IO.Ports.SerialPort class as simply SerialPort. Without the import, you could still use the fully qualified name. Even if saying "To Access..." were accurate, it is the SerialPort type or class, not object. There's no object until you actually create an instance of that type. That so many people are so sloppy with terminology is one reason that beginners get confused about these topics when they're learning.

  5. #5
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Serialport error

    You are right but I don't confound both. It is just that I think we are a victim of the ease provided by VS. When you put the import line (eg. Imports System.IO.Ports) if it is not referenced, it will give an error and so "invites" you to go to the project windows to add the reference. So for me putting the import line is also a way to be sure that I have the reference.

    The ease can be a enemy of rigor.

    It seems, bay the ways, that you don't need to add a reference for System.IO.ports, it seems included by default
    Last edited by Delaney; Apr 19th, 2021 at 05:59 AM.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Serialport error

    Quote Originally Posted by Delaney View Post
    It seems, bay the ways, that you don't need to add a reference for System.IO.ports, it seems included by default
    I have a solution full of test projects and I just checked the WinForms and Console projects targeting .NET 4.8 and it's not referenced in either project.

  7. #7
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Serialport error

    Quote Originally Posted by jmcilhinney View Post
    I have a solution full of test projects and I just checked the WinForms and Console projects targeting .NET 4.8 and it's not referenced in either project.
    In the .NET Framework, the SerialPort Class is part of the System.dll assembly which is referenced by default.

    The OP's project is using some flavour of .NET Core/.NET 5 and it's those versions of .NET that have the SerialPort Class in the System.IO.Ports.dll assembly (available through Nuget); part of .NET Platform Extensions 5.0 or some such??....too many versions of .NET for me to wrap my tired old brain around these days

  8. #8
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Serialport error

    Looking at the op's code I have a few coding tips:
    1. Always initialize your variables when declaring them. In this case add something like "= Nothing".
    2. In vb.net you can declare the iterator variable in the For statement:
    Code:
    For Each Port As String In AvailablePorts
    .
    3. Where possible I prefer lists over arrays.
    4. The whole For loop can be reduced to a single line:

    Code:
    Array.ForEach(AvailablePorts, AddressOf Console.WriteLine)
    in fact:
    Code:
    Array.ForEach(SerialPort.GetPortNames(), AddressOf Console.WriteLine)
    should work too.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Aug 2016
    Posts
    25

    Re: Serialport error

    @Delaney
    My.Computer.Ports is also red underlined and is given me the message 'ports is not a member of Mycomputer'.

    @jmcilhinney I searched my ssd for the file but nowhere to be found :
    C:\>dir /s System.IO.Ports.dll
    Volume in drive C is OS
    Volume Serial Number is B045-FBA2
    File Not Found

    Also thx for the nice explanation I've also read your blog http://jmcilhinney.blogspot.com/2009...importing.html for the full details. But it's not the problem the file is nowhere on my drive.

    @Inferrd
    I installed the Nuget package but still the same and you were right I am using .net 5.0.
    Install-Package Microsoft.Extensions.Configuration -Version 5.0.0

    after a little search on the web I found out I needed this package :
    Install-Package System.IO.Ports -Version 5.0.1
    I was in the assumption that all files were on my drive but you need some extra packages!

  10. #10
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Serialport error

    Quote Originally Posted by lamko View Post
    @Inferrd
    I installed the Nuget package but still the same and you were right I am using .net 5.0.
    Install-Package Microsoft.Extensions.Configuration -Version 5.0.0

    after a little search on the web I found out I needed this package :
    Install-Package System.IO.Ports -Version 5.0.1
    I was in the assumption that all files were on my drive but you need some extra packages!
    Ah. I see how what I wrote is confusing. I meant that the package available via Nuget is System.IO.Ports.

    My mention of .NET Platform Extensions 5.0 was in reference to what currently appears in the documentation for the SerialPort Class in the framework Version dropdown box when you try to select any of the .NET Core or .NET5.0/6.0 versions.
    Last edited by Inferrd; Apr 19th, 2021 at 08:45 AM. Reason: wrong link

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Serialport error

    Quote Originally Posted by Inferrd View Post
    In the .NET Framework, the SerialPort Class is part of the System.dll assembly which is referenced by default.

    The OP's project is using some flavour of .NET Core/.NET 5 and it's those versions of .NET that have the SerialPort Class in the System.IO.Ports.dll assembly (available through Nuget); part of .NET Platform Extensions 5.0 or some such??....too many versions of .NET for me to wrap my tired old brain around these days
    Bugger! Thought I was looking at Framework documentation but must have been Core/5.0. Thanks for the correction.

  12. #12
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Serialport error

    A general remark : This just shows that it will be important in the future threads that the OP should precise the .net version (FW 3->4.8 or core, etc) that it is used...
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

Posting Permissions

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



Click Here to Expand Forum to Full Width