如何用VB.NET向串口发送十六进制数

jixishunan 2008-07-27 05:19:15
看了论坛里有一些用VB6.0实现的,但是由于其他的部分都是用VB.NET做的,所以想用VB.NET实现
...全文
444 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
da00xiong1985 2009-07-03
  • 打赏
  • 举报
回复
谢谢~~~
jixishunan 2008-07-28
  • 打赏
  • 举报
回复
谢谢了!!已经解决了!!
jixishunan 2008-07-28
  • 打赏
  • 举报
回复
貌似VB.NET里没有MSComm控件把……
zdingyun 2008-07-27
  • 打赏
  • 举报
回复
Public Class Form1
Inherits System.Windows.Forms.Form
Dim strData As String

#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 AxMSComm1 As AxMSCommLib.AxMSComm
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.AxMSComm1 = New AxMSCommLib.AxMSComm
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.Button2 = New System.Windows.Forms.Button
CType(Me.AxMSComm1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'AxMSComm1
'
Me.AxMSComm1.Enabled = True
Me.AxMSComm1.Location = New System.Drawing.Point(376, 128)
Me.AxMSComm1.Name = "AxMSComm1"
Me.AxMSComm1.OcxState = CType(resources.GetObject("AxMSComm1.OcxState"), System.Windows.Forms.AxHost.State)
Me.AxMSComm1.Size = New System.Drawing.Size(38, 38)
Me.AxMSComm1.TabIndex = 1
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(8, 32)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.TextBox1.Size = New System.Drawing.Size(248, 216)
Me.TextBox1.TabIndex = 3
Me.TextBox1.Text = "TextBox1"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(304, 48)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(112, 32)
Me.Button1.TabIndex = 4
Me.Button1.Text = "Button1"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(264, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(120, 24)
Me.Label1.TabIndex = 5
Me.Label1.Text = "Label1"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(304, 88)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(112, 32)
Me.Button2.TabIndex = 6
Me.Button2.Text = "Button2"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(472, 266)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.AxMSComm1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.AxMSComm1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AxMSComm1.Settings = "9600,N,8,1"
AxMSComm1.RThreshold = 1
AxMSComm1.InputMode = MSCommLib.InputModeConstants.comInputModeBinary
AxMSComm1.PortOpen = True
End Sub

Private Sub AxMSComm1_OnComm(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AxMSComm1.OnComm
Dim bytInput() As Byte
Dim intInputLen As Integer
Select Case AxMSComm1.CommEvent
'Case comEvReceive
Case 2
'此处添加处理接收的代码
'AxMSComm1.InputMode = comInputModeBinary '二进制接收
intInputLen = AxMSComm1.InBufferCount
ReDim bytInput(intInputLen)
bytInput = AxMSComm1.Input
Dim i As Integer
For i = 0 To UBound(bytInput)
If Len(Hex(bytInput(i))) = 1 Then
strData = strData & "0" & Hex(bytInput(i))
Else
strData = strData & Hex(bytInput(i))
End If
Next
'If Mid(strData, 17, 2) = "20" And Mid(strData, 19, 2) = "20" And Len(strData) = 20 Then
TextBox1.Text = strData
strData = ""
'End If
End Select
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sj As String
Dim bytHex() As Byte
sj = "002702013524200239892700020020010201E9030D07"
ReDim bytHex((Len(sj) \ 2) - 1)
Dim i As Integer
For i = 1 To Len(sj) Step 2
bytHex((i - 1) / 2) = Val("&H" & Mid(sj, i, 2))
Next
AxMSComm1.Output = bytHex
End Sub
'清除
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
strData = ""
TextBox1.Text = ""
End Sub
End Class

863

社区成员

发帖
与我相关
我的任务
社区描述
VB COM/DCOM/COM+
c++ 技术论坛(原bbs)
社区管理员
  • COM/DCOM/COM+社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧