-1

I'm new to python and I've write a simple code that reading lines from serial port and than write those lines to a text file. No errores occured, but the serial masseges did not appear in the text. the code:

import serial

ser = serial.Serial('COM32', baudrate=115200, parity=serial.PARITY_NONE,    stopbits=serial.STOPBITS_ONE,
                bytesize=serial.EIGHTBITS, xonxoff=1)
text = open("temptext1.txt", "a+")
while 1:
    read_line = ser.readline()
    print read_line
    text.write(read_line)

Thnaks for the helpers, i siriously dont have a clue how to debug this.

GP1308
  • 1
  • 1

1 Answers1

0

Try below code.

import serial
import io
def getSerialLogs(comport, fileName='SerialLog.txt', baudrate=115200):
    ser = serial.Serial(comport, baudrate, xonxoff =True, timeout=1)
    sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser, 1), encoding="utf-8")
    with open(fileName, 'a') as f:
        while ser.isOpen():
            datastring = sio.readline()
            f.write(datastring)
Ankur
  • 185
  • 1
  • 2
  • 10
  • workin' fine til this error: Traceback (most recent call last): File "C:/Users/tc34669/PycharmProjects/untitled/gui 3.py", line 11, in datastring = sio.readline() File "C:\Python27\lib\encodings\cp1255.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0xff in position 0: character maps to – GP1308 Feb 27 '18 at 12:13
  • try adding parameter encoding="utf-8" to io.TextIOWrapper. Modified the code – Ankur Mar 09 '18 at 09:09