[PyQt] Using QSerialPort() instead of pyserial

Florian Bruhin me at the-compiler.org
Mon Nov 20 08:33:58 GMT 2017


Hi,

On Mon, Nov 20, 2017 at 06:02:57PM +1000, Phil wrote:
> The pyserial and QSerialPort code supplied was an example of what worked and
> what didn't.

Yes, but it was code which didn't run at all, so it can't possibly be the code
you did *actually* run. Having code which actually runs makes things easier to
debug ;-)

> Anyway, after a good night's sleep and a bit of experimentation I now have a
> working solution as follows:
> 
>         if self.serial.canReadLine():
>             data = self.serial.readLine()
>             print(data)
> 
> I'm not sure what happens, at the moment, if there isn't and serial date to
> read

That block would just be skipped.

> One thing I haven't sorted out is how to convert a QBytearray, "data" in
> this case, to a string. The following converts the mytext QString to a byte
> array but I cannot see how to convert a byte array to a string.

(I'm assuming Python 3 here)

I usually do bytes(data).decode('ascii').
That converts the QByteArray to a Python bytes object, and then decodes that.

You could probably do str(data, encoding='ascii') too, though.

> byte = bytes(mytext, encoding='ascii')

FWIW the more idiomatic way this is usually done is  mytext.encode('ascii')

Florian

-- 
https://www.qutebrowser.org  | me at the-compiler.org (Mail/XMPP)
   GPG: 916E B0C8 FD55 A072  | https://the-compiler.org/pubkey.asc
         I love long mails!  | https://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20171120/690d859a/attachment.sig>


More information about the PyQt mailing list