⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 如何手工注册MSComm控件及其串口编程详解 - _NET与管理信息系统 -
💻 VB
📖 第 1 页 / 共 2 页
字号:
        Me.GroupBox1.Controls.Add(Me.Label4)
        Me.GroupBox1.Controls.Add(Me.txtBaudrate)
        Me.GroupBox1.Controls.Add(Me.Label1)
        Me.GroupBox1.Controls.Add(Me.optCom2)
        Me.GroupBox1.Controls.Add(Me.optCom1)
        Me.GroupBox1.Location = New System.Drawing.Point(7, 11)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(198, 99)
        Me.GroupBox1.TabIndex = 0
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "COM Setup"
        '
        'Label8
        '
        Me.Label8.Location = New System.Drawing.Point(132, 20)
        Me.Label8.Name = "Label8"
        Me.Label8.Size = New System.Drawing.Size(58, 14)
        Me.Label8.TabIndex = 8
        Me.Label8.Text = "Port check"
        '
        'btnExit
        '
        Me.btnExit.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnExit.Location = New System.Drawing.Point(251, 488)
        Me.btnExit.Name = "btnExit"
        Me.btnExit.Size = New System.Drawing.Size(61, 23)
        Me.btnExit.TabIndex = 14
        Me.btnExit.Text = "&Close"
        '
        'linkAuthor
        '
        Me.linkAuthor.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.linkAuthor.Location = New System.Drawing.Point(7, 496)
        Me.linkAuthor.Name = "linkAuthor"
        Me.linkAuthor.Size = New System.Drawing.Size(81, 12)
        Me.linkAuthor.TabIndex = 15
        Me.linkAuthor.TabStop = True
        Me.linkAuthor.Text = "Contact Author"
        '
        'lbHex
        '
        Me.lbHex.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lbHex.Location = New System.Drawing.Point(8, 242)
        Me.lbHex.Name = "lbHex"
        Me.lbHex.Size = New System.Drawing.Size(305, 41)
        Me.lbHex.TabIndex = 16
        '
        'Label6
        '
        Me.Label6.Location = New System.Drawing.Point(8, 226)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(114, 14)
        Me.Label6.TabIndex = 17
        Me.Label6.Text = "Received Data (Hex)"
        '
        'cboStatusLine
        '
        Me.cboStatusLine.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboStatusLine.Location = New System.Drawing.Point(83, 304)
        Me.cboStatusLine.Name = "cboStatusLine"
        Me.cboStatusLine.Size = New System.Drawing.Size(97, 21)
        Me.cboStatusLine.Sorted = True
        Me.cboStatusLine.TabIndex = 18
        '
        'btnCheck
        '
        Me.btnCheck.Enabled = False
        Me.btnCheck.Location = New System.Drawing.Point(185, 306)
        Me.btnCheck.Name = "btnCheck"
        Me.btnCheck.Size = New System.Drawing.Size(43, 19)
        Me.btnCheck.TabIndex = 19
        Me.btnCheck.Text = "Check"
        '
        'Label7
        '
        Me.Label7.Location = New System.Drawing.Point(84, 288)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(82, 14)
        Me.Label7.TabIndex = 20
        Me.Label7.Text = "Status line"
        '
        'lblAsync
        '
        Me.lblAsync.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lblAsync.Location = New System.Drawing.Point(7, 411)
        Me.lblAsync.Name = "lblAsync"
        Me.lblAsync.Size = New System.Drawing.Size(82, 14)
        Me.lblAsync.TabIndex = 26
        Me.lblAsync.Text = "Async flow"
        '
        'LinkLabel1
        '
        Me.LinkLabel1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.LinkLabel1.Location = New System.Drawing.Point(119, 496)
        Me.LinkLabel1.Name = "LinkLabel1"
        Me.LinkLabel1.Size = New System.Drawing.Size(99, 12)
        Me.LinkLabel1.TabIndex = 30
        Me.LinkLabel1.TabStop = True
        Me.LinkLabel1.Text = "www.codeworks.it"
        '
        'btnInBuffer
        '
        Me.btnInBuffer.Location = New System.Drawing.Point(8, 371)
        Me.btnInBuffer.Name = "btnInBuffer"
        Me.btnInBuffer.Size = New System.Drawing.Size(151, 23)
        Me.btnInBuffer.TabIndex = 32
        Me.btnInBuffer.Text = "Bytes on Rx buffer"
        '
        'lblInBuffer
        '
        Me.lblInBuffer.BackColor = System.Drawing.Color.Khaki
        Me.lblInBuffer.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lblInBuffer.Location = New System.Drawing.Point(170, 372)
        Me.lblInBuffer.Name = "lblInBuffer"
        Me.lblInBuffer.Size = New System.Drawing.Size(139, 19)
        Me.lblInBuffer.TabIndex = 33
        Me.lblInBuffer.Text = "?"
        Me.lblInBuffer.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14)
        Me.ClientSize = New System.Drawing.Size(319, 514)
        Me.Controls.Add(Me.lblInBuffer)
        Me.Controls.Add(Me.btnInBuffer)
        Me.Controls.Add(Me.chkAddCR)
        Me.Controls.Add(Me.LinkLabel1)
        Me.Controls.Add(Me.chkEvents)
        Me.Controls.Add(Me.lblAsync)
        Me.Controls.Add(Me.lbAsync)
        Me.Controls.Add(Me.txtBytes2Read)
        Me.Controls.Add(Me.Label7)
        Me.Controls.Add(Me.btnCheck)
        Me.Controls.Add(Me.cboStatusLine)
        Me.Controls.Add(Me.Label6)
        Me.Controls.Add(Me.lbHex)
        Me.Controls.Add(Me.linkAuthor)
        Me.Controls.Add(Me.btnExit)
        Me.Controls.Add(Me.chkDTR)
        Me.Controls.Add(Me.chkRTS)
        Me.Controls.Add(Me.chkAutorx)
        Me.Controls.Add(Me.GroupBox1)
        Me.Controls.Add(Me.Label5)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.btnRx)
        Me.Controls.Add(Me.txtRx)
        Me.Controls.Add(Me.txtTx)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.btnTx)
        Me.Controls.Add(Me.btnCloseCom)
        Me.Controls.Add(Me.btnOpenCom)
        Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MaximizeBox = False
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "VB.Net Serial comunication example"
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region




	Private Sub btnOpenCom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenCom.Click
		moRS232 = New Rs232()
		Try
			'// Setup parameters
			With moRS232
				.Port = miComPort
				.BaudRate = Int32.Parse(txtBaudrate.Text)
            .DataBit = 8
            .StopBit = Rs232.DataStopBit.StopBit_1
            .Parity = Rs232.DataParity.Parity_None
				.Timeout = Int32.Parse(txtTimeout.Text)
         End With
         '// Initializes port
         moRS232.Open()
         '// Set state of RTS / DTS
         moRS232.Dtr = (chkDTR.CheckState = CheckState.Checked)
         moRS232.Rts = (chkRTS.CheckState = CheckState.Checked)
         If chkEvents.Checked Then moRS232.EnableEvents()
         chkEvents.Enabled = True
      Catch Ex As Exception
			MessageBox.Show(Ex.Message, "Connection Error", MessageBoxButtons.OK)
		Finally
			btnCloseCom.Enabled = moRS232.IsOpen
			btnOpenCom.Enabled = Not moRS232.IsOpen
			btnTx.Enabled = moRS232.IsOpen
			btnRx.Enabled = moRS232.IsOpen
			btnCheck.Enabled = moRS232.IsOpen
		End Try
	End Sub

	Private Sub btnCloseCom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCloseCom.Click
		chkEvents.Enabled = False
		moRS232.Close()
		btnCloseCom.Enabled = moRS232.IsOpen
		btnOpenCom.Enabled = Not moRS232.IsOpen
		btnTx.Enabled = moRS232.IsOpen
		btnRx.Enabled = moRS232.IsOpen
		btnCheck.Enabled = moRS232.IsOpen
	End Sub

	Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTx.Click
      Dim sTx As String
      '----------------------
		'// Clear Tx/Rx Buffers
		moRS232.PurgeBuffer(Rs232.PurgeBuffers.TxClear Or Rs232.PurgeBuffers.RXClear)
		sTx = txtTx.Text
		If chkAddCR.Checked Then sTx += ControlChars.Cr
		moRS232.Write(sTx)
		'moRS232.Write(Chr(2) & Chr(2) & Chr(73) & Chr(48) & Chr(121) & Chr(3))
		'// Clears Rx textbox
		txtRx.Text = String.Empty
		txtRx.Refresh()
		lbHex.Items.Clear()
		If chkAutorx.Checked Then Button1_Click(Nothing, Nothing)
	End Sub

	Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
		If Not moRS232 Is Nothing Then
			'// Disables Events if active
			moRS232.DisableEvents()
			If moRS232.IsOpen Then moRS232.Close()
		End If
	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRx.Click
		Try
			moRS232.Read(Int32.Parse(txtBytes2Read.Text))
			txtRx.Text = moRS232.InputStreamString
			txtRx.ForeColor = Color.Black
			txtRx.BackColor = Color.White
			'// Fills listbox with hex values
			Dim aBytes As Byte() = moRS232.InputStream
			Dim iPnt As Int32
			For iPnt = 0 To aBytes.Length - 1
				lbHex.Items.Add(iPnt.ToString & ControlChars.Tab & String.Format("0x{0}", aBytes(iPnt).ToString("X")))
			Next
		Catch Ex As Exception
			txtRx.BackColor = Color.Red
			txtRx.ForeColor = Color.White
			txtRx.Text = "Error occurred " & Ex.Message & "  data fetched: " & moRS232.InputStreamString
		End Try
	End Sub



	Private Sub chkDTR_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkDTR.Click
		If Not (moRS232 Is Nothing) Then
			moRS232.Dtr = chkDTR.CheckState = CheckState.Checked
		End If
	End Sub

	Private Sub chkRTS_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkRTS.Click
		If Not (moRS232 Is Nothing) Then
			moRS232.Rts = chkRTS.CheckState = CheckState.Checked
		End If

	End Sub

	Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
		Me.Close()
	End Sub


	Private Sub optCom1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optCom1.CheckedChanged, optCom2.CheckedChanged
		If (sender Is optCom1) Then
			miComPort = 1
		Else
			miComPort = 2
		End If
	End Sub

	Private Sub linkAuthor_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles linkAuthor.LinkClicked
		System.Diagnostics.Process.Start("mailto:corrado@mvps.org")
	End Sub

	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'===================================================
		'
		'	Description	:	Fills form items
		'	Created			:	28/02/2002 - 10:33:20
		'
		'						*Parameters Info*
		'
		'	Notes				:
		'===================================================
		cboStatusLine.Items.Add("CTS")
		cboStatusLine.Items.Add("DSR")
		cboStatusLine.Items.Add("RI")
		cboStatusLine.Items.Add("CD")
	End Sub

	Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
		'===================================================
		'												
		'	Description	:	Check passed status line
		'	Created			:	28/02/2002 - 10:35:54
		'
		'						*Parameters Info*
		'
		'	Notes				:
		'===================================================
		If Not moRS232 Is Nothing Then
			Dim bState As Boolean
			Select Case cboStatusLine.Text
				Case "CTS"
					bState = moRS232.CheckLineStatus(Rs232.ModemStatusBits.ClearToSendOn)
				Case "DSR"
					bState = moRS232.CheckLineStatus(Rs232.ModemStatusBits.DataSetReadyOn)
				Case "RI"
					bState = moRS232.CheckLineStatus(Rs232.ModemStatusBits.RingIndicatorOn)
				Case "CD"
					bState = moRS232.CheckLineStatus(Rs232.ModemStatusBits.CarrierDetect)
			End Select
			MessageBox.Show("Selected line is " & IIf(bState, "On", "Off").ToString, "Check line status")
		End If
	End Sub

	Private Sub btnAsyncTx_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
		'// Clears Rx textbox
		lbAsync.Items.Clear()
		txtRx.Text = String.Empty
		txtRx.Refresh()
		lbHex.Items.Clear()
		mlTicks = DateTime.Now.Ticks
		'moRS232.AsyncWrite(Chr(4) & Chr(3) & Chr(0) & Chr(0) & Chr(0) & Chr(16) & Chr(68) & Chr(83))
		lbAsync.Items.Add("Tx Started at ticks: " & mlTicks.ToString)
		If chkAutorx.Checked Then btnAsync_Click(Nothing, Nothing)
	End Sub

	Private Sub btnAsync_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
		Try
			lbHex.Text = String.Empty
			lbHex.Refresh()
			Dim lTicks As Long = DateTime.Now.Ticks
			lbAsync.Items.Add("Rx Started at ticks: " & lTicks.ToString & " (" & (lTicks - mlTicks).ToString & ")")
		Catch Ex As Exception
			txtRx.BackColor = Color.Red
			txtRx.ForeColor = Color.White
			txtRx.Text = "Error occurred " & Ex.Message & "  data fetched: " & moRS232.InputStreamString
		End Try
	End Sub

	Private Sub moRS232_CommEvent(ByVal source As Rs232, ByVal Mask As Rs232.EventMasks) Handles moRS232.CommEvent
		'===================================================
		'												

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -