tentavarious
asked on
transmitting through rs 232 port using vb.net
Hello experts, i would like to transmit some data through my 232 port when a button is clicked on my webform. I have no idea how to do this using vb.net. What i have done is built a check list webpage, and when the check list is completed by the user, the user clicks a button and some data is transmitted through the 232 port, and picked up by a plc. Could someone show me or give an example of coding this, using vb.net?
ASKER
Does vb.net come with a class that deals with rs 232 port, or can i download it from microsoft for free?
Sax.NET comes with the Visual Basic.NET Resource Kit for 2003. It is a community version, and provides simple RS-232 support, and should fit your requirements.
Bob
Bob
ASKER
Isnt there a way i can do it without using another tool? I thought vb.net had a class that dealt with this where i could set up the properites of what com port to use and the baud rate and so on?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Could you explain to me what all the properties of the com port are used for. For example i know baud rate is the speed, but what does parity mean?
Aaah, a serial communication lesson:
Introduction to Serial Communications:
http://www.taltech.com/TALtech_web/resources/intro-sc.html
"A parity bit affords a small amount of error checking, to help detect data corruption that might occur during transmission. You can choose either even parity, odd parity, mark parity, space parity or none at all. When even or odd parity is being used, the number of marks (logical 1 bits) in each data byte are counted, and a single bit is transmitted following the data bits to indicate whether the number of 1 bits just sent is even or odd."
Look at the "Baud vs. Bits per Second" to make sure that you understand that it is not Bits/Second.
Bob
Introduction to Serial Communications:
http://www.taltech.com/TALtech_web/resources/intro-sc.html
"A parity bit affords a small amount of error checking, to help detect data corruption that might occur during transmission. You can choose either even parity, odd parity, mark parity, space parity or none at all. When even or odd parity is being used, the number of marks (logical 1 bits) in each data byte are counted, and a single bit is transmitted following the data bits to indicate whether the number of 1 bits just sent is even or odd."
Look at the "Baud vs. Bits per Second" to make sure that you understand that it is not Bits/Second.
Bob
ASKER
Thanks for all the input
ASKER
I did find this code that uses vb.net and win forms to establish a serial connection, and it doesnt seem like it is using sax. Could you explain how they are doing it. The only import is the system.text
Imports System.Text
Public Class frmMain
Inherits System.Windows.Forms.Form
' Declare necessary class variables.
Private m_CommPort As New Rs232()
Private m_IsModemFound As Boolean = False
Private m_ModemPort As Integer = 0
#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
' So that we only need to set the title of the application once,
' we use the AssemblyInfo class (defined in the AssemblyInfo.vb file)
' to read the AssemblyTitle attribute.
Dim ainfo As New AssemblyInfo()
Me.Text = ainfo.Title
Me.mnuAbout.Text = String.Format("&About {0} ...", ainfo.Title)
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.ICon tainer
'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 mnuMain As System.Windows.Forms.MainM enu
Friend WithEvents mnuFile As System.Windows.Forms.MenuI tem
Friend WithEvents mnuExit As System.Windows.Forms.MenuI tem
Friend WithEvents mnuHelp As System.Windows.Forms.MenuI tem
Friend WithEvents mnuAbout As System.Windows.Forms.MenuI tem
Friend WithEvents txtStatus As System.Windows.Forms.TextB ox
Friend WithEvents tmrReadCommPort As System.Windows.Forms.Timer
Friend WithEvents clstPorts As System.Windows.Forms.Check edListBox
Friend WithEvents btnCheckForPorts As System.Windows.Forms.Butto n
Friend WithEvents btnCheckModems As System.Windows.Forms.Butto n
Friend WithEvents txtSelectedModem As System.Windows.Forms.TextB ox
Friend WithEvents btnSendATCommand As System.Windows.Forms.Butto n
Friend WithEvents txtUserCommand As System.Windows.Forms.TextB ox
Friend WithEvents btnSendUserCommand As System.Windows.Forms.Butto n
Friend WithEvents btnClear As System.Windows.Forms.Butto n
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Cont ainer()
Dim resources As System.Resources.ResourceM anager = New System.Resources.ResourceM anager(Get Type(frmMa in))
Me.mnuMain = New System.Windows.Forms.MainM enu()
Me.mnuFile = New System.Windows.Forms.MenuI tem()
Me.mnuExit = New System.Windows.Forms.MenuI tem()
Me.mnuHelp = New System.Windows.Forms.MenuI tem()
Me.mnuAbout = New System.Windows.Forms.MenuI tem()
Me.btnCheckForPorts = New System.Windows.Forms.Butto n()
Me.txtStatus = New System.Windows.Forms.TextB ox()
Me.tmrReadCommPort = New System.Windows.Forms.Timer (Me.compon ents)
Me.clstPorts = New System.Windows.Forms.Check edListBox( )
Me.btnCheckModems = New System.Windows.Forms.Butto n()
Me.txtSelectedModem = New System.Windows.Forms.TextB ox()
Me.btnSendATCommand = New System.Windows.Forms.Butto n()
Me.btnSendUserCommand = New System.Windows.Forms.Butto n()
Me.txtUserCommand = New System.Windows.Forms.TextB ox()
Me.btnClear = New System.Windows.Forms.Butto n()
Me.SuspendLayout()
'
'mnuMain
'
Me.mnuMain.MenuItems.AddRa nge(New System.Windows.Forms.MenuI tem() {Me.mnuFile, Me.mnuHelp})
Me.mnuMain.RightToLeft = CType(resources.GetObject( "mnuMain.R ightToLeft "), System.Windows.Forms.Right ToLeft)
'
'mnuFile
'
Me.mnuFile.Enabled = CType(resources.GetObject( "mnuFile.E nabled"), Boolean)
Me.mnuFile.Index = 0
Me.mnuFile.MenuItems.AddRa nge(New System.Windows.Forms.MenuI tem() {Me.mnuExit})
Me.mnuFile.Shortcut = CType(resources.GetObject( "mnuFile.S hortcut"), System.Windows.Forms.Short cut)
Me.mnuFile.ShowShortcut = CType(resources.GetObject( "mnuFile.S howShortcu t"), Boolean)
Me.mnuFile.Text = resources.GetString("mnuFi le.Text")
Me.mnuFile.Visible = CType(resources.GetObject( "mnuFile.V isible"), Boolean)
'
'mnuExit
'
Me.mnuExit.Enabled = CType(resources.GetObject( "mnuExit.E nabled"), Boolean)
Me.mnuExit.Index = 0
Me.mnuExit.Shortcut = CType(resources.GetObject( "mnuExit.S hortcut"), System.Windows.Forms.Short cut)
Me.mnuExit.ShowShortcut = CType(resources.GetObject( "mnuExit.S howShortcu t"), Boolean)
Me.mnuExit.Text = resources.GetString("mnuEx it.Text")
Me.mnuExit.Visible = CType(resources.GetObject( "mnuExit.V isible"), Boolean)
'
'mnuHelp
'
Me.mnuHelp.Enabled = CType(resources.GetObject( "mnuHelp.E nabled"), Boolean)
Me.mnuHelp.Index = 1
Me.mnuHelp.MenuItems.AddRa nge(New System.Windows.Forms.MenuI tem() {Me.mnuAbout})
Me.mnuHelp.Shortcut = CType(resources.GetObject( "mnuHelp.S hortcut"), System.Windows.Forms.Short cut)
Me.mnuHelp.ShowShortcut = CType(resources.GetObject( "mnuHelp.S howShortcu t"), Boolean)
Me.mnuHelp.Text = resources.GetString("mnuHe lp.Text")
Me.mnuHelp.Visible = CType(resources.GetObject( "mnuHelp.V isible"), Boolean)
'
'mnuAbout
'
Me.mnuAbout.Enabled = CType(resources.GetObject( "mnuAbout. Enabled"), Boolean)
Me.mnuAbout.Index = 0
Me.mnuAbout.Shortcut = CType(resources.GetObject( "mnuAbout. Shortcut") , System.Windows.Forms.Short cut)
Me.mnuAbout.ShowShortcut = CType(resources.GetObject( "mnuAbout. ShowShortc ut"), Boolean)
Me.mnuAbout.Text = resources.GetString("mnuAb out.Text")
Me.mnuAbout.Visible = CType(resources.GetObject( "mnuAbout. Visible"), Boolean)
'
'btnCheckForPorts
'
Me.btnCheckForPorts.Access ibleDescri ption = resources.GetString("btnCh eckForPort s.Accessib leDescript ion")
Me.btnCheckForPorts.Access ibleName = resources.GetString("btnCh eckForPort s.Accessib leName")
Me.btnCheckForPorts.Anchor = CType(resources.GetObject( "btnCheckF orPorts.An chor"), System.Windows.Forms.Ancho rStyles)
Me.btnCheckForPorts.Backgr oundImage = CType(resources.GetObject( "btnCheckF orPorts.Ba ckgroundIm age"), System.Drawing.Image)
Me.btnCheckForPorts.Dock = CType(resources.GetObject( "btnCheckF orPorts.Do ck"), System.Windows.Forms.DockS tyle)
Me.btnCheckForPorts.Enable d = CType(resources.GetObject( "btnCheckF orPorts.En abled"), Boolean)
Me.btnCheckForPorts.FlatSt yle = CType(resources.GetObject( "btnCheckF orPorts.Fl atStyle"), System.Windows.Forms.FlatS tyle)
Me.btnCheckForPorts.Font = CType(resources.GetObject( "btnCheckF orPorts.Fo nt"), System.Drawing.Font)
Me.btnCheckForPorts.Image = CType(resources.GetObject( "btnCheckF orPorts.Im age"), System.Drawing.Image)
Me.btnCheckForPorts.ImageA lign = CType(resources.GetObject( "btnCheckF orPorts.Im ageAlign") , System.Drawing.ContentAlig nment)
Me.btnCheckForPorts.ImageI ndex = CType(resources.GetObject( "btnCheckF orPorts.Im ageIndex") , Integer)
Me.btnCheckForPorts.ImeMod e = CType(resources.GetObject( "btnCheckF orPorts.Im eMode"), System.Windows.Forms.ImeMo de)
Me.btnCheckForPorts.Locati on = CType(resources.GetObject( "btnCheckF orPorts.Lo cation"), System.Drawing.Point)
Me.btnCheckForPorts.Name = "btnCheckForPorts"
Me.btnCheckForPorts.RightT oLeft = CType(resources.GetObject( "btnCheckF orPorts.Ri ghtToLeft" ), System.Windows.Forms.Right ToLeft)
Me.btnCheckForPorts.Size = CType(resources.GetObject( "btnCheckF orPorts.Si ze"), System.Drawing.Size)
Me.btnCheckForPorts.TabInd ex = CType(resources.GetObject( "btnCheckF orPorts.Ta bIndex"), Integer)
Me.btnCheckForPorts.Text = resources.GetString("btnCh eckForPort s.Text")
Me.btnCheckForPorts.TextAl ign = CType(resources.GetObject( "btnCheckF orPorts.Te xtAlign"), System.Drawing.ContentAlig nment)
Me.btnCheckForPorts.Visibl e = CType(resources.GetObject( "btnCheckF orPorts.Vi sible"), Boolean)
'
'txtStatus
'
Me.txtStatus.AccessibleDes cription = resources.GetString("txtSt atus.Acces sibleDescr iption")
Me.txtStatus.AccessibleNam e = resources.GetString("txtSt atus.Acces sibleName" )
Me.txtStatus.Anchor = CType(resources.GetObject( "txtStatus .Anchor"), System.Windows.Forms.Ancho rStyles)
Me.txtStatus.AutoSize = CType(resources.GetObject( "txtStatus .AutoSize" ), Boolean)
Me.txtStatus.BackgroundIma ge = CType(resources.GetObject( "txtStatus .Backgroun dImage"), System.Drawing.Image)
Me.txtStatus.Dock = CType(resources.GetObject( "txtStatus .Dock"), System.Windows.Forms.DockS tyle)
Me.txtStatus.Enabled = CType(resources.GetObject( "txtStatus .Enabled") , Boolean)
Me.txtStatus.Font = CType(resources.GetObject( "txtStatus .Font"), System.Drawing.Font)
Me.txtStatus.ImeMode = CType(resources.GetObject( "txtStatus .ImeMode") , System.Windows.Forms.ImeMo de)
Me.txtStatus.Location = CType(resources.GetObject( "txtStatus .Location" ), System.Drawing.Point)
Me.txtStatus.MaxLength = CType(resources.GetObject( "txtStatus .MaxLength "), Integer)
Me.txtStatus.Multiline = CType(resources.GetObject( "txtStatus .Multiline "), Boolean)
Me.txtStatus.Name = "txtStatus"
Me.txtStatus.PasswordChar = CType(resources.GetObject( "txtStatus .PasswordC har"), Char)
Me.txtStatus.ReadOnly = True
Me.txtStatus.RightToLeft = CType(resources.GetObject( "txtStatus .RightToLe ft"), System.Windows.Forms.Right ToLeft)
Me.txtStatus.ScrollBars = CType(resources.GetObject( "txtStatus .ScrollBar s"), System.Windows.Forms.Scrol lBars)
Me.txtStatus.Size = CType(resources.GetObject( "txtStatus .Size"), System.Drawing.Size)
Me.txtStatus.TabIndex = CType(resources.GetObject( "txtStatus .TabIndex" ), Integer)
Me.txtStatus.Text = resources.GetString("txtSt atus.Text" )
Me.txtStatus.TextAlign = CType(resources.GetObject( "txtStatus .TextAlign "), System.Windows.Forms.Horiz ontalAlign ment)
Me.txtStatus.Visible = CType(resources.GetObject( "txtStatus .Visible") , Boolean)
Me.txtStatus.WordWrap = CType(resources.GetObject( "txtStatus .WordWrap" ), Boolean)
'
'tmrReadCommPort
'
'
'clstPorts
'
Me.clstPorts.AccessibleDes cription = resources.GetString("clstP orts.Acces sibleDescr iption")
Me.clstPorts.AccessibleNam e = resources.GetString("clstP orts.Acces sibleName" )
Me.clstPorts.Anchor = CType(resources.GetObject( "clstPorts .Anchor"), System.Windows.Forms.Ancho rStyles)
Me.clstPorts.BackgroundIma ge = CType(resources.GetObject( "clstPorts .Backgroun dImage"), System.Drawing.Image)
Me.clstPorts.ColumnWidth = CType(resources.GetObject( "clstPorts .ColumnWid th"), Integer)
Me.clstPorts.Dock = CType(resources.GetObject( "clstPorts .Dock"), System.Windows.Forms.DockS tyle)
Me.clstPorts.Enabled = CType(resources.GetObject( "clstPorts .Enabled") , Boolean)
Me.clstPorts.Font = CType(resources.GetObject( "clstPorts .Font"), System.Drawing.Font)
Me.clstPorts.HorizontalExt ent = CType(resources.GetObject( "clstPorts .Horizonta lExtent"), Integer)
Me.clstPorts.HorizontalScr ollbar = CType(resources.GetObject( "clstPorts .Horizonta lScrollbar "), Boolean)
Me.clstPorts.ImeMode = CType(resources.GetObject( "clstPorts .ImeMode") , System.Windows.Forms.ImeMo de)
Me.clstPorts.IntegralHeigh t = CType(resources.GetObject( "clstPorts .IntegralH eight"), Boolean)
Me.clstPorts.Items.AddRang e(New Object() {resources.GetString("clst Ports.Item s.Items"), resources.GetString("clstP orts.Items .Items1"), resources.GetString("clstP orts.Items .Items2"), resources.GetString("clstP orts.Items .Items3")} )
Me.clstPorts.Location = CType(resources.GetObject( "clstPorts .Location" ), System.Drawing.Point)
Me.clstPorts.Name = "clstPorts"
Me.clstPorts.RightToLeft = CType(resources.GetObject( "clstPorts .RightToLe ft"), System.Windows.Forms.Right ToLeft)
Me.clstPorts.ScrollAlwaysV isible = CType(resources.GetObject( "clstPorts .ScrollAlw aysVisible "), Boolean)
Me.clstPorts.Size = CType(resources.GetObject( "clstPorts .Size"), System.Drawing.Size)
Me.clstPorts.TabIndex = CType(resources.GetObject( "clstPorts .TabIndex" ), Integer)
Me.clstPorts.TabStop = False
Me.clstPorts.Visible = CType(resources.GetObject( "clstPorts .Visible") , Boolean)
'
'btnCheckModems
'
Me.btnCheckModems.Accessib leDescript ion = resources.GetString("btnCh eckModems. Accessible Descriptio n")
Me.btnCheckModems.Accessib leName = resources.GetString("btnCh eckModems. Accessible Name")
Me.btnCheckModems.Anchor = CType(resources.GetObject( "btnCheckM odems.Anch or"), System.Windows.Forms.Ancho rStyles)
Me.btnCheckModems.Backgrou ndImage = CType(resources.GetObject( "btnCheckM odems.Back groundImag e"), System.Drawing.Image)
Me.btnCheckModems.Dock = CType(resources.GetObject( "btnCheckM odems.Dock "), System.Windows.Forms.DockS tyle)
Me.btnCheckModems.Enabled = CType(resources.GetObject( "btnCheckM odems.Enab led"), Boolean)
Me.btnCheckModems.FlatStyl e = CType(resources.GetObject( "btnCheckM odems.Flat Style"), System.Windows.Forms.FlatS tyle)
Me.btnCheckModems.Font = CType(resources.GetObject( "btnCheckM odems.Font "), System.Drawing.Font)
Me.btnCheckModems.Image = CType(resources.GetObject( "btnCheckM odems.Imag e"), System.Drawing.Image)
Me.btnCheckModems.ImageAli gn = CType(resources.GetObject( "btnCheckM odems.Imag eAlign"), System.Drawing.ContentAlig nment)
Me.btnCheckModems.ImageInd ex = CType(resources.GetObject( "btnCheckM odems.Imag eIndex"), Integer)
Me.btnCheckModems.ImeMode = CType(resources.GetObject( "btnCheckM odems.ImeM ode"), System.Windows.Forms.ImeMo de)
Me.btnCheckModems.Location = CType(resources.GetObject( "btnCheckM odems.Loca tion"), System.Drawing.Point)
Me.btnCheckModems.Name = "btnCheckModems"
Me.btnCheckModems.RightToL eft = CType(resources.GetObject( "btnCheckM odems.Righ tToLeft"), System.Windows.Forms.Right ToLeft)
Me.btnCheckModems.Size = CType(resources.GetObject( "btnCheckM odems.Size "), System.Drawing.Size)
Me.btnCheckModems.TabIndex = CType(resources.GetObject( "btnCheckM odems.TabI ndex"), Integer)
Me.btnCheckModems.Text = resources.GetString("btnCh eckModems. Text")
Me.btnCheckModems.TextAlig n = CType(resources.GetObject( "btnCheckM odems.Text Align"), System.Drawing.ContentAlig nment)
Me.btnCheckModems.Visible = CType(resources.GetObject( "btnCheckM odems.Visi ble"), Boolean)
'
'txtSelectedModem
'
Me.txtSelectedModem.Access ibleDescri ption = resources.GetString("txtSe lectedMode m.Accessib leDescript ion")
Me.txtSelectedModem.Access ibleName = resources.GetString("txtSe lectedMode m.Accessib leName")
Me.txtSelectedModem.Anchor = CType(resources.GetObject( "txtSelect edModem.An chor"), System.Windows.Forms.Ancho rStyles)
Me.txtSelectedModem.AutoSi ze = CType(resources.GetObject( "txtSelect edModem.Au toSize"), Boolean)
Me.txtSelectedModem.Backgr oundImage = CType(resources.GetObject( "txtSelect edModem.Ba ckgroundIm age"), System.Drawing.Image)
Me.txtSelectedModem.Dock = CType(resources.GetObject( "txtSelect edModem.Do ck"), System.Windows.Forms.DockS tyle)
Me.txtSelectedModem.Enable d = CType(resources.GetObject( "txtSelect edModem.En abled"), Boolean)
Me.txtSelectedModem.Font = CType(resources.GetObject( "txtSelect edModem.Fo nt"), System.Drawing.Font)
Me.txtSelectedModem.ImeMod e = CType(resources.GetObject( "txtSelect edModem.Im eMode"), System.Windows.Forms.ImeMo de)
Me.txtSelectedModem.Locati on = CType(resources.GetObject( "txtSelect edModem.Lo cation"), System.Drawing.Point)
Me.txtSelectedModem.MaxLen gth = CType(resources.GetObject( "txtSelect edModem.Ma xLength"), Integer)
Me.txtSelectedModem.Multil ine = CType(resources.GetObject( "txtSelect edModem.Mu ltiline"), Boolean)
Me.txtSelectedModem.Name = "txtSelectedModem"
Me.txtSelectedModem.Passwo rdChar = CType(resources.GetObject( "txtSelect edModem.Pa sswordChar "), Char)
Me.txtSelectedModem.RightT oLeft = CType(resources.GetObject( "txtSelect edModem.Ri ghtToLeft" ), System.Windows.Forms.Right ToLeft)
Me.txtSelectedModem.Scroll Bars = CType(resources.GetObject( "txtSelect edModem.Sc rollBars") , System.Windows.Forms.Scrol lBars)
Me.txtSelectedModem.Size = CType(resources.GetObject( "txtSelect edModem.Si ze"), System.Drawing.Size)
Me.txtSelectedModem.TabInd ex = CType(resources.GetObject( "txtSelect edModem.Ta bIndex"), Integer)
Me.txtSelectedModem.Text = resources.GetString("txtSe lectedMode m.Text")
Me.txtSelectedModem.TextAl ign = CType(resources.GetObject( "txtSelect edModem.Te xtAlign"), System.Windows.Forms.Horiz ontalAlign ment)
Me.txtSelectedModem.Visibl e = CType(resources.GetObject( "txtSelect edModem.Vi sible"), Boolean)
Me.txtSelectedModem.WordWr ap = CType(resources.GetObject( "txtSelect edModem.Wo rdWrap"), Boolean)
'
'btnSendATCommand
'
Me.btnSendATCommand.Access ibleDescri ption = resources.GetString("btnSe ndATComman d.Accessib leDescript ion")
Me.btnSendATCommand.Access ibleName = resources.GetString("btnSe ndATComman d.Accessib leName")
Me.btnSendATCommand.Anchor = CType(resources.GetObject( "btnSendAT Command.An chor"), System.Windows.Forms.Ancho rStyles)
Me.btnSendATCommand.Backgr oundImage = CType(resources.GetObject( "btnSendAT Command.Ba ckgroundIm age"), System.Drawing.Image)
Me.btnSendATCommand.Dock = CType(resources.GetObject( "btnSendAT Command.Do ck"), System.Windows.Forms.DockS tyle)
Me.btnSendATCommand.Enable d = CType(resources.GetObject( "btnSendAT Command.En abled"), Boolean)
Me.btnSendATCommand.FlatSt yle = CType(resources.GetObject( "btnSendAT Command.Fl atStyle"), System.Windows.Forms.FlatS tyle)
Me.btnSendATCommand.Font = CType(resources.GetObject( "btnSendAT Command.Fo nt"), System.Drawing.Font)
Me.btnSendATCommand.Image = CType(resources.GetObject( "btnSendAT Command.Im age"), System.Drawing.Image)
Me.btnSendATCommand.ImageA lign = CType(resources.GetObject( "btnSendAT Command.Im ageAlign") , System.Drawing.ContentAlig nment)
Me.btnSendATCommand.ImageI ndex = CType(resources.GetObject( "btnSendAT Command.Im ageIndex") , Integer)
Me.btnSendATCommand.ImeMod e = CType(resources.GetObject( "btnSendAT Command.Im eMode"), System.Windows.Forms.ImeMo de)
Me.btnSendATCommand.Locati on = CType(resources.GetObject( "btnSendAT Command.Lo cation"), System.Drawing.Point)
Me.btnSendATCommand.Name = "btnSendATCommand"
Me.btnSendATCommand.RightT oLeft = CType(resources.GetObject( "btnSendAT Command.Ri ghtToLeft" ), System.Windows.Forms.Right ToLeft)
Me.btnSendATCommand.Size = CType(resources.GetObject( "btnSendAT Command.Si ze"), System.Drawing.Size)
Me.btnSendATCommand.TabInd ex = CType(resources.GetObject( "btnSendAT Command.Ta bIndex"), Integer)
Me.btnSendATCommand.Text = resources.GetString("btnSe ndATComman d.Text")
Me.btnSendATCommand.TextAl ign = CType(resources.GetObject( "btnSendAT Command.Te xtAlign"), System.Drawing.ContentAlig nment)
Me.btnSendATCommand.Visibl e = CType(resources.GetObject( "btnSendAT Command.Vi sible"), Boolean)
'
'btnSendUserCommand
'
Me.btnSendUserCommand.Acce ssibleDesc ription = resources.GetString("btnSe ndUserComm and.Access ibleDescri ption")
Me.btnSendUserCommand.Acce ssibleName = resources.GetString("btnSe ndUserComm and.Access ibleName")
Me.btnSendUserCommand.Anch or = CType(resources.GetObject( "btnSendUs erCommand. Anchor"), System.Windows.Forms.Ancho rStyles)
Me.btnSendUserCommand.Back groundImag e = CType(resources.GetObject( "btnSendUs erCommand. Background Image"), System.Drawing.Image)
Me.btnSendUserCommand.Dock = CType(resources.GetObject( "btnSendUs erCommand. Dock"), System.Windows.Forms.DockS tyle)
Me.btnSendUserCommand.Enab led = CType(resources.GetObject( "btnSendUs erCommand. Enabled"), Boolean)
Me.btnSendUserCommand.Flat Style = CType(resources.GetObject( "btnSendUs erCommand. FlatStyle" ), System.Windows.Forms.FlatS tyle)
Me.btnSendUserCommand.Font = CType(resources.GetObject( "btnSendUs erCommand. Font"), System.Drawing.Font)
Me.btnSendUserCommand.Imag e = CType(resources.GetObject( "btnSendUs erCommand. Image"), System.Drawing.Image)
Me.btnSendUserCommand.Imag eAlign = CType(resources.GetObject( "btnSendUs erCommand. ImageAlign "), System.Drawing.ContentAlig nment)
Me.btnSendUserCommand.Imag eIndex = CType(resources.GetObject( "btnSendUs erCommand. ImageIndex "), Integer)
Me.btnSendUserCommand.ImeM ode = CType(resources.GetObject( "btnSendUs erCommand. ImeMode"), System.Windows.Forms.ImeMo de)
Me.btnSendUserCommand.Loca tion = CType(resources.GetObject( "btnSendUs erCommand. Location") , System.Drawing.Point)
Me.btnSendUserCommand.Name = "btnSendUserCommand"
Me.btnSendUserCommand.Righ tToLeft = CType(resources.GetObject( "btnSendUs erCommand. RightToLef t"), System.Windows.Forms.Right ToLeft)
Me.btnSendUserCommand.Size = CType(resources.GetObject( "btnSendUs erCommand. Size"), System.Drawing.Size)
Me.btnSendUserCommand.TabI ndex = CType(resources.GetObject( "btnSendUs erCommand. TabIndex") , Integer)
Me.btnSendUserCommand.Text = resources.GetString("btnSe ndUserComm and.Text")
Me.btnSendUserCommand.Text Align = CType(resources.GetObject( "btnSendUs erCommand. TextAlign" ), System.Drawing.ContentAlig nment)
Me.btnSendUserCommand.Visi ble = CType(resources.GetObject( "btnSendUs erCommand. Visible"), Boolean)
'
'txtUserCommand
'
Me.txtUserCommand.Accessib leDescript ion = resources.GetString("txtUs erCommand. Accessible Descriptio n")
Me.txtUserCommand.Accessib leName = resources.GetString("txtUs erCommand. Accessible Name")
Me.txtUserCommand.Anchor = CType(resources.GetObject( "txtUserCo mmand.Anch or"), System.Windows.Forms.Ancho rStyles)
Me.txtUserCommand.AutoSize = CType(resources.GetObject( "txtUserCo mmand.Auto Size"), Boolean)
Me.txtUserCommand.Backgrou ndImage = CType(resources.GetObject( "txtUserCo mmand.Back groundImag e"), System.Drawing.Image)
Me.txtUserCommand.Dock = CType(resources.GetObject( "txtUserCo mmand.Dock "), System.Windows.Forms.DockS tyle)
Me.txtUserCommand.Enabled = CType(resources.GetObject( "txtUserCo mmand.Enab led"), Boolean)
Me.txtUserCommand.Font = CType(resources.GetObject( "txtUserCo mmand.Font "), System.Drawing.Font)
Me.txtUserCommand.ImeMode = CType(resources.GetObject( "txtUserCo mmand.ImeM ode"), System.Windows.Forms.ImeMo de)
Me.txtUserCommand.Location = CType(resources.GetObject( "txtUserCo mmand.Loca tion"), System.Drawing.Point)
Me.txtUserCommand.MaxLengt h = CType(resources.GetObject( "txtUserCo mmand.MaxL ength"), Integer)
Me.txtUserCommand.Multilin e = CType(resources.GetObject( "txtUserCo mmand.Mult iline"), Boolean)
Me.txtUserCommand.Name = "txtUserCommand"
Me.txtUserCommand.Password Char = CType(resources.GetObject( "txtUserCo mmand.Pass wordChar") , Char)
Me.txtUserCommand.RightToL eft = CType(resources.GetObject( "txtUserCo mmand.Righ tToLeft"), System.Windows.Forms.Right ToLeft)
Me.txtUserCommand.ScrollBa rs = CType(resources.GetObject( "txtUserCo mmand.Scro llBars"), System.Windows.Forms.Scrol lBars)
Me.txtUserCommand.Size = CType(resources.GetObject( "txtUserCo mmand.Size "), System.Drawing.Size)
Me.txtUserCommand.TabIndex = CType(resources.GetObject( "txtUserCo mmand.TabI ndex"), Integer)
Me.txtUserCommand.Text = resources.GetString("txtUs erCommand. Text")
Me.txtUserCommand.TextAlig n = CType(resources.GetObject( "txtUserCo mmand.Text Align"), System.Windows.Forms.Horiz ontalAlign ment)
Me.txtUserCommand.Visible = CType(resources.GetObject( "txtUserCo mmand.Visi ble"), Boolean)
Me.txtUserCommand.WordWrap = CType(resources.GetObject( "txtUserCo mmand.Word Wrap"), Boolean)
'
'btnClear
'
Me.btnClear.AccessibleDesc ription = resources.GetString("btnCl ear.Access ibleDescri ption")
Me.btnClear.AccessibleName = resources.GetString("btnCl ear.Access ibleName")
Me.btnClear.Anchor = CType(resources.GetObject( "btnClear. Anchor"), System.Windows.Forms.Ancho rStyles)
Me.btnClear.BackgroundImag e = CType(resources.GetObject( "btnClear. Background Image"), System.Drawing.Image)
Me.btnClear.Dock = CType(resources.GetObject( "btnClear. Dock"), System.Windows.Forms.DockS tyle)
Me.btnClear.Enabled = CType(resources.GetObject( "btnClear. Enabled"), Boolean)
Me.btnClear.FlatStyle = CType(resources.GetObject( "btnClear. FlatStyle" ), System.Windows.Forms.FlatS tyle)
Me.btnClear.Font = CType(resources.GetObject( "btnClear. Font"), System.Drawing.Font)
Me.btnClear.Image = CType(resources.GetObject( "btnClear. Image"), System.Drawing.Image)
Me.btnClear.ImageAlign = CType(resources.GetObject( "btnClear. ImageAlign "), System.Drawing.ContentAlig nment)
Me.btnClear.ImageIndex = CType(resources.GetObject( "btnClear. ImageIndex "), Integer)
Me.btnClear.ImeMode = CType(resources.GetObject( "btnClear. ImeMode"), System.Windows.Forms.ImeMo de)
Me.btnClear.Location = CType(resources.GetObject( "btnClear. Location") , System.Drawing.Point)
Me.btnClear.Name = "btnClear"
Me.btnClear.RightToLeft = CType(resources.GetObject( "btnClear. RightToLef t"), System.Windows.Forms.Right ToLeft)
Me.btnClear.Size = CType(resources.GetObject( "btnClear. Size"), System.Drawing.Size)
Me.btnClear.TabIndex = CType(resources.GetObject( "btnClear. TabIndex") , Integer)
Me.btnClear.Text = resources.GetString("btnCl ear.Text")
Me.btnClear.TextAlign = CType(resources.GetObject( "btnClear. TextAlign" ), System.Drawing.ContentAlig nment)
Me.btnClear.Visible = CType(resources.GetObject( "btnClear. Visible"), Boolean)
'
'frmMain
'
Me.AccessibleDescription = CType(resources.GetObject( "$this.Acc essibleDes cription") , String)
Me.AccessibleName = CType(resources.GetObject( "$this.Acc essibleNam e"), String)
Me.Anchor = CType(resources.GetObject( "$this.Anc hor"), System.Windows.Forms.Ancho rStyles)
Me.AutoScaleBaseSize = CType(resources.GetObject( "$this.Aut oScaleBase Size"), System.Drawing.Size)
Me.AutoScroll = CType(resources.GetObject( "$this.Aut oScroll"), Boolean)
Me.AutoScrollMargin = CType(resources.GetObject( "$this.Aut oScrollMar gin"), System.Drawing.Size)
Me.AutoScrollMinSize = CType(resources.GetObject( "$this.Aut oScrollMin Size"), System.Drawing.Size)
Me.BackgroundImage = CType(resources.GetObject( "$this.Bac kgroundIma ge"), System.Drawing.Image)
Me.ClientSize = CType(resources.GetObject( "$this.Cli entSize"), System.Drawing.Size)
Me.Controls.AddRange(New System.Windows.Forms.Contr ol() {Me.btnClear, Me.txtUserCommand, Me.btnSendUserCommand, Me.btnSendATCommand, Me.txtSelectedModem, Me.btnCheckModems, Me.clstPorts, Me.txtStatus, Me.btnCheckForPorts})
Me.Dock = CType(resources.GetObject( "$this.Doc k"), System.Windows.Forms.DockS tyle)
Me.Enabled = CType(resources.GetObject( "$this.Ena bled"), Boolean)
Me.Font = CType(resources.GetObject( "$this.Fon t"), System.Drawing.Font)
Me.FormBorderStyle = System.Windows.Forms.FormB orderStyle .FixedSing le
Me.Icon = CType(resources.GetObject( "$this.Ico n"), System.Drawing.Icon)
Me.ImeMode = CType(resources.GetObject( "$this.Ime Mode"), System.Windows.Forms.ImeMo de)
Me.Location = CType(resources.GetObject( "$this.Loc ation"), System.Drawing.Point)
Me.MaximizeBox = False
Me.MaximumSize = CType(resources.GetObject( "$this.Max imumSize") , System.Drawing.Size)
Me.Menu = Me.mnuMain
Me.MinimumSize = CType(resources.GetObject( "$this.Min imumSize") , System.Drawing.Size)
Me.Name = "frmMain"
Me.RightToLeft = CType(resources.GetObject( "$this.Rig htToLeft") , System.Windows.Forms.Right ToLeft)
Me.StartPosition = CType(resources.GetObject( "$this.Sta rtPosition "), System.Windows.Forms.FormS tartPositi on)
Me.Text = resources.GetString("$this .Text")
Me.Visible = CType(resources.GetObject( "$this.Vis ible"), Boolean)
Me.ResumeLayout(False)
End Sub
#End Region
#Region " Standard Menu Code "
' <System.Diagnostics.Debugg erStepThro ugh()> has been added to some procedures since they are
' not the focus of the demo. Remove them if you wish to debug the procedures.
' This code simply shows the About form.
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub mnuAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAbout.Click
' Open the About form in Dialog Mode
Dim frm As New frmAbout()
frm.ShowDialog(Me)
frm.Dispose()
End Sub
' This code will close the form.
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
' Close the current form
Me.Close()
End Sub
#End Region
' This subroutine checks for available ports on the local machine. It does
' this by attempting to open ports 1 through 4.
Private Sub btnCheckForPorts_Click(ByV al sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckForPorts.Click
' Check for Availability of each of the 4 Comm Ports, and
' place a check in the list box items that have openable ports.
Dim i As Integer
For i = 1 To 4
WriteMessage("Testing COM" + i.ToString())
If IsPortAvailable(i) Then
' Check the box for available ports.
Me.clstPorts.SetItemChecke d(i - 1, True)
Else
' Uncheck the box for unavailable ports.
Me.clstPorts.SetItemChecke d(i - 1, False)
End If
Next
' Enable the Find Modems button.
Me.btnCheckModems.Enabled = True
End Sub
' This subroutine attempts to send an AT command to any active Comm Ports.
' If a response is returned then a usable modem has been detected
' on that port.
Private Sub btnCheckModems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckModems.Click
Dim i As Integer
For i = 0 To 3
If Me.clstPorts.GetItemChecke d(i) Then
' Item is checked so it MIGHT be a valid port.
' Test for validity.
If IsPortAvailable(i + 1) Then
' Check if port responds to an AT command.
If IsPortAModem(i + 1) Then
' Set the class variables to the last modem found.
Me.m_IsModemFound = True
Me.m_ModemPort = i + 1
' Write message to the user.
WriteMessage("Port " + (i + 1).ToString() + _
" is a responsive modem.")
Else
' Write message to the user.
WriteMessage("Port " + (i + 1).ToString() + _
" is not a responsive modem.")
End If
End If
End If
Next
' If a modem was found, enable the rest of the buttons, so the user
' can interact with the modem.
If Me.m_IsModemFound Then
Me.txtSelectedModem.Text = "Using Modem on COM" + _
Me.m_ModemPort.ToString()
Me.txtUserCommand.Enabled = True
Me.btnSendATCommand.Enable d = True
Me.btnSendUserCommand.Enab led = True
End If
End Sub
' This subroutine clears the TextBox.
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
Me.txtStatus.Clear()
End Sub
' This subroutine sends an AT command to the modem, and records its response.
' It depends on the timer to do the reading of the response.
Private Sub btnSendATCommand_Click(ByV al sender As System.Object, ByVal e As System.EventArgs) Handles btnSendATCommand.Click
' Always wrap up working with Comm Ports in exception handlers.
Try
' Enable the timer.
Me.tmrReadCommPort.Enabled = True
' Attempt to open the port.
m_CommPort.Open(m_ModemPor t, 115200, 8, Rs232.DataParity.Parity_No ne, _
Rs232.DataStopBit.StopBit_ 1, 4096)
' Write an AT Command to the Port.
m_CommPort.Write(Encoding. ASCII.GetB ytes("AT" & Chr(13)))
' Sleep long enough for the modem to respond and the timer to fire.
System.Threading.Thread.Sl eep(200)
Application.DoEvents()
m_CommPort.Close()
Catch ex As Exception
' Warn the user.
MessageBox.Show("Unable to communicate with Modem")
Finally
' Disable the timer.
Me.tmrReadCommPort.Enabled = False
End Try
End Sub
' This subroutine sends a user specified command to the modem, and records its
' response. It depends on the timer to do the reading of the response.
Private Sub btnSendUserCommand_Click(B yVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendUserCommand.Click
' Always wrap up working with Comm Ports in exception handlers.
Try
' Enable the timer.
Me.tmrReadCommPort.Enabled = True
' Attempt to open the port.
m_CommPort.Open(m_ModemPor t, 115200, 8, Rs232.DataParity.Parity_No ne, Rs232.DataStopBit.StopBit_ 1, 4096)
' Write an user specified Command to the Port.
m_CommPort.Write(Encoding. ASCII.GetB ytes(Me.tx tUserComma nd.Text & Chr(13)))
' Sleep long enough for the modem to respond and the timer to fire.
System.Threading.Thread.Sl eep(200)
Application.DoEvents()
m_CommPort.Close()
Catch ex As Exception
' Warn the user.
MessageBox.Show("Unable to communicate with Modem")
Finally
' Disable the timer.
Me.tmrReadCommPort.Enabled = False
End Try
End Sub
' This subroutine is fired when the timer event is raised. It writes whatever
' is in the Comm Port buffer to the output window.
Private Sub tmrReadCommPort_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrReadCommPort.Tick
Try
' As long as there is information, read one byte at a time and
' output it.
While (m_CommPort.Read(1) <> -1)
' Write the output to the screen.
WriteMessage(Chr(m_CommPor t.InputStr eam(0)), False)
End While
Catch exc As Exception
' An exception is raised when there is no information to read.
' Don't do anything here, just let the exception go.
End Try
End Sub
' This function checks to see if the port is a modem, by sending
' an AT command to the port. If the port responds, it is assumed to
' be a modem. The function returns a Boolean.
Private Function IsPortAModem(ByVal port As Integer) As Boolean
' Always wrap up working with Comm Ports in exception handlers.
Try
' Attempt to open the port.
m_CommPort.Open(port, 115200, 8, Rs232.DataParity.Parity_No ne, _
Rs232.DataStopBit.StopBit_ 1, 4096)
' Write an AT Command to the Port.
m_CommPort.Write(Encoding. ASCII.GetB ytes("AT" & Chr(13)))
' Sleep long enough for the modem to respond.
System.Threading.Thread.Sl eep(200)
Application.DoEvents()
' Try to get info from the Comm Port.
Try
Dim b As Byte
' Try to read a single byte. If you get it, then assume
' that the port contains a modem. Clear the buffer before
' leaving.
m_CommPort.Read(1)
m_CommPort.ClearInputBuffe r()
m_CommPort.Close()
Return True
Catch exc As Exception
' Nothing to read from the Comm Port, so set to False
m_CommPort.Close()
Return False
End Try
Catch exc As Exception
' Port could not be opened or written to.
Me.clstPorts.SetItemChecke d(port - 1, False)
MsgBox("Could not open port.", MsgBoxStyle.OKOnly, Me.Text)
Return False
End Try
End Function
' This function attempts to open the passed Comm Port. If it is
' available, it returns True, else it returns False. To determine
' availability a Try-Catch block is used.
Private Function IsPortAvailable(ByVal ComPort As Integer) As Boolean
Try
m_CommPort.Open(ComPort, 115200, 8, Rs232.DataParity.Parity_No ne, _
Rs232.DataStopBit.StopBit_ 1, 4096)
' If it makes it to here, then the Comm Port is available.
m_CommPort.Close()
Return True
Catch
' If it gets here, then the attempt to open the Comm Port
' was unsuccessful.
Return False
End Try
End Function
' This subroutine writes a message to the txtStatus TextBox.
Private Sub WriteMessage(ByVal message As String)
Me.txtStatus.Text += message + vbCrLf
End Sub
' This subroutine writes a message to the txtStatus TextBox and allows
' the line feed to be suppressed.
Private Sub WriteMessage(ByVal message As String, ByVal linefeed As Boolean)
Me.txtStatus.Text += message
If linefeed Then
Me.txtStatus.Text += vbCrLf
End If
End Sub
End Class
Imports System.Text
Public Class frmMain
Inherits System.Windows.Forms.Form
' Declare necessary class variables.
Private m_CommPort As New Rs232()
Private m_IsModemFound As Boolean = False
Private m_ModemPort As Integer = 0
#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
' So that we only need to set the title of the application once,
' we use the AssemblyInfo class (defined in the AssemblyInfo.vb file)
' to read the AssemblyTitle attribute.
Dim ainfo As New AssemblyInfo()
Me.Text = ainfo.Title
Me.mnuAbout.Text = String.Format("&About {0} ...", ainfo.Title)
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.ICon
'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 mnuMain As System.Windows.Forms.MainM
Friend WithEvents mnuFile As System.Windows.Forms.MenuI
Friend WithEvents mnuExit As System.Windows.Forms.MenuI
Friend WithEvents mnuHelp As System.Windows.Forms.MenuI
Friend WithEvents mnuAbout As System.Windows.Forms.MenuI
Friend WithEvents txtStatus As System.Windows.Forms.TextB
Friend WithEvents tmrReadCommPort As System.Windows.Forms.Timer
Friend WithEvents clstPorts As System.Windows.Forms.Check
Friend WithEvents btnCheckForPorts As System.Windows.Forms.Butto
Friend WithEvents btnCheckModems As System.Windows.Forms.Butto
Friend WithEvents txtSelectedModem As System.Windows.Forms.TextB
Friend WithEvents btnSendATCommand As System.Windows.Forms.Butto
Friend WithEvents txtUserCommand As System.Windows.Forms.TextB
Friend WithEvents btnSendUserCommand As System.Windows.Forms.Butto
Friend WithEvents btnClear As System.Windows.Forms.Butto
<System.Diagnostics.Debugg
Me.components = New System.ComponentModel.Cont
Dim resources As System.Resources.ResourceM
Me.mnuMain = New System.Windows.Forms.MainM
Me.mnuFile = New System.Windows.Forms.MenuI
Me.mnuExit = New System.Windows.Forms.MenuI
Me.mnuHelp = New System.Windows.Forms.MenuI
Me.mnuAbout = New System.Windows.Forms.MenuI
Me.btnCheckForPorts = New System.Windows.Forms.Butto
Me.txtStatus = New System.Windows.Forms.TextB
Me.tmrReadCommPort = New System.Windows.Forms.Timer
Me.clstPorts = New System.Windows.Forms.Check
Me.btnCheckModems = New System.Windows.Forms.Butto
Me.txtSelectedModem = New System.Windows.Forms.TextB
Me.btnSendATCommand = New System.Windows.Forms.Butto
Me.btnSendUserCommand = New System.Windows.Forms.Butto
Me.txtUserCommand = New System.Windows.Forms.TextB
Me.btnClear = New System.Windows.Forms.Butto
Me.SuspendLayout()
'
'mnuMain
'
Me.mnuMain.MenuItems.AddRa
Me.mnuMain.RightToLeft = CType(resources.GetObject(
'
'mnuFile
'
Me.mnuFile.Enabled = CType(resources.GetObject(
Me.mnuFile.Index = 0
Me.mnuFile.MenuItems.AddRa
Me.mnuFile.Shortcut = CType(resources.GetObject(
Me.mnuFile.ShowShortcut = CType(resources.GetObject(
Me.mnuFile.Text = resources.GetString("mnuFi
Me.mnuFile.Visible = CType(resources.GetObject(
'
'mnuExit
'
Me.mnuExit.Enabled = CType(resources.GetObject(
Me.mnuExit.Index = 0
Me.mnuExit.Shortcut = CType(resources.GetObject(
Me.mnuExit.ShowShortcut = CType(resources.GetObject(
Me.mnuExit.Text = resources.GetString("mnuEx
Me.mnuExit.Visible = CType(resources.GetObject(
'
'mnuHelp
'
Me.mnuHelp.Enabled = CType(resources.GetObject(
Me.mnuHelp.Index = 1
Me.mnuHelp.MenuItems.AddRa
Me.mnuHelp.Shortcut = CType(resources.GetObject(
Me.mnuHelp.ShowShortcut = CType(resources.GetObject(
Me.mnuHelp.Text = resources.GetString("mnuHe
Me.mnuHelp.Visible = CType(resources.GetObject(
'
'mnuAbout
'
Me.mnuAbout.Enabled = CType(resources.GetObject(
Me.mnuAbout.Index = 0
Me.mnuAbout.Shortcut = CType(resources.GetObject(
Me.mnuAbout.ShowShortcut = CType(resources.GetObject(
Me.mnuAbout.Text = resources.GetString("mnuAb
Me.mnuAbout.Visible = CType(resources.GetObject(
'
'btnCheckForPorts
'
Me.btnCheckForPorts.Access
Me.btnCheckForPorts.Access
Me.btnCheckForPorts.Anchor
Me.btnCheckForPorts.Backgr
Me.btnCheckForPorts.Dock = CType(resources.GetObject(
Me.btnCheckForPorts.Enable
Me.btnCheckForPorts.FlatSt
Me.btnCheckForPorts.Font = CType(resources.GetObject(
Me.btnCheckForPorts.Image = CType(resources.GetObject(
Me.btnCheckForPorts.ImageA
Me.btnCheckForPorts.ImageI
Me.btnCheckForPorts.ImeMod
Me.btnCheckForPorts.Locati
Me.btnCheckForPorts.Name = "btnCheckForPorts"
Me.btnCheckForPorts.RightT
Me.btnCheckForPorts.Size = CType(resources.GetObject(
Me.btnCheckForPorts.TabInd
Me.btnCheckForPorts.Text = resources.GetString("btnCh
Me.btnCheckForPorts.TextAl
Me.btnCheckForPorts.Visibl
'
'txtStatus
'
Me.txtStatus.AccessibleDes
Me.txtStatus.AccessibleNam
Me.txtStatus.Anchor = CType(resources.GetObject(
Me.txtStatus.AutoSize = CType(resources.GetObject(
Me.txtStatus.BackgroundIma
Me.txtStatus.Dock = CType(resources.GetObject(
Me.txtStatus.Enabled = CType(resources.GetObject(
Me.txtStatus.Font = CType(resources.GetObject(
Me.txtStatus.ImeMode = CType(resources.GetObject(
Me.txtStatus.Location = CType(resources.GetObject(
Me.txtStatus.MaxLength = CType(resources.GetObject(
Me.txtStatus.Multiline = CType(resources.GetObject(
Me.txtStatus.Name = "txtStatus"
Me.txtStatus.PasswordChar = CType(resources.GetObject(
Me.txtStatus.ReadOnly = True
Me.txtStatus.RightToLeft = CType(resources.GetObject(
Me.txtStatus.ScrollBars = CType(resources.GetObject(
Me.txtStatus.Size = CType(resources.GetObject(
Me.txtStatus.TabIndex = CType(resources.GetObject(
Me.txtStatus.Text = resources.GetString("txtSt
Me.txtStatus.TextAlign = CType(resources.GetObject(
Me.txtStatus.Visible = CType(resources.GetObject(
Me.txtStatus.WordWrap = CType(resources.GetObject(
'
'tmrReadCommPort
'
'
'clstPorts
'
Me.clstPorts.AccessibleDes
Me.clstPorts.AccessibleNam
Me.clstPorts.Anchor = CType(resources.GetObject(
Me.clstPorts.BackgroundIma
Me.clstPorts.ColumnWidth = CType(resources.GetObject(
Me.clstPorts.Dock = CType(resources.GetObject(
Me.clstPorts.Enabled = CType(resources.GetObject(
Me.clstPorts.Font = CType(resources.GetObject(
Me.clstPorts.HorizontalExt
Me.clstPorts.HorizontalScr
Me.clstPorts.ImeMode = CType(resources.GetObject(
Me.clstPorts.IntegralHeigh
Me.clstPorts.Items.AddRang
Me.clstPorts.Location = CType(resources.GetObject(
Me.clstPorts.Name = "clstPorts"
Me.clstPorts.RightToLeft = CType(resources.GetObject(
Me.clstPorts.ScrollAlwaysV
Me.clstPorts.Size = CType(resources.GetObject(
Me.clstPorts.TabIndex = CType(resources.GetObject(
Me.clstPorts.TabStop = False
Me.clstPorts.Visible = CType(resources.GetObject(
'
'btnCheckModems
'
Me.btnCheckModems.Accessib
Me.btnCheckModems.Accessib
Me.btnCheckModems.Anchor = CType(resources.GetObject(
Me.btnCheckModems.Backgrou
Me.btnCheckModems.Dock = CType(resources.GetObject(
Me.btnCheckModems.Enabled = CType(resources.GetObject(
Me.btnCheckModems.FlatStyl
Me.btnCheckModems.Font = CType(resources.GetObject(
Me.btnCheckModems.Image = CType(resources.GetObject(
Me.btnCheckModems.ImageAli
Me.btnCheckModems.ImageInd
Me.btnCheckModems.ImeMode = CType(resources.GetObject(
Me.btnCheckModems.Location
Me.btnCheckModems.Name = "btnCheckModems"
Me.btnCheckModems.RightToL
Me.btnCheckModems.Size = CType(resources.GetObject(
Me.btnCheckModems.TabIndex
Me.btnCheckModems.Text = resources.GetString("btnCh
Me.btnCheckModems.TextAlig
Me.btnCheckModems.Visible = CType(resources.GetObject(
'
'txtSelectedModem
'
Me.txtSelectedModem.Access
Me.txtSelectedModem.Access
Me.txtSelectedModem.Anchor
Me.txtSelectedModem.AutoSi
Me.txtSelectedModem.Backgr
Me.txtSelectedModem.Dock = CType(resources.GetObject(
Me.txtSelectedModem.Enable
Me.txtSelectedModem.Font = CType(resources.GetObject(
Me.txtSelectedModem.ImeMod
Me.txtSelectedModem.Locati
Me.txtSelectedModem.MaxLen
Me.txtSelectedModem.Multil
Me.txtSelectedModem.Name = "txtSelectedModem"
Me.txtSelectedModem.Passwo
Me.txtSelectedModem.RightT
Me.txtSelectedModem.Scroll
Me.txtSelectedModem.Size = CType(resources.GetObject(
Me.txtSelectedModem.TabInd
Me.txtSelectedModem.Text = resources.GetString("txtSe
Me.txtSelectedModem.TextAl
Me.txtSelectedModem.Visibl
Me.txtSelectedModem.WordWr
'
'btnSendATCommand
'
Me.btnSendATCommand.Access
Me.btnSendATCommand.Access
Me.btnSendATCommand.Anchor
Me.btnSendATCommand.Backgr
Me.btnSendATCommand.Dock = CType(resources.GetObject(
Me.btnSendATCommand.Enable
Me.btnSendATCommand.FlatSt
Me.btnSendATCommand.Font = CType(resources.GetObject(
Me.btnSendATCommand.Image = CType(resources.GetObject(
Me.btnSendATCommand.ImageA
Me.btnSendATCommand.ImageI
Me.btnSendATCommand.ImeMod
Me.btnSendATCommand.Locati
Me.btnSendATCommand.Name = "btnSendATCommand"
Me.btnSendATCommand.RightT
Me.btnSendATCommand.Size = CType(resources.GetObject(
Me.btnSendATCommand.TabInd
Me.btnSendATCommand.Text = resources.GetString("btnSe
Me.btnSendATCommand.TextAl
Me.btnSendATCommand.Visibl
'
'btnSendUserCommand
'
Me.btnSendUserCommand.Acce
Me.btnSendUserCommand.Acce
Me.btnSendUserCommand.Anch
Me.btnSendUserCommand.Back
Me.btnSendUserCommand.Dock
Me.btnSendUserCommand.Enab
Me.btnSendUserCommand.Flat
Me.btnSendUserCommand.Font
Me.btnSendUserCommand.Imag
Me.btnSendUserCommand.Imag
Me.btnSendUserCommand.Imag
Me.btnSendUserCommand.ImeM
Me.btnSendUserCommand.Loca
Me.btnSendUserCommand.Name
Me.btnSendUserCommand.Righ
Me.btnSendUserCommand.Size
Me.btnSendUserCommand.TabI
Me.btnSendUserCommand.Text
Me.btnSendUserCommand.Text
Me.btnSendUserCommand.Visi
'
'txtUserCommand
'
Me.txtUserCommand.Accessib
Me.txtUserCommand.Accessib
Me.txtUserCommand.Anchor = CType(resources.GetObject(
Me.txtUserCommand.AutoSize
Me.txtUserCommand.Backgrou
Me.txtUserCommand.Dock = CType(resources.GetObject(
Me.txtUserCommand.Enabled = CType(resources.GetObject(
Me.txtUserCommand.Font = CType(resources.GetObject(
Me.txtUserCommand.ImeMode = CType(resources.GetObject(
Me.txtUserCommand.Location
Me.txtUserCommand.MaxLengt
Me.txtUserCommand.Multilin
Me.txtUserCommand.Name = "txtUserCommand"
Me.txtUserCommand.Password
Me.txtUserCommand.RightToL
Me.txtUserCommand.ScrollBa
Me.txtUserCommand.Size = CType(resources.GetObject(
Me.txtUserCommand.TabIndex
Me.txtUserCommand.Text = resources.GetString("txtUs
Me.txtUserCommand.TextAlig
Me.txtUserCommand.Visible = CType(resources.GetObject(
Me.txtUserCommand.WordWrap
'
'btnClear
'
Me.btnClear.AccessibleDesc
Me.btnClear.AccessibleName
Me.btnClear.Anchor = CType(resources.GetObject(
Me.btnClear.BackgroundImag
Me.btnClear.Dock = CType(resources.GetObject(
Me.btnClear.Enabled = CType(resources.GetObject(
Me.btnClear.FlatStyle = CType(resources.GetObject(
Me.btnClear.Font = CType(resources.GetObject(
Me.btnClear.Image = CType(resources.GetObject(
Me.btnClear.ImageAlign = CType(resources.GetObject(
Me.btnClear.ImageIndex = CType(resources.GetObject(
Me.btnClear.ImeMode = CType(resources.GetObject(
Me.btnClear.Location = CType(resources.GetObject(
Me.btnClear.Name = "btnClear"
Me.btnClear.RightToLeft = CType(resources.GetObject(
Me.btnClear.Size = CType(resources.GetObject(
Me.btnClear.TabIndex = CType(resources.GetObject(
Me.btnClear.Text = resources.GetString("btnCl
Me.btnClear.TextAlign = CType(resources.GetObject(
Me.btnClear.Visible = CType(resources.GetObject(
'
'frmMain
'
Me.AccessibleDescription = CType(resources.GetObject(
Me.AccessibleName = CType(resources.GetObject(
Me.Anchor = CType(resources.GetObject(
Me.AutoScaleBaseSize = CType(resources.GetObject(
Me.AutoScroll = CType(resources.GetObject(
Me.AutoScrollMargin = CType(resources.GetObject(
Me.AutoScrollMinSize = CType(resources.GetObject(
Me.BackgroundImage = CType(resources.GetObject(
Me.ClientSize = CType(resources.GetObject(
Me.Controls.AddRange(New System.Windows.Forms.Contr
Me.Dock = CType(resources.GetObject(
Me.Enabled = CType(resources.GetObject(
Me.Font = CType(resources.GetObject(
Me.FormBorderStyle = System.Windows.Forms.FormB
Me.Icon = CType(resources.GetObject(
Me.ImeMode = CType(resources.GetObject(
Me.Location = CType(resources.GetObject(
Me.MaximizeBox = False
Me.MaximumSize = CType(resources.GetObject(
Me.Menu = Me.mnuMain
Me.MinimumSize = CType(resources.GetObject(
Me.Name = "frmMain"
Me.RightToLeft = CType(resources.GetObject(
Me.StartPosition = CType(resources.GetObject(
Me.Text = resources.GetString("$this
Me.Visible = CType(resources.GetObject(
Me.ResumeLayout(False)
End Sub
#End Region
#Region " Standard Menu Code "
' <System.Diagnostics.Debugg
' not the focus of the demo. Remove them if you wish to debug the procedures.
' This code simply shows the About form.
<System.Diagnostics.Debugg
' Open the About form in Dialog Mode
Dim frm As New frmAbout()
frm.ShowDialog(Me)
frm.Dispose()
End Sub
' This code will close the form.
<System.Diagnostics.Debugg
' Close the current form
Me.Close()
End Sub
#End Region
' This subroutine checks for available ports on the local machine. It does
' this by attempting to open ports 1 through 4.
Private Sub btnCheckForPorts_Click(ByV
' Check for Availability of each of the 4 Comm Ports, and
' place a check in the list box items that have openable ports.
Dim i As Integer
For i = 1 To 4
WriteMessage("Testing COM" + i.ToString())
If IsPortAvailable(i) Then
' Check the box for available ports.
Me.clstPorts.SetItemChecke
Else
' Uncheck the box for unavailable ports.
Me.clstPorts.SetItemChecke
End If
Next
' Enable the Find Modems button.
Me.btnCheckModems.Enabled = True
End Sub
' This subroutine attempts to send an AT command to any active Comm Ports.
' If a response is returned then a usable modem has been detected
' on that port.
Private Sub btnCheckModems_Click(ByVal
Dim i As Integer
For i = 0 To 3
If Me.clstPorts.GetItemChecke
' Item is checked so it MIGHT be a valid port.
' Test for validity.
If IsPortAvailable(i + 1) Then
' Check if port responds to an AT command.
If IsPortAModem(i + 1) Then
' Set the class variables to the last modem found.
Me.m_IsModemFound = True
Me.m_ModemPort = i + 1
' Write message to the user.
WriteMessage("Port " + (i + 1).ToString() + _
" is a responsive modem.")
Else
' Write message to the user.
WriteMessage("Port " + (i + 1).ToString() + _
" is not a responsive modem.")
End If
End If
End If
Next
' If a modem was found, enable the rest of the buttons, so the user
' can interact with the modem.
If Me.m_IsModemFound Then
Me.txtSelectedModem.Text = "Using Modem on COM" + _
Me.m_ModemPort.ToString()
Me.txtUserCommand.Enabled = True
Me.btnSendATCommand.Enable
Me.btnSendUserCommand.Enab
End If
End Sub
' This subroutine clears the TextBox.
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
Me.txtStatus.Clear()
End Sub
' This subroutine sends an AT command to the modem, and records its response.
' It depends on the timer to do the reading of the response.
Private Sub btnSendATCommand_Click(ByV
' Always wrap up working with Comm Ports in exception handlers.
Try
' Enable the timer.
Me.tmrReadCommPort.Enabled
' Attempt to open the port.
m_CommPort.Open(m_ModemPor
Rs232.DataStopBit.StopBit_
' Write an AT Command to the Port.
m_CommPort.Write(Encoding.
' Sleep long enough for the modem to respond and the timer to fire.
System.Threading.Thread.Sl
Application.DoEvents()
m_CommPort.Close()
Catch ex As Exception
' Warn the user.
MessageBox.Show("Unable to communicate with Modem")
Finally
' Disable the timer.
Me.tmrReadCommPort.Enabled
End Try
End Sub
' This subroutine sends a user specified command to the modem, and records its
' response. It depends on the timer to do the reading of the response.
Private Sub btnSendUserCommand_Click(B
' Always wrap up working with Comm Ports in exception handlers.
Try
' Enable the timer.
Me.tmrReadCommPort.Enabled
' Attempt to open the port.
m_CommPort.Open(m_ModemPor
' Write an user specified Command to the Port.
m_CommPort.Write(Encoding.
' Sleep long enough for the modem to respond and the timer to fire.
System.Threading.Thread.Sl
Application.DoEvents()
m_CommPort.Close()
Catch ex As Exception
' Warn the user.
MessageBox.Show("Unable to communicate with Modem")
Finally
' Disable the timer.
Me.tmrReadCommPort.Enabled
End Try
End Sub
' This subroutine is fired when the timer event is raised. It writes whatever
' is in the Comm Port buffer to the output window.
Private Sub tmrReadCommPort_Tick(ByVal
Try
' As long as there is information, read one byte at a time and
' output it.
While (m_CommPort.Read(1) <> -1)
' Write the output to the screen.
WriteMessage(Chr(m_CommPor
End While
Catch exc As Exception
' An exception is raised when there is no information to read.
' Don't do anything here, just let the exception go.
End Try
End Sub
' This function checks to see if the port is a modem, by sending
' an AT command to the port. If the port responds, it is assumed to
' be a modem. The function returns a Boolean.
Private Function IsPortAModem(ByVal port As Integer) As Boolean
' Always wrap up working with Comm Ports in exception handlers.
Try
' Attempt to open the port.
m_CommPort.Open(port, 115200, 8, Rs232.DataParity.Parity_No
Rs232.DataStopBit.StopBit_
' Write an AT Command to the Port.
m_CommPort.Write(Encoding.
' Sleep long enough for the modem to respond.
System.Threading.Thread.Sl
Application.DoEvents()
' Try to get info from the Comm Port.
Try
Dim b As Byte
' Try to read a single byte. If you get it, then assume
' that the port contains a modem. Clear the buffer before
' leaving.
m_CommPort.Read(1)
m_CommPort.ClearInputBuffe
m_CommPort.Close()
Return True
Catch exc As Exception
' Nothing to read from the Comm Port, so set to False
m_CommPort.Close()
Return False
End Try
Catch exc As Exception
' Port could not be opened or written to.
Me.clstPorts.SetItemChecke
MsgBox("Could not open port.", MsgBoxStyle.OKOnly, Me.Text)
Return False
End Try
End Function
' This function attempts to open the passed Comm Port. If it is
' available, it returns True, else it returns False. To determine
' availability a Try-Catch block is used.
Private Function IsPortAvailable(ByVal ComPort As Integer) As Boolean
Try
m_CommPort.Open(ComPort, 115200, 8, Rs232.DataParity.Parity_No
Rs232.DataStopBit.StopBit_
' If it makes it to here, then the Comm Port is available.
m_CommPort.Close()
Return True
Catch
' If it gets here, then the attempt to open the Comm Port
' was unsuccessful.
Return False
End Try
End Function
' This subroutine writes a message to the txtStatus TextBox.
Private Sub WriteMessage(ByVal message As String)
Me.txtStatus.Text += message + vbCrLf
End Sub
' This subroutine writes a message to the txtStatus TextBox and allows
' the line feed to be suppressed.
Private Sub WriteMessage(ByVal message As String, ByVal linefeed As Boolean)
Me.txtStatus.Text += message
If linefeed Then
Me.txtStatus.Text += vbCrLf
End If
End Sub
End Class
I can provide you links to information but full code is against the rules of EE
http://www.mentalis.org/classlib/class.php?id=15
http://www.htservices.com/Tools/VBandC/SerialCommunications.htm