Results 1 to 15 of 15

Thread: System.IO.Ports.SerialPort not loading

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    24

    System.IO.Ports.SerialPort not loading

    Hi,

    im trying to open com2 . whene i clicked on Button Open_com2 the com 2 must be open but i have some problem like the following
    1- Type 'IO.Ports.SerialPort' is not defined.
    2-'Ports' is not a member of 'IO'.


    Can anyone tell me how can I solve this?
    please help me i got headache

    this all my code i use Visual Studio 2003

    VB Code:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.  
    4. #Region " Windows Form Designer generated code "
    5.  
    6.     Public Sub New()
    7.         MyBase.New()
    8.  
    9.         'This call is required by the Windows Form Designer.
    10.         InitializeComponent()
    11.  
    12.         'Add any initialization after the InitializeComponent() call
    13.  
    14.     End Sub
    15.  
    16.     'Form overrides dispose to clean up the component list.
    17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    18.         If disposing Then
    19.             If Not (components Is Nothing) Then
    20.                 components.Dispose()
    21.             End If
    22.         End If
    23.         MyBase.Dispose(disposing)
    24.     End Sub
    25.  
    26.     'Required by the Windows Form Designer
    27.     Private components As System.ComponentModel.IContainer
    28.  
    29.     'NOTE: The following procedure is required by the Windows Form Designer
    30.     'It can be modified using the Windows Form Designer.  
    31.     'Do not modify it using the code editor.
    32.     Friend WithEvents open_com2 As System.Windows.Forms.Button
    33.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    34.         Me.open_com2 = New System.Windows.Forms.Button
    35.         Me.SuspendLayout()
    36.         '
    37.         'open_com2
    38.         '
    39.         Me.open_com2.Location = New System.Drawing.Point(56, 88)
    40.         Me.open_com2.Name = "open_com2"
    41.         Me.open_com2.Size = New System.Drawing.Size(184, 64)
    42.         Me.open_com2.TabIndex = 0
    43.         Me.open_com2.Text = "Open Com2"
    44.         '
    45.         'Form1
    46.         '
    47.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    48.         Me.ClientSize = New System.Drawing.Size(292, 266)
    49.         Me.Controls.Add(Me.open_com2)
    50.         Me.Name = "Form1"
    51.         Me.Text = "Form1"
    52.         Me.ResumeLayout(False)
    53.  
    54.     End Sub
    55.  
    56. #End Region
    57.  
    58.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles open_com2.Click
    59.  
    60.  
    61.         'usually, you would declare the serial port at form level and initialize
    62.         'it in the constructor so you can get responses from the port
    63.  
    64.         'declare the serial port and set it for 4800,N,8,1
    65.         Dim serial As New IO.Ports.SerialPort("COM2")
    66.         serial.BaudRate = 4800
    67.         serial.Parity = IO.Ports.Parity.None
    68.         serial.DataBits = 8
    69.         serial.StopBits = IO.Ports.StopBits.One
    70.  
    71.          'make sure the port is open
    72.         Try
    73.             If Not serial.IsOpen Then serial.Open()
    74.         Catch ex As Exception
    75.             'specifically an UnauthorizedAccessException
    76.             'the port is already open somewhere else
    77.             MessageBox.Show("The port is already opened by another process.")
    78.             Exit Sub
    79.         End Try
    80.  
    81.        '
    82.         'uncomment the next line if you do not need to get responses
    83.         'and you are done with the port
    84.         'serial.Close()
    85.  
    86.     End Sub
    87. End Class
    88. '

  2. #2
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: System.IO.Ports.SerialPort not loading

    I'm not seeing the System namespace in your declaration.

    Dim serial As New IO.Ports.SerialPort("COM2")

    should be

    Dim serial As New System.IO.Ports.SerialPort("COM2")

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  3. #3
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: System.IO.Ports.SerialPort not loading

    Also you could just import the System namespace at the very top of your codewindow and you won't have to type it each time

    Imports System

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    24

    Re: System.IO.Ports.SerialPort not loading

    sorry , not workd. new message
    'System.IO.Ports.SerialPort' is not defined.

  5. #5
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: System.IO.Ports.SerialPort not loading

    so when you use

    Dim serial As New System.IO.Ports.SerialPort("COM2")

    its still telling you its not defined?

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  6. #6
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: System.IO.Ports.SerialPort not loading

    show me your code again for your button click

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    24

    Re: System.IO.Ports.SerialPort not loading

    Public Class Form1
    Inherits System.Windows.Forms.Form
    Friend WithEvents open_com2 As System.Windows.Forms.Button

    #Region " Windows Form Designer generated code "

    Public Sub New()

    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    MyBase.Dispose(disposing)
    End Sub

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    Private Sub InitializeComponent()
    Me.open_com2 = New System.Windows.Forms.Button
    '
    'open_com2
    '
    Me.open_com2.Location = New System.Drawing.Point(240, 112)
    Me.open_com2.Size = New System.Drawing.Size(176, 72)
    Me.open_com2.Text = "open com2"
    '
    'Form1
    '
    Me.Controls.Add(Me.open_com2)
    Me.Text = "Form1"

    End Sub

    Public Shared Sub Main()
    Application.Run(New Form1())
    End Sub

    #End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles open_com2.Click
    'usually, you would declare the serial port at form level and initialize
    'it in the constructor so you can get responses from the port

    'declare the serial port and set it for 4800,N,8,1
    Dim serial As New System.IO.Ports.SerialPort("COM2")
    serial.BaudRate = 4800
    serial.Parity = System.IO.Ports.Parity.None
    serial.DataBits = 8
    serial.StopBits = IO.Ports.StopBits.One

    'make sure the port is open
    Try
    If Not serial.IsOpen Then serial.Open()
    Catch ex As Exception
    'specifically an UnauthorizedAccessException
    'the port is already open somewhere else
    MessageBox.Show("The port is already opened by another process.")
    Exit Sub
    End Try

    '
    'uncomment the next line if you do not need to get responses
    'and you are done with the port
    'serial.Close()

    End Sub



    End Class

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    24

    Re: System.IO.Ports.SerialPort not loading

    may be the prblem in Ports.SerialPort not worked with Visual Studio 2003?!

  9. #9
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: System.IO.Ports.SerialPort not loading

    Click REBUILD.

    Strange, but I'm not having any problems with that code you just posted

    I'm on 2005, but that shouldn't make a difference with this code

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  10. #10
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: System.IO.Ports.SerialPort not loading

    Quote Originally Posted by vbnm
    may be the prblem in Ports.SerialPort not worked with Visual Studio 2003?!
    You may be right, try to follow the intellisense is all i can say. I don't have a 2003 install available right now to test with.

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  11. #11
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: System.IO.Ports.SerialPort not loading

    I think that namespace is 2005 only.

    Try System.IO.Serial

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    24

    Re: System.IO.Ports.SerialPort not loading

    how can i open com2 by clicked on Button Open_com2 in Visual Studio 2003,please?!

  13. #13
    Lively Member
    Join Date
    Apr 2006
    Location
    Local
    Posts
    112

    Re: System.IO.Ports.SerialPort not loading

    system.io.ports namespace is not part of VS2003.
    support for serial ports was bought back in VS2005

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    24

    Re: System.IO.Ports.SerialPort not loading

    oky please learn me how can i communicate whit Rs232 ???!! i will use windows ce and smart drive

  15. #15
    Addicted Member
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    198

    Re: System.IO.Ports.SerialPort not loading

    VB Code:
    1. Imports System.IO.Ports
    2. Public Class Form1
    3.       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.        
    5. Dim SP As SerialPort = New SerialPort(ComboBox1.Text.ToString, ComboBox2.Text, Parity.None)
    6.         Try
    7.             SP.DataBits = TextBox9.Text
    8.  
    9.             Select Case ComboBox4.Text
    10.                 Case "0"
    11.                     SP.StopBits = StopBits.None
    12.                 Case "1"
    13.                     SP.StopBits = StopBits.One
    14.                 Case "1,5"
    15.                     SP.StopBits = StopBits.OnePointFive
    16.                 Case "2"
    17.                     SP.StopBits = StopBits.Two
    18.             End Select
    19.  
    20.             SP.Open()
    21.         Catch ex As Exception
    22.             MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    23.             Exit Sub
    24.         End Try
    25.  
    26.         'startcode
    27.         SP.Write(TextBox1.Text)
    28.         SP.Close()

    combobox1 = {"COM1","COM2"}
    combobox2 = {600,1200,2400,4800,9600,14400,...whatever}
    textbox9 = between 5 and 8


    Works fine

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