Sending string to serial.to_bytes not working
<p>I am trying to send a string variable contains the command.</p>

<p>Like this:</p>

<pre><code>value="[0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]"
self.s.write(serial.to_bytes(value))
</code></pre>

<p>The above one fails. Won't give any error.</p>

<p>But it's working when I send a value like this:</p>

<pre><code>self.s.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]))
</code></pre>

<p>I also tried sending string like this:</p>

<pre><code>self.s.write(serial.to_bytes(str(value)))
</code></pre>

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

<p>I want to do this thing:</p>

<pre><code>value="[0x"+anotherstring+",0x"+string2+"0x33, 0x0a]"
</code></pre>

<p>and send the value.</p>

<p>Thanks!</p>

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

<p>Detailed steps:</p>

<ol>
<li><p>Open a python interpreter</p></li>
<li><p>Import <code>serial</code>and open a serial port, call it <code>ser</code>.</p></li>
<li><p>Copy the code below and paste it in the python interpreter:</p></li>
</ol>

<p>The code:</p>

<pre><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))
</code></pre>

<p>It will have the same effect that just this:</p>

<pre><code>ser.write(serial.to_bytes([0x31, 0x0a, 0x32, 0x0a, 0x33, 0x0a]))
</code></pre>

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

python pyserial