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

vb6-发送字节,接收端从0到255,vb6,Vb6,如果我使用VB6通过WriteFile(串行通信)发送字符串(字符)a,接收端将获得97(读取字节),这是a的Dec值 现在我想让接收方得到0-255之间的字节值 很容易得到一些结果,比如: 发送字符串a,获取字节97。发送字符串z,获取字节122。但是如何让receive获得字节值0或1 我怎样才能意识到这一点?我找到了vb6cbyte函数,但它似乎工作不正常。多谢各位 以下是我当前的代码: 发送端(vb6): 'this send the character "a" ca

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

现在我想让接收方得到0-255之间的字节值

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

发送字符串
a
,获取字节
97
。发送字符串
z
,获取字节
122
。但是如何让receive获得字节值
0
1

我怎样才能意识到这一点?我找到了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
void setup() {
  Serial.begin(115200);
}

void loop() {
  if (Serial.available()) {
    Serial.println(Serial.read()); //this print the byte received
  }
}
接收端(arduino):

'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
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应该可以正常工作


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

该问题是关于0或1的。。。所以你可以做:

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

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


请编辑您的问题,以包含您正在使用的相关代码。Thanks@StayOnTarget谢谢你的回复,我已经这样做了。请编辑你的问题,包括你正在使用的相关代码。Thanks@StayOnTarget谢谢你的回复,我做到了。非常感谢!这个函数工作得很好,看起来很简单,但可以帮助我摆脱牢笼。我真的很高兴邀请你喝杯咖啡:)很高兴听到这对你有帮助!非常感谢你!这个函数工作得很好,看起来很简单,但可以帮助我摆脱牢笼。我真的很高兴邀请你喝杯咖啡:)很高兴听到这对你有帮助!