串口数据类型问题 [英] Serial port data type question
问题描述
我写了一个简单的程序,通过蓝牙与Arduino进行通信。该程序打开一个串口,连接到Arduino上的蓝牙端口,并在击键时发送单个字符(x,y,z)。由于Arduino执行按键确定的功能,因此该部分工作正常。
我希望电路板返回按下的键的字符发送命令,所以我有Arduino Serial.write,它似乎做了。我用以下代码读取数据:
私有 Sub SerialPort1_DataReceived( ByVal sender As System。对象,
ByVal e 作为系统。 IO.Ports.SerialDataReceivedEventArgs)_
句柄 SerialPort1.DataReceived
如果 SerialPort1。 IsOpen 然后
尝试
readBuffer = SerialPort1.ReadLine()
' 数据到UI线程
我 .Invoke(新 EventHandler( AddressOf DoUpdate))
Catch ex As 异常
MsgBox( read& ex.Message)
结束 尝试
结束 如果
结束 Sub
' 更新UI中收到的字符串
公共 Sub DoUpdate( ByVal 发件人作为 对象, ByVal e As System.EventArgs)
' rtbMonitor.Text = SerialPort1.BytesToRead
如果 readBuffer.Contains( y)然后
rtbMonitor.Text = 包含字母y ' 这是结果
其他
rtbMonitor.Text = readBuffer
结束 如果
如果 readBuffer.Equals( y)然后
rtbMonitor2.Text = 字母y
其他
rtbMonitor2.Text = readBuffer ' y是结果
End 如果
我尝试过:
我想将返回的字符用于其他一些函数,但数据类型让我感到厌烦。或者,我不确定从Arduino发回的是什么。我想如果我如上所述读取缓冲区,将返回简单的字符串y。显然,缓冲区包含y但不等于y。 y似乎是文本(字符串);它不是作为char读取的。所以问题是除了y之外的缓冲区(readBuffer)是什么,我该如何阅读它?发送的数据非常少。每隔几秒钟只需一次击键。
你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。
调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。
调试器 - 维基百科,免费的百科全书 [ ^ ]
掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]
调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候pected,你接近一个bug。
使用ReadLine()
暗示如果你想要一个字符串chars,使用一次读取1个字符的函数。
如果您使用的是System.IO.Ports.SerialPort,则SerialPort.ReadLine将等待接收CR。如果没有CR,则在超时期限之后,应抛出TimeoutException。
你是否收到TimeoutException?
尝试使用SerialPort.ReadByte来读取单个字节。检查SerialPort.BytesToRead并循环读取所有字节。
另外,查看SerialPort.ReceivedBytesThreshold - 将其设置为1以获取每个字节的事件。如果你设置为1,你处理字节的能力是有限的,所以不要发送很多字节。
抱歉,VB.Net不是我的主要编程语言
如果 SerialPort1.IsOpen 然后
尝试
Dim byteCount < span class =code-keyword> as Int = SerialPort1.BytesToRead
Dim i as Int
Dim 数据 as Int
For i = 1 to byteCount
data = SerialPort1 .ReadByte()
如果数据<> -1 然后
' 数据到UI线程
我 .Invoke(新 EventHandler( AddressOf DoUpdate))
结束 如果
< span class =code-keyword> Next
Catch ex As 异常
MsgBox( read& ex.Message)
结束 尝试
结束 如果跨度>
I wrote a simple program to communicate via Bluetooth with an Arduino. The program opens a serial port, connects to the Bluetooth port on the Arduino and sends single characters (x,y,z)on keystrokes. Since the Arduino carries out the functions as determined by the keys, that part is working fine.
I wanted the board to return the character of the key that was pressed to deliver the command, so I have the Arduino Serial.write back, which it appears to do. I read the data with this code:
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object,
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles SerialPort1.DataReceived
If SerialPort1.IsOpen Then
Try
readBuffer = SerialPort1.ReadLine()
'data to UI thread
Me.Invoke(New EventHandler(AddressOf DoUpdate))
Catch ex As Exception
MsgBox("read " & ex.Message)
End Try
End If
End Sub
' update received string in UI
Public Sub DoUpdate(ByVal sender As Object, ByVal e As System.EventArgs)
' rtbMonitor.Text = SerialPort1.BytesToRead
If readBuffer.Contains("y") Then
rtbMonitor.Text = "contains the letter y" 'this is the result
Else
rtbMonitor.Text = readBuffer
End If
If readBuffer.Equals("y") Then
rtbMonitor2.Text = "the letter y"
Else
rtbMonitor2.Text = readBuffer ' y is the result
End If
What I have tried:
I want to use the returned character for some other functions, however, the data type escapes me. Alternately, I am unsure of what is being sent back from the Arduino. I thought if I read the buffer as above, the simple string "y" would be returned. Apparently, the buffer contains a "y" but does not equal a "y". The y seems to be text (string); it is not read as char. So the question is what is in the buffer (readBuffer) other than "y" and how can I read it? There is very little data being sent. Just a keystroke every few seconds.
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.
The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
UsingReadLine()
imply getting a string, if you want chars, use a function that read 1 char at a time.
If you are using System.IO.Ports.SerialPort, then the SerialPort.ReadLine waits for a CR to be received. If no CR, then after the timeout period, a TimeoutException should be thrown.
Are you getting the TimeoutException?
Try using SerialPort.ReadByte instead in order to read a single byte. Check SerialPort.BytesToRead and loop reading all the bytes.
Also, look at SerialPort.ReceivedBytesThreshold - set it to 1 to get an event for each byte. If you do set to 1, your ability to process the bytes is limited so don't send very many bytes.
Sorry, VB.Net is not my primary programming language
If SerialPort1.IsOpen Then Try Dim byteCount as Int = SerialPort1.BytesToRead Dim i as Int Dim data as Int For i = 1 to byteCount data = SerialPort1.ReadByte() If data <> -1 Then 'data to UI thread Me.Invoke(New EventHandler(AddressOf DoUpdate)) End If Next Catch ex As Exception MsgBox("read " & ex.Message) End Try End If
这篇关于串口数据类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!