pythonのモジュールでpyserialを利用してUSB通信先のセンサとシリアル通信のテストをしています.
ATコマンドを送信して,その返答を読み込んだ際に以下のような文字列で返ってきました.
バイナリ文字の処理はあまり素養がないためどのようにして文字列に変換すればよいのか調べたのですが,困惑してしまいました...
'\xe6\x80\x98\x80\x1e\x0c\xe6\x80\x98\x80'
このような文字列の処理はどのように考えて対処していけばよいのか,ご教授いただきたいです.
よろしくお願いします.
追記です
python2.7 コマンドに対して,<CR><LF>OK<CR><LF> または<CR><LF>#<CR><LF>の返答が帰ってくるようです.
コマンドはasciiで書き込み,出力は
計測が開始されると32bitのリトルエンディアンの形式でデータが受信されるようです.
今は以下のコードで動作確認をしているところでした.
python
1import serial 2import sys, time 3import os 4 5def main(): 6 7 port = 'COM5' 8 baudrate = 460800 9 ser = serial.Serial(port, baudrate, timeout=3) 10 11 print 'Starting Up Serial Monitor' 12 13 if ser.isOpen(): 14 15 try: 16 ser.flushInput() # flush input buffer, discarding all its contents 17 ser.flushOutput() # flush output buffer, aborting current output 18 time.sleep(1) 19 20 ser.write(bytes("ATCONFIG\r\n")) 21 time.sleep(1) 22 print "Send the command to config sensor" 23 24 ser.write(bytes("ATWRDTRG = 2, 00005, 0\r\n")) 25 time.sleep(1) 26 27 28 ser.write(bytes("ATRDDTRG\r\n")) 29 time.sleep(1) 30 31 print "Send the command to read the trigger setting" 32 print ser.portstr 33 r = ser.readline() 34 print r.encode('hex') 35 print r 36 37 ser.write(bytes("ATENDMES\r\n")) 38 time.sleep(1) 39 40 print "Send the command to start Measurement Mode" 41 42 ser.write(bytes("ATMESMD1\r\n")) 43 time.sleep(1) 44 45 print "Send the command to start Measurement Mode" 46 time.sleep(2.5) # Measurement Wait Time is 220[ms] 47 48 49 50 nline = 0 51 while True: 52 ser.write(b"ATRDDFRM\r\n") 53 54 str = ser.readline() 55 print ser.readline() 56 57 ser.write(bytes("ATREQDAT\r\n")) 58 print "Send the command to request Measurement data" 59 60 nline = nline + 1 61 if (nline >= 1000): 62 break 63 64 65 ser.close() 66 67 except Exception as e: 68 print "error communicating...: " + str(e) 69 70 else: 71 print "cannot open serial port " 72 73 74# Main Process ----------------------------- 75if __name__ == '__main__': 76 try: 77 78 main() 79 80 except Exception as e: 81 print('Error occurred on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e), e) 82 raise e

回答1件
あなたの回答
tips
プレビュー