【问题标题】:vb6 - send byte, the receive side get from 0 to 255vb6 - 发送字节,接收端从 0 到 255
【发布时间】:2021-05-09 17:03:42
【问题描述】:

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

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

很容易得到一些结果,比如:

发送字符串a,获取字节97。发送字符串z,获取字节122。但是如何让receive获得字节值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
  }
}

【问题讨论】:

  • 请编辑您的问题以包含您正在使用的相关代码。谢谢
  • @StayOnTarget 谢谢你的回复,我做到了。

标签: vb6


【解决方案1】:

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

这个问题问的是 0 还是 1...所以你可以这样做:

s = Chr(0)
send_string handle, s

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

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

Documentation

【讨论】:

  • 非常感谢!这个功能很好用,看起来很简单,但可以帮助我摆脱困境。我真的很高兴请你喝杯咖啡:)
  • 很高兴听到它有帮助!
猜你喜欢
  • 2011-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-13
  • 1970-01-01
  • 1970-01-01
  • 2012-04-26
  • 1970-01-01
相关资源
最近更新 更多