我正在使用Visual Basic编写程序,遇到了一个奇怪的问题。我通过串口向望远镜发送字符串。当我发送“check”字符串时,范围可以返回chr(0)或chr(255)。在Python和C++中,这可以正常工作并返回chr(255)。但是,在Visual Basic中运行脚本时,它返回chr(0)或chr(63)。
下面是两个相同的函数,一个在Python中,一个在Visual Basic中。
有人能给我一个想法,为什么Visual Basic返回63而不是255吗?
Python中的函数(返回正确值0和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()
Visual Basic中的脚本(返回不正确的值0和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