Search code examples
pythonserial-portpyserialisonon-ascii-characters

Python : write to COM in ISO 8859-1


I need to write to a COM port using the HART protocol. The words I need to write in the port are in ISO 8859-1 format. I tried with pyvisa but I get an error (for characters like these : ÿÿÿÿÿ)... I think because they are not-standard ASCII. What can I do now? Should I go for another package to write on the COM port?

Any suggestion?


Solution

  • If you use pyserial, you can use the "Latin1" encoding instead of the standard "UTF-8". RX looks like:

    inpchar = comXX.read().decode("Latin1",'ignore')
    

    TX looks like:

    Text = "-273\xb0C\n" ## will print "-273°C\n"
    comXX.write(Text.encode("Latin1"))
    

    This works on pyserial 3.x, not sure about 2.x. (update) I just ran this on my build machine, printing fails on python 3.4, but works on python 3.6.2. the read() decode seemed to work OK, but the print failed... I nuked python 3.4 and put in 3.6.2, and a fresh download of pyserial, works fine. A fresh install of 3.6.2 and pyserial 3.4 will work fine.