收不到00

dx_cyb 2009-02-09 03:18:17
收十六进制的数据不对,收不到00的数据,如 01 00 02 00 03 仅收到 01 02 03

Imports System.Threading

Public Class Form1

Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated

End Sub

Public Sub SaveFile()
' You should replace the bold file name in the
' sample below with a file name of your own choosing.
txtDataReceived.SaveFile(System.Environment.GetFolderPath _
(System.Environment.SpecialFolder.Personal) _
& "\Testdoc.rtf", _
RichTextBoxStreamType.RichNoOleObjs)
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 ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

End Sub

Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

End Sub

Private Sub Form1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Validating

End Sub

Private Sub BtnClsText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClsText.Click
txtDataReceived.Clear()

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Showbool = Not Showbool
If Showbool = True Then
Button1.Text = "停止显示"
Else
Button1.Text = "继续显示"
End If
End Sub

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged

End Sub

Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged

End Sub

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbRAsc.CheckedChanged

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
SaveFileDialog1.Filter = "TXT Files|*.Txt"
SaveFileDialog1.Title = "保存对话框"


If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
txtDataReceived.SaveFile(SaveFileDialog1.FileName, _
RichTextBoxStreamType.PlainText)
End If
End Sub




Private Sub BtnCloseCom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCloseCom.Click
If SerialPort1.IsOpen = True Then
SerialPort1.Close()
BtnCloseCom.Text = "打开串口"
Else
With SerialPort1
.PortName = ComboBox1.Text
.BaudRate = CInt(ComboBox2.Text)

Select Case ComboBox3.SelectedIndex
Case 0
.Parity = IO.Ports.Parity.None
Case 1
.Parity = IO.Ports.Parity.Odd
Case 2
.Parity = IO.Ports.Parity.Even
End Select
.DataBits = CInt(ComboBox4.Text)
.StopBits = CInt(ComboBox5.Text)
End With
If SerialPort1.IsOpen = False Then
SerialPort1.Open()
BtnCloseCom.Text = "关闭串口"

End If
End If
End Sub


Private Sub BtnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSend.Click
If SerialPort1.IsOpen = True Then
SerialPort1.Write(TextBox1.Text)
Else
MessageBox.Show("串口未打开", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)
End If
End Sub

'Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
' Dim strT As String

' strT = SerialPort1.ReadExisting()
' If strT <> "" Then
' txtDataReceived.AppendText(strT + Format(Now, "H;mm:ss:ff"))

' End If

'End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

'触发-串口收到数据
txtDataReceived.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})

End Sub

Public Delegate Sub myDelegate()

Public Sub updateTextBox()
'显示更新串口收到的数据
If Showbool = True Then


With txtDataReceived
If rbRAsc.Checked Then '字符
.AppendText(SerialPort1.ReadExisting) '& vbCrLf
Else '16进制
Dim iReceivedbytes As Integer, i As Integer, j As Integer
Dim strReceiveBytes, str As String
Dim bReadByte() As Byte
iReceivedbytes = SerialPort1.BytesToRead 'buffer中所有字节数

ReDim bReadByte(iReceivedbytes - 1)
'If m_bStopDisp = False Then
SerialPort1.Read(bReadByte, 0, iReceivedbytes)
j = iReceivedbytes - 1
For i = 0 To iReceivedbytes - 1
strReceiveBytes = Hex(bReadByte(i))
.AppendText(IIf(Len(strReceiveBytes) > 1, strReceiveBytes, "0" & strReceiveBytes) & " ")
If bReadByte(i) = &H0 Then
TextBox2.Text = "00"
.AppendText("00")
End If
Next

str = Hex(bReadByte(j))
str = IIf(Len(str) > 1, str, "0" & str)
If str = "03" Then
.SelectionColor = System.Drawing.Color.Red
.AppendText(Format(Now, "H;mm:ss:fff") & " " & vbCrLf)
.SelectionColor = System.Drawing.Color.Black
End If

End If
.ScrollToCaret()
End With
End If
End Sub

End Class
...全文
114 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
龙宜坡 2009-02-12
  • 打赏
  • 举报
回复
解决就好!
怎么的是发送二进制呢,不是十六进制吗?
dx_cyb 2009-02-12
  • 打赏
  • 举报
回复
问题已解决,发送二进制时应discardnull=false
流年岁月 2009-02-10
  • 打赏
  • 举报
回复
不太懂,帮顶了
dx_cyb 2009-02-10
  • 打赏
  • 举报
回复
好,我去看看
zdingyun 2009-02-09
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20090125/20/8b123367-b08e-4771-b064-ed6a5b31d9ff.html
请LZ参阅上述链接下我用VB.NET2008做的代码,按2进制接收,分别显示为ASCII字符串或16进制字符串.
hanchao0821 2009-02-09
  • 打赏
  • 举报
回复
不懂!
龙宜坡 2009-02-09
  • 打赏
  • 举报
回复
串口通信

我的经验:
1.每次发送或接收数据后需要延迟,>300毫秒建议
2.使用VB6的那个MSCOMM控件比.NET带的那个串口控件出现问题的几率小一点,后者使用C++开发的,C++写的
底层不用说!


3.看见这样的代码我有想撞墙的冲动!

16,553

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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