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

📄 form1.vb

📁 rs232通讯VB.NET小程序
💻 VB
📖 第 1 页 / 共 3 页
字号:
         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 = 3
		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
		'===================================================
		'												?003 www.codeworks.it All rights reserved
		'
		'	Description	:	Events raised when a comunication event occurs
		'	Created			:	15/07/03 - 15:13:46
		'	Author			:	Corrado Cavalli
		'
		'						*Parameters Info*
		'
		'	Notes				:	
		'===================================================
		Debug.Assert(Me.InvokeRequired = False)

		Dim iPnt As Int32, sBuf As String, Buffer() As Byte
		Debug.Assert(Me.InvokeRequired = False)
		lbAsync.Items.Add("Mask: " & Mask.ToString)
		If (Mask And Rs232.EventMasks.RxChar) > 0 Then
			lbHex.Items.Add("Received data: " & source.InputStreamString)
			Buffer = source.InputStream
			For iPnt = 0 To Buffer.Length - 1
				lbHex.Items.Add(iPnt.ToString & ControlChars.Tab & String.Format("0x{0}", Buffer(iPnt).ToString("X")))
			Next
			lbHex.SelectedIndex = lbHex.Items.Count - 1
		End If
		lbAsync.SelectedIndex = lbAsync.Items.Count - 1
	End Sub

#Region "UI update routine"
#End Region

	Private Sub chkEvents_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkEvents.CheckedChanged
		'===================================================
		'												?003 www.codeworks.it All rights reserved
		'
		'	Description	:	Set state of notification events
		'	Created			:	16/07/03 - 9:44:05
		'	Author			:	Corrado Cavalli
		'
		'						*Parameters Info*
		'
		'	Notes				:
		'===================================================
		If Not moRS232 Is Nothing Then
			If txtBytes2Read.Text.Length = 0 Then
				moRS232.RxBufferThreshold = 1
			Else
				moRS232.RxBufferThreshold = Int32.Parse(txtBytes2Read.Text)
			End If
			If chkEvents.Checked Then
				moRS232.EnableEvents()
			Else
				moRS232.DisableEvents()
			End If
		End If
	End Sub

	Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
		System.Diagnostics.Process.Start("http://www.codeworks.it/net/index.htm")
	End Sub


	Private Sub lbAsync_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbAsync.SelectedIndexChanged

	End Sub

	Private Sub lbAsync_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbAsync.DoubleClick
		lbAsync.Items.Clear()
	End Sub


	Private Sub lbHex_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbHex.DoubleClick
		lbHex.Items.Clear()
	End Sub

	Private Sub txtRx_BackColorChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRx.BackColorChanged

	End Sub

	Private Sub txtRx_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRx.DoubleClick
		txtRx.Text = String.Empty
	End Sub


	Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
        Try
            If Rs232.IsPortAvailable(Int32.Parse(txtPortNum.Text)) Then
                MessageBox.Show("Port available", "Port test", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("Port NOT available", "Port test", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Catch ex As Exception
            MessageBox.Show("Port test failed", "Port test", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
	End Sub

    Private Sub btnInBuffer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInBuffer.Click
        lblInBuffer.Text = moRS232.InBufferCount.ToString()
    End Sub

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub
End Class

⌨️ 快捷键说明

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