首页 > 解决方案 > vb6 - 发送字节,接收端从 0 到 255

问题描述

a如果我使用VB6通过WriteFile(串行通信)发送字符串(字符) ,接收端将获得97(读取字节),这是Dec值a

现在我希望接收方获取 0-255 的字节值。

某些结果很容易,例如:

发送字符串a,获取字节97。发送字符串z,获取字节122。但是如何让接收获得字节值01

我怎么能意识到这一点?我找到了 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
  }
}

标签: vb6

解决方案


如果您真正想要的是发送数值,我不确定为什么代码必须以字符串开头作为其输入。但假设是这种情况,您可能可以使用该Chr()函数将其他值编码为单个字符。

该问题询问了 0 或 1 ......所以你可以这样做:

s = Chr(0)
send_string handle, s

由此,s将是一个字符串。它不会包含可打印的字符(ASCII 中的 0 不代表任何字母、数字、标点符号等),但这并不重要。

Chr对于 0-255 的值应该可以正常工作。

文档


推荐阅读