OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Python serial send byte array with byte values

  • Thread starter Thread starter Andrew
  • Start date Start date
A

Andrew

Guest
I would like to send such array for example via pyserial:

Code:
[49, 56, 48] or [82]

I tried such solution before sending:

Code:
print(list(str(180).encode()))

And it gives me array as I want. But when I tried to send it via pySerial, it said that I need to have buffer instead of list. In general I'm trying to get some analogue from kotlin:

Code:
byteArrayOf('O'.code.toByte())


"70".toByteArray()

Which gives me such arrays for sending them to Arduino. I tried sending maps, dicts, and so on, but did not succeed. Whether it's possible to have something similar in Python?

Arduino code for communication:

Code:
byte chosed_color;
byte incoming_byte;
int val = -1;  
void setup() 
{
  Serial.begin(9600);
  Serial.setTimeout(3);
...
}

void loop() 
{
  if(Serial.available() > 0) 
  {
    incoming_byte = Serial.peek();
    if(incoming_byte >= '0' && incoming_byte <= '9')
    {
      val = Serial.parseInt() ;
      
    }
    else
    {
      incoming_byte = Serial.read();
      switch(incoming_byte)
      {
      case 'R':
chosed_color = rPin;
        break;
      case 'G':...
      case 'B':...
      case 'O':...
      }
    }
  }
}
<p>I would like to send such array for example via pyserial:</p>
<pre><code>[49, 56, 48] or [82]
</code></pre>
<p>I tried such solution before sending:</p>
<pre><code>print(list(str(180).encode()))
</code></pre>
<p>And it gives me array as I want. But when I tried to send it via pySerial, it said that I need to have buffer instead of list. In general I'm trying to get some analogue from kotlin:</p>
<pre><code>byteArrayOf('O'.code.toByte())


"70".toByteArray()
</code></pre>
<p>Which gives me such arrays for sending them to Arduino. I tried sending maps, dicts, and so on, but did not succeed. Whether it's possible to have something similar in Python?</p>
<p>Arduino code for communication:</p>
<pre><code>byte chosed_color;
byte incoming_byte;
int val = -1;
void setup()
{
Serial.begin(9600);
Serial.setTimeout(3);
...
}

void loop()
{
if(Serial.available() > 0)
{
incoming_byte = Serial.peek();
if(incoming_byte >= '0' && incoming_byte <= '9')
{
val = Serial.parseInt() ;

}
else
{
incoming_byte = Serial.read();
switch(incoming_byte)
{
case 'R':
chosed_color = rPin;
break;
case 'G':...
case 'B':...
case 'O':...
}
}
}
}
</code></pre>
 

Latest posts

M
Replies
0
Views
1
MOHAMED AMIIN ABDI AADAN
M
Top