VB.net returns chr (255) as chr (63)

I am writing a program in visual basic and am facing an odd problem. I am sending strings through the serial port to the telescope. When I send a string check, the region can return either chr (0) or chr (255). This works fine in python and C ++ returning chr (255). However, when I run the script in the visual base, it returns chr (0) or chr (63).

below are two identical functions, one in python and one in the visual base.

Can someone give me an idea why visual base returns 63 instead of 255?

in python (returns the correct values ​​0 and 255):

global d, check
d=chr(80)+chr(4)+chr(16)+chr(2)+chr(1)+chr(112)+chr(252)+chr(0)
check=chr(80) + chr(1) + chr(16) + chr(19) + chr(0) + chr(0) + chr(0)+chr(1)


def test():
    ser.write(d)
    time.sleep(.1)
    print ser.readline()
    ser.write(check)
    time.sleep(.1)
    out=ser.readline()[0]
    print "out=",ord(out)
    while out == chr(0):
            print "out = ", ord(out)
            ser.write(check)
        time.sleep(.1)
            out=ser.readline()[0]
        print "out=",ord(out)
    print "out is now", ord(out)
    ser.readline()

script in visual base (returns invalid values ​​0 and 63)

Public Sub test()
    Dim out As Char
    Dim d As String = Chr(80) + Chr(4) + Chr(16) + Chr(2) + Chr(1) + Chr(112) + Chr(252) + Chr(0)
    Dim check As String = Chr(80) + Chr(1) + Chr(16) + Chr(19) + Chr(0) + Chr(0) + Chr(0) + Chr(1)
    port.Write(d)
    Threading.Thread.Sleep(100)
    Console.Write(port.ReadTo("#"))
    port.Write(check)
    Threading.Thread.Sleep(100)
    out = port.ReadTo("#")
    Console.Write(vbNewLine & "out=" & out)
    While out = Chr(0)
        Console.Write("out = " & Convert.ToInt16(out))
        port.Write(check)
        Threading.Thread.Sleep(0.1)
        out = port.ReadTo("#")
        Console.Write("out=" & Convert.ToInt16(out))
    End While
    Console.Write("out is now" & Convert.ToInt16(out))
    port.ReadLine()


End Sub
+4
source share
1 answer

.NET SerialPort a Encoding, , , . , " ". , Encoding , string, fromt ReadTo() ( "-" ).

, , , 255. ReadTo() Encoding , , string. Encoding - ASCIIEncoding, 0x00 - 0x7f. '?', ASCII 63.

ReadByte() , - .

. - BCL, , , 8- . 26 2006 " SerialPort" (BCL).

SerialPort , SerialPort.Encoding . , .

SerialPort ASCII, 0-127 . , "Hello" {72, 101, 108, 108, 111}. 63 '?'. {72, 101, 108, 108, 111}, "Hello" , , 127 '?'.

ASCII , , 127 , 127. ; API, byte[] Encoding, . , , 127 , , , , . , 0-255 - " (ISO)". SerialPort:

SerialPort mySerialPort = new SerialPort("COM1");
mySerialPort.Encoding = Encoding.GetEncoding(28591);
+6

All Articles