Re: [Tutor] Converting a string to a byte array
On 26/09/17 07:02, Cameron Simpson wrote: Just to this. If your serial file handle is a normal buffered one, you may also need to call .flush(). When you run from the command line as "python3 mycode.py" is done automatically at programme exit. In the IDE, the programme has not exited, so your bytes may be lurking in the buffer, unsent. Thank you Cameron, that sounds like a logical explanation. I'll try it. -- Regards, Phil ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Converting a string to a byte array
On 25Sep2017 09:29, Phil wrote: [...] Just for interest I amended my code to use what you provided and tried it under IDLE. There aren't any errors but but my Arduino is not responding. However, if I enter python3 mycode.py then it works perfectly. I'm sure there's an explanation for this. Just to this. If your serial file handle is a normal buffered one, you may also need to call .flush(). When you run from the command line as "python3 mycode.py" is done automatically at programme exit. In the IDE, the programme has not exited, so your bytes may be lurking in the buffer, unsent. Cheers, Cameron Simpson (formerly c...@zip.com.au) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Converting a string to a byte array
On 25Sep2017 09:29, Phil wrote: On 25/09/17 07:26, Cameron Simpson wrote: I don't understand why this works from the pyqt IDE but not when run from the console. I suppose the IDE is adding the correct encoding. I'm guessing the IDE is python 2 and not doing any encoding at all. In python 2 str _is_ effectively bytes and if you stay in ASCII you just get away with it. No, the IDE is Eric and as far as I know it's python3 only. Weird. What if you put your code in try/except in the IDE: try: your code here except BaseException as e: print("exception: %s" % (e,)) raise just in case some esoteric exception is being swallowed silently by the IDE. Not that the error you cite should act that way... Just for interest I amended my code to use what you provided and tried it under IDLE. There aren't any errors but but my Arduino is not responding. Weird indeed. Try the try/except above and see if it reveals anything at all. However, if I enter python3 mycode.py then it works perfectly. I'm sure there's an explanation for this. I have thoney, another python IDE, on a raspberrypi I'll try that later and see what the result is. Anyway, it works from Eric and from the command prompt. So you need to know what your serial device expects. ASCII only? As it turns out, it doesn't matter if the data is ASCII or UTF-8. If you're only using ASCII valid characters then the byte sequences are the same, so you don't really know yet. mytext = "Fred" mytext = mytext + "\n" mybytes = mytext.encode('utf-8') ser.write(mybytes) Notice that I've appended the newline _before_ converting to bytes, Thank you for the code and the explanation, it's greatly appreciated. It's all a bit of an anticlimax really. Now that it works I don't know what to do with it. Like so many of my projects. Control something? As a remote to control a TV or PVR, using an IR adapter? The weather? Courier delivery times? So many possibilities... Cheers, Cameron Simpson (formerly c...@zip.com.au) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Converting a string to a byte array
On 25/09/17 07:26, Cameron Simpson wrote: Thank you Cameron and Peter for your replies. I don't understand why this works from the pyqt IDE but not when run from the console. I suppose the IDE is adding the correct encoding. I'm guessing the IDE is python 2 and not doing any encoding at all. In python 2 str _is_ effectively bytes and if you stay in ASCII you just get away with it. No, the IDE is Eric and as far as I know it's python3 only. Just for interest I amended my code to use what you provided and tried it under IDLE. There aren't any errors but but my Arduino is not responding. However, if I enter python3 mycode.py then it works perfectly. I'm sure there's an explanation for this. I have thoney, another python IDE, on a raspberrypi I'll try that later and see what the result is. Anyway, it works from Eric and from the command prompt. So you need to know what your serial device expects. ASCII only? As it turns out, it doesn't matter if the data is ASCII or UTF-8. mytext = "Fred" mytext = mytext + "\n" mybytes = mytext.encode('utf-8') ser.write(mybytes) Notice that I've appended the newline _before_ converting to bytes, Thank you for the code and the explanation, it's greatly appreciated. It's all a bit of an anticlimax really. Now that it works I don't know what to do with it. Like so many of my projects. -- Regards, Phil ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Converting a string to a byte array
On 24Sep2017 20:24, Phil wrote: I'm trying to do the same under pyqt but I'm having trouble converting a string to a byte array. This works correctly from the pyqt IDE but not from the console. python3 mycode.py generates "typeError: string argument without an encoding" Is it possible your pyqt IDE is using Python 2 instead of Python 3. In Python 2 strings are effectively byte arrays. It's very likely that my method of converting a string to a byte array is incorrect. This is my attempt: mytext = "Fred" mybytes = bytes(mytext) byte = byte + '\n' ser.write(mybytes) This can't be your code. I suspect the third line should be talking about "mybytes". I don't understand why this works from the pyqt IDE but not when run from the console. I suppose the IDE is adding the correct encoding. I'm guessing the IDE is python 2 and not doing any encoding at all. In python 2 str _is_ effectively bytes and if you stay in ASCII you just get away with it. I suspect utf8 is involved somewhere. In Python 3, strs contain Unicode code points, and when converting to bytes you need to specify how those code points are to be encoded, because a byte is 8 bits wide, not enough to store most code points. So you need to "encode" the str into bytes. So you need to know what your serial device expects. ASCII only? Some 8 bit coding like ISO8859-1 (covers a lotof Western Europe) or something else. For most modern environments the encoding will be UTF-8, but in serial environments that may not be the case depending on what is reading your data. Anyway, if you're only using ASCII your ok, because the ISO8859 codings and UTF-8 use the _same_ encoding for pure ASCII as ASCII does (1 to 1, each byte holding a value from 0-127 becing the ASCII code). So you could try UTF-8 if you don't know what your serial device expects. So: mytext = "Fred" mytext = mytext + "\n" mybytes = mytext.encode('utf-8') ser.write(mybytes) Notice that I've appended the newline _before_ converting to bytes, while we're still talking about "text" (the stuff that goes in a str). This will "just work". The risk is if your device can't cope with UTF-8. If it turns out that your device expects something more constrained (eg ASCII) you can put 'ascii' where I have 'utf-8'. That has the advantage that your code will raise an exception if you feed in something outside ASCII, which your device wouldn't have coped with anyway. Look up the documentation for what your device expects. If the documentation is silent on this, maybe use 'ascii' instead just on principle - better to notice in your program and decide what to do there than to feed garbage to your device and have is misbehave. Cheers, Cameron Simpson (formerly c...@zip.com.au) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Converting a string to a byte array
Phil wrote: > Thank you for reading this. > > The following code sends "Fred" to my serial connected device without > any problems. > > import serial > > ser = serial.Serial('/dev/ttyACM0',9600) > ser.write(b'Fred\n') > > > I'm trying to do the same under pyqt but I'm having trouble converting a > string to a byte array. This works correctly from the pyqt IDE but not > from the console. python3 mycode.py generates "typeError: string > argument without an encoding" > > It's very likely that my method of converting a string to a byte array > is incorrect. This is my attempt: > > mytext = "Fred" > mybytes = bytes(mytext) > byte = byte + '\n' > ser.write(mybytes) This is a bit of a mess. Always cut and paste actual code that you want to show. If there is an exception include the traceback. > I don't understand why this works from the pyqt IDE but not when run > from the console. I suppose the IDE is adding the correct encoding. I > suspect utf8 is involved somewhere. When you want to convert a string to a byte sequence it is your turn to decide about the encoding. With "Fred" even ASCII would work. When Python doesn't pick a default you have to specify it yourself, e. g. if you want to use UTF-8: >>> text = "Fred" >>> text.encode("utf-8") b'Fred' In Python 3 you cannot mix str and bytes, so to add a newline your options are >>> (text + "\n").encode("utf-8") # concatenate text b'Fred\n' >>> text.encode("utf-8") + b"\n" # concatenate bytes b'Fred\n' If you want to write only strings it may also be worth trying to wrap the Serial instance into a TextIOWrapper: # untested! import serial import io ser = serial.Serial('/dev/ttyACM0', 9600) serial_device = io.TextIOWrapper(ser) print("Fred", file=serial_device) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Converting a string to a byte array
Thank you for reading this. The following code sends "Fred" to my serial connected device without any problems. import serial ser = serial.Serial('/dev/ttyACM0',9600) ser.write(b'Fred\n') I'm trying to do the same under pyqt but I'm having trouble converting a string to a byte array. This works correctly from the pyqt IDE but not from the console. python3 mycode.py generates "typeError: string argument without an encoding" It's very likely that my method of converting a string to a byte array is incorrect. This is my attempt: mytext = "Fred" mybytes = bytes(mytext) byte = byte + '\n' ser.write(mybytes) I don't understand why this works from the pyqt IDE but not when run from the console. I suppose the IDE is adding the correct encoding. I suspect utf8 is involved somewhere. -- Regards, Phil ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor