1

I am trying to send a string variable contains the command.

Like this:

value="[0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]"
self.s.write(serial.to_bytes(value))

The above one fails. Won't give any error.

But it's working when I send a value like this:

self.s.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]))

I also tried sending string like this:

self.s.write(serial.to_bytes(str(value)))

Still not working. Can someone please let me know how to send the value by storing in string?

I want to do this thing:

value="[0x"+anotherstring+",0x"+string2+"0x33, 0x0a]"

and send the value.

Thanks!

arslion
  • 409
  • 6
  • 16

2 Answers2

4

serial.to_bytes takes a sequence as input. You should remove double quotes around value to pass a sequence of integers instead of a str representing the sequence you want to pass:

value = [0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]
self.s.write(serial.to_bytes(value))  # works now

In the first case, you sent a sequence of bytes representing "[0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]". Now, you will send the sequence [0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a] as expected.


If you want to send a string, just send it as bytes:

# Python 2
self.s.write('this is my string')
text = 'a string'
self.s.write(text)

# Python 3
self.s.write(b'this is my string')
text = 'a string'
self.s.write(text.encode())

And for a sequence:

for value in values:
    # Python 2
    self.s.write(value)

    # Python 3
    self.s.write(value.encode())
Tiger-222
  • 6,677
  • 3
  • 47
  • 60
  • `str` is also a sequence... it just so happens that the representation of a list of ints with hex notation is different than the actual list itself... – Jon Clements Sep 10 '16 at 10:34
  • I want to join multiple values like this `value="[0x"+anotherstring+",0x"+string2` how can i do this? – arslion Sep 10 '16 at 10:43
  • @JonClements yes I know that, but in this case, it is not what arslion is seeking for. – Tiger-222 Sep 10 '16 at 11:35
  • @Tiger-222 if they're only able to receive the data as a string and it's not hard-coded that way so they can remove the quote marks, then I can only guess the OP is after `ast.literal_eval` or else the answer is literally - don't send it as a string :) – Jon Clements Sep 10 '16 at 11:45
  • So there is no other way to attach a string to a serial value? – arslion Sep 11 '16 at 18:57
  • @arslion can you clarify your needs please? – Tiger-222 Sep 15 '16 at 08:16
  • @Tiger-222 want to do this thing: value="[0x"+anotherstring+",0x"+string2+"0x33, 0x0a]" – arslion Sep 15 '16 at 08:18
  • Updated my answer. Is it what you are looking for? – Tiger-222 Sep 15 '16 at 08:29
  • @Tiger-222 I don't know how to make you understand. I have this `310a320a330a` hex data converted from a string using `encode('hex')` I want to convert hex data to this: `[0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]` and then send it as a variable to this `s.write(serial.to_bytes(value))` – arslion Sep 16 '16 at 09:22
3

If passing a list of integers works for you then just convert your hexadecimal representations into integers and put them in a list.

Detailed steps:

  1. Open a python interpreter

  2. Import serialand open a serial port, call it ser.

  3. Copy the code below and paste it in the python interpreter:

The code:

command = '310a320a330a'
hex_values = ['0x' + command[0:2],  '0x' + command[2:4],
              '0x' + command[4:6],  '0x' + command[6:8],
              '0x' + command[8:10], '0x' + command[10:12]]
int_values = [int(h, base=16) for h in hex_values]
ser.write(serial.to_bytes(int_values))

It will have the same effect that just this:

ser.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]))

actually you can test that int_values == [0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a] is True so you are writing exactly the same thing.

Stop harming Monica
  • 12,141
  • 1
  • 36
  • 56
  • I have this `310a320a330a` hex data converted from a string using `encode('hex')` I want to convert hex data to this: `[0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]` and then send it as a variable to this `s.write(serial.to_bytes(value))` – arslion Sep 16 '16 at 09:18
  • That's exactly what the code in my answer does except (a) I used the name `int_values` instead of `value` for clarity, feel free to change it to your heart's content (b) I assumed your hex data is already split into individual bytes because that's how it appears to be in your question, feel free to edit it if you think something is missing. – Stop harming Monica Sep 16 '16 at 10:53
  • done exactly the same thing you did, gives me syntax error on Python 2.7 @goyo – arslion Sep 18 '16 at 19:11
  • @arslion you must have done something different, I double checked and there are no syntax errors in the code I posted. Of course you will get a `NameError` if `string1`, `string2` and `self`are undefined, but no syntax errors. – Stop harming Monica Sep 18 '16 at 22:44
  • string is defined already. `sendcmd=[0x+command[0:2],0x+command[2:4],0x+command[4:6],0x+command[6:8],0x+command[8:10],0x+command[10:12],0x+command[12:14],0x0D] ^ SyntaxError: invalid token` .This is the error – arslion Sep 19 '16 at 10:27
  • Yes, that's invalid syntax but it's not what I did. I guess you want `sendcmd=["0x"+command[0:2],"0x"+command[2:4],"0x"+command[4:6],"0x"+command[6:8],"0x"+command[8:10],"0x"+command[10:12],"0x"+command[12:14],"0x0D"]` instead. – Stop harming Monica Sep 19 '16 at 11:10
  • it gave me this error: `SyntaxError: Non-ASCII character '\xe2' in file MKSreader1.py on line 49, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details ` – arslion Sep 19 '16 at 11:54
  • Well what do you expect me to do about it? I have no idea what's in file MKSreader1.py on line 49. Anyway you should be declaring the encoding in your source code files as explained in the linked document but that has nothing to do with your question. – Stop harming Monica Sep 19 '16 at 12:07
  • The code is working when i put the command directly into it. `ser.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]))` i want to do this the other way. If you know how to, then tell me, otherwise don't do anything :) – arslion Sep 19 '16 at 12:12