0
일부 GPIO 값을 읽으려고합니다.파이썬 시리얼 : 포트를 읽는 데 문제가 있습니다.
import serial
import codecs
import time
ser = serial.Serial(port = 'COM4', baudrate = 9600, \
parity = serial.PARITY_NONE, \
stopbits = serial.STOPBITS_ONE, \
bytesize = serial.EIGHTBITS, \
timeout = 0, \
)
print('connected to: ',ser.name)
ser.close()
def SSend(input):
ser.write(codecs.decode(input, "hex_codec")) #send as ASCII
print('sent: ', input)
def ReadIO():
#open the port
try:
ser.open()
except:
print('error opening serial port')
exit()
#flush the buffers
ser.flushInput()
ser.flushOutput()
#write data to read from GPIO
#causes SC18IM to return a byte containing each of the 8 I/O values
SSend(b'4950')
time.sleep(0.1) #allow time for the data to be received
#read the data
serialData = False
serialData = ser.readline()
ser.close()
return serialData
while 1:
print(ReadIO())
time.sleep(0.5)
이 다음 인쇄 : 여기 코드는
보내 b'4950
(I 다시 기대하고있다 × 00 또는 0x20에 대신 중 하나 '
B'를 ' 빈 바이트)
Realterm을 사용할 때 예상 한 결과를 얻었고 성공적인 쓰기 명령을 받았기 때문에 내 하드웨어가 양호하다는 것을 알고 있습니다. 내 대본에서 다른 곳에.
나는이 일을하고 간헐적으로 작동 나타납니다 이유#read the data
serialData = False
for c in ser.readline():
print('in loop')
print(c)
serialData = c
ser.close()
그러나, 난 정말 이해가 안 돼요 사용하여 약간의 행운이 있었다.
읽어 주셔서 감사합니다.
오, 이제 알겠습니다. 그 문제를 해결해 주셔서 감사합니다. 문제의 또 다른 부분은 노예가 ASCII 문자로 시리얼 데이터를 보내고 있다는 것이 었습니다.이 문제는'binascii.hexlify (data)'를 사용하여 수정되었습니다. – Jack