Search code examples
vb6

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


if i using VB6 to send the string (character) a through WriteFile (serial communication), the receive side will get 97 (reading byte), this is Dec value of a.

now i want the receiver side get the byte value from 0-255.

it's easy for some result, like:

send string a, get byte 97. send string z, get byte 122. but how to let receive get the byte value 0 or 1?

how can i to realize this? i found vb6 cbyte function, but it seems not work properly. thank you.

here are my code current:

send side (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

receive side (arduino):

void setup() {
  Serial.begin(115200);
}

void loop() {
  if (Serial.available()) {
    Serial.println(Serial.read()); //this print the byte received
  }
}

Solution

  • I'm not sure why the code necesarily has to start with a string as its input, if what you really want is to send numeric values. But assuming that is the case, you probably can use the Chr() function to encode other values as individual characters.

    The question asked about 0 or 1... so you could do:

    s = Chr(0)
    send_string handle, s
    

    From this, s will be a string. It won't contain a printable character (0 in ASCII does not represent any letter, numeral, punctuation, etc.) but that doesn't matter.

    Chr should work fine for values 0-255.

    Documentation