|
-
Apr 30th, 2006, 11:16 PM
#1
Thread Starter
Junior Member
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:
Public Class Form1
Inherits System.Windows.Forms.Form
#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)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'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.
Friend WithEvents open_com2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.open_com2 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'open_com2
'
Me.open_com2.Location = New System.Drawing.Point(56, 88)
Me.open_com2.Name = "open_com2"
Me.open_com2.Size = New System.Drawing.Size(184, 64)
Me.open_com2.TabIndex = 0
Me.open_com2.Text = "Open Com2"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.open_com2)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
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 IO.Ports.SerialPort("COM2")
serial.BaudRate = 4800
serial.Parity = 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
'
-
Apr 30th, 2006, 11:23 PM
#2
Fanatic Member
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 
-
Apr 30th, 2006, 11:25 PM
#3
Fanatic Member
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 
-
Apr 30th, 2006, 11:47 PM
#4
Thread Starter
Junior Member
Re: System.IO.Ports.SerialPort not loading
sorry , not workd. new message
'System.IO.Ports.SerialPort' is not defined.
-
May 1st, 2006, 12:00 AM
#5
Fanatic Member
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 
-
May 1st, 2006, 12:01 AM
#6
Fanatic Member
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 
-
May 1st, 2006, 12:10 AM
#7
Thread Starter
Junior Member
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
-
May 1st, 2006, 12:12 AM
#8
Thread Starter
Junior Member
Re: System.IO.Ports.SerialPort not loading
may be the prblem in Ports.SerialPort not worked with Visual Studio 2003?!
-
May 1st, 2006, 12:12 AM
#9
Fanatic Member
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 
-
May 1st, 2006, 12:14 AM
#10
Fanatic Member
Re: System.IO.Ports.SerialPort not loading
 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 
-
May 1st, 2006, 12:17 AM
#11
Fanatic Member
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 
-
May 1st, 2006, 12:43 AM
#12
Thread Starter
Junior Member
Re: System.IO.Ports.SerialPort not loading
how can i open com2 by clicked on Button Open_com2 in Visual Studio 2003,please?!
-
May 1st, 2006, 05:20 AM
#13
Lively Member
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
-
May 1st, 2006, 06:28 AM
#14
Thread Starter
Junior Member
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
-
May 1st, 2006, 07:39 AM
#15
Addicted Member
Re: System.IO.Ports.SerialPort not loading
VB Code:
Imports System.IO.Ports
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SP As SerialPort = New SerialPort(ComboBox1.Text.ToString, ComboBox2.Text, Parity.None)
Try
SP.DataBits = TextBox9.Text
Select Case ComboBox4.Text
Case "0"
SP.StopBits = StopBits.None
Case "1"
SP.StopBits = StopBits.One
Case "1,5"
SP.StopBits = StopBits.OnePointFive
Case "2"
SP.StopBits = StopBits.Two
End Select
SP.Open()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
'startcode
SP.Write(TextBox1.Text)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|