vb6 - vb6 - 发送字节,接收端从 0 到 255
问题描述
a
如果我使用VB6通过WriteFile
(串行通信)发送字符串(字符) ,接收端将获得97
(读取字节),这是Dec值a
。
现在我希望接收方获取 0-255 的字节值。
某些结果很容易,例如:
发送字符串a
,获取字节97
。发送字符串z
,获取字节122
。但是如何让接收获得字节值0
或1
?
我怎么能意识到这一点?我找到了 vb6cbyte
功能,但它似乎无法正常工作。谢谢你。
这是我的当前代码:
发送方(vb6):
'this send the character "a"
call send_string(handle, "a")
Sub send_string(ByVal handle_connect As Long, ByVal s As String)
WriteComm handle_connect, StrConv(s, vbFromUnicode)
End Sub
Function WriteComm(ByVal hComm As Long, BytesBuffer() As Byte) As Long
Dim dwBytesWrite
If SafeArrayGetDim(BytesBuffer) = 0 Then Exit Function
WriteFile hComm, BytesBuffer(0), UBound(BytesBuffer) + 1, dwBytesWrite, 0
WriteComm = dwBytesWrite
End Function
接收方(arduino):
void setup() {
Serial.begin(115200);
}
void loop() {
if (Serial.available()) {
Serial.println(Serial.read()); //this print the byte received
}
}
解决方案
如果您真正想要的是发送数值,我不确定为什么代码必须以字符串开头作为其输入。但假设是这种情况,您可能可以使用该Chr()
函数将其他值编码为单个字符。
该问题询问了 0 或 1 ......所以你可以这样做:
s = Chr(0)
send_string handle, s
由此,s
将是一个字符串。它不会包含可打印的字符(ASCII 中的 0 不代表任何字母、数字、标点符号等),但这并不重要。
Chr
对于 0-255 的值应该可以正常工作。
推荐阅读
- android - Webview 没有使用 loadUrl() 加载更多数据
- javascript - 鼠标光标移向中心时的背景比例
- python - 获取函数有问题无法读取 URL 不知道为什么?
- python - 用 Keras 分类损失的意义
- node.js - 节点错误:resolve-url-loader:CSS 错误
- python - ResourceWarning:未关闭的文件 <_io.BufferedReader name=4>
- php - 无法从公共存储文件夹访问图像
- javascript - 在重定向到另一个 URL 的新打开的 chrome 选项卡上执行内容脚本
- selenium - 如何识别具有动态 id 和名称的 webelement?
- java - 如何在不使用背景的情况下删除形状(255);