Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing commands to serial
#9
Thank you for this useful reply. However -

I am afraid that the result of your illustration is ambiguous. You have included so many brackets and quote marks that I have no idea how cmd ends up, so what pyserial encodes and sends. You cannot assume that the naive user (me) understands the detailed syntax or how it is interpreted by the compiler, especially as the questioner also failed to understand the syntax.

Is cmd = "Enter command or 'exit':"\r\n
or cmd = input("Enter command or 'exit':")\r\n
or cmd = Enter command or 'exit':\r\n
or cmd = what?

And if cmd1 = G0 X11.4158 Y0.5132
rtnl = \r\n
can I state
cmd2 = cmd1 + rtnl
to create
cmd2 = G0 X11.4158 Y0.5132\r\n

and is that the same as
cmd2 = 'G0 X11.4158 Y0.5132' + '\r\n'
?

(Jan-30-2018, 04:25 PM)DeaD_EyE Wrote: You can not concatenate bytes with strings.
Just concatenate them, when they are still str:

cmd = input("Enter command or 'exit':") + '\r\n'
...
ser.write(cmd.encode())
PS: cmd.encode('ascii') + b'13' + b'10' is wrong.

You have following possibilities:
your_str.encode() + b'\x0d' + b'\x0a'
your_str.encode() + bytes([13, 10])
your_str.encode() + bytes([0x0d, 0x0a])

or with a str:

end = '\x0d\x0a'
cmd.encode() + end.encode()


I think the best way is, to do all formatting and concatenation with strings and when you need them to send them as bytes, do it at this place directly without any additional stuff like concatenation.
Reply


Messages In This Thread
Writing commands to serial - by python_beginner - Jan-30-2018, 02:41 PM
RE: Writing commands to serial - by sparkz_alot - Jan-30-2018, 04:04 PM
RE: Writing commands to serial - by DeaD_EyE - Jan-30-2018, 04:25 PM
RE: Writing commands to serial - by cvh - Jun-23-2020, 08:59 PM
RE: Writing commands to serial - by sparkz_alot - Jan-30-2018, 08:29 PM
RE: Writing commands to serial - by python_beginner - Feb-01-2018, 01:19 PM
RE: Writing commands to serial - by dalwood - Feb-01-2018, 03:48 PM
RE: Writing commands to serial - by DeaD_EyE - Feb-02-2018, 10:10 AM
RE: Writing commands to serial - by jambuna35 - Feb-03-2019, 09:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 4,168 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  Trouble writing over serial EngineerNeil 1 2,647 Apr-07-2019, 08:17 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020