vb6 - 发送字节,接收方从 0 到 255

vb6 - send byte, the receive side get from 0 to 255

如果我使用VB6通过WriteFile(串口通信)发送字符串(字符)a,接收方将得到97(读取字节),这是Dec a.

的值

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

有些结果很容易,例如:

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

我怎样才能意识到这一点?我找到了 vb6 cbyte 函数,但它似乎无法正常工作。谢谢。

这是我的当前代码:

发送方 (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。

Documentation