2017-07-20 224 views
0

私はRS232ケーブルを介してSR830ロックインアンプと通信しています。次のコードのようにデータを読み出す場合:Pythonシリアル(pySerial) nの代わりにEOL rで行を読む

import serial 

def main(): 
    ser = serial.Serial(
     port='COM6', 
     baudrate=19200, 
     parity=serial.PARITY_NONE, 
     stopbits=serial.STOPBITS_ONE, 
     bytesize=serial.EIGHTBITS) 
    ser.timeout=1 
    ser.write("OUTP? 1 \r\n".encode()) #Asks the Lock-in for x-value 
    ser.write("++read\r\n".encode()) 
    x=ser.readline() 
    print (x) 
if __name__ == '__main__': main() 

を私はb'-3.7486e-008\r'のようなバイト文字列を取得します。しかし、ser.readline()関数は\ rをEOLとして認識しません。だから私はデータを読むたびにタイムアウトを待たなければならない。できるだけ早く多くのポイントを取って欲しいので面倒だ。そして番号の長さが大きく変わるので、例えばser.read(12)を使うことはできません。私はio.TextIOWrapperを使用しようとしましたが、それを実装する方法は私には分かりません。ここに私の試みがあります:

import serial 
import io 
def main(): 
    ser = serial.Serial(
     port='COM6', 
     baudrate=19200, 
     parity=serial.PARITY_NONE, 
     stopbits=serial.STOPBITS_ONE, 
     bytesize=serial.EIGHTBITS) 
    ser.timeout=1 
    sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser)) 
    sio.write("OUTP? 1 \r\n") #Asks the Lock-in for x-value 
    sio.write("++read\r\n") 
    x=sio.readline() 
    print (x) 
if __name__ == '__main__': main() 

ちょうど空白が表示されます。どんな助けでも大変感謝します。

EDIT: ここに私の作業コードは、ループを使用して、答えた後です:

についての読書のための単純なループを使用して何
import serial 
def main(): 
    ser = serial.Serial(
     port='COM6', 
     baudrate=19200, 
     parity=serial.PARITY_NONE, 
     stopbits=serial.STOPBITS_ONE, 
     bytesize=serial.EIGHTBITS) 
    ser.timeout=5 
    ser.write("OUTP? 1 \r\n".encode()) #Asks the Lock-in for x-value 
    ser.write("++read\r\n".encode()) 
    buffer = "" 
    while True: 
     oneByte = ser.read(1) 
     if oneByte == b"\r": #method should returns bytes 
      print (buffer) 
      break 
     else: 
      buffer += oneByte.decode() 
if __name__ == '__main__': main() 
+0

可能性のある重複した[pySerial 2.6:)(readlineの中で行末を指定](https://stackoverflow.com/questions/16470903/pyserial-2-6-end-of-line-in-readline) – SiHa

+1

'TextIOWrapper'に少なくとも' newline = '\ r "を入れて、それを使う必要があります。 '\ n'も探します – Eric

答えて

2

def readData(): 
    buffer = "" 
    while True: 
     oneByte = ser.read(1) 
     if oneByte == b"\r": #method should returns bytes 
      return buffer 
     else: 
      buffer += oneByte.decode("ascii") 

あなたは彼らが法read_untilを達成するために、同じ方法を使用し、Pyserialパッケージからserialutil.pyファイルを確認することができます。 the docs for readline()から

+1

ひどいパフォーマンスのために一般的な解決策としては推奨しませんが、低速シリアルインターフェイスについて話しているので、おそらく動作します。 – juhist

+1

あなたは '\ r'ではなく' \ r'を意味します。 –

+0

@Błotosmętekありがとう、固定。 –

1

:もちろん

The line terminator is always b'\n' for binary files; for text files, the newline argument to open() can be used to select the line terminator(s) recognized.

、あなたはここでopenを使用することはできません。しかし、あなたが行うことができますが、テキストストリームにバイトストリームを変換するためにio.TextIOWrapperを使用している:の

ser_text = io.TextIOWrapper(ser, newline='\r') 
ser_text.readline() 
+0

'BufferedRWPair'は必要ですか? – Eric

+0

実際、このコードでもタイムアウトを待っていることが分かりました。私はループに固執します。私はそのようなことが起こっているのを見ることができます。 –

+0

なぜタイムアウトを0に設定しないのですか? – Eric

関連する問題