Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的Arduino串行数据处理_Python_Arduino - Fatal编程技术网

Python中的Arduino串行数据处理

Python中的Arduino串行数据处理,python,arduino,Python,Arduino,我是Arduino的新手,我正在尝试制作一个LED带控制器。我正在用Python将串行数据发送到Arduino。我用三个刻度来控制LED的颜色 我的问题是: 我应该像Python示例代码中那样发送颜色字节,还是应该单独发送颜色字节,或者以任何其他方式发送颜色字节?发送的字节如下所示:b'255 32 28' 如何将此字节转换为独立整数或列表? 例如:b'255 32 28',toint list[4]={255,32,28} Python示例代码: from tkinter import *

我是Arduino的新手,我正在尝试制作一个LED带控制器。我正在用Python将串行数据发送到Arduino。我用三个刻度来控制LED的颜色

我的问题是:

  • 我应该像Python示例代码中那样发送颜色字节,还是应该单独发送颜色字节,或者以任何其他方式发送颜色字节?发送的字节如下所示:b'255 32 28'

  • 如何将此字节转换为独立整数或列表? 例如:b'255 32 28',toint list[4]={255,32,28}

Python示例代码:

from tkinter import *
import serial
import serial.tools.list_ports as ports

master = Tk()
master.geometry('400x300')

for ee in list(ports.comports()):
                if ee.serial_number=='557363134383519112E0':
                    usb=ee.device
ser=serial.Serial(usb,baudrate=9600)

def getThrottle():
    data=str(r.get())+' '+str(g.get())+' '+str(b.get())+' '
    data=bytes(str(data),'utf8')
    ser.write(data)

r = Scale(master,from_=255,to=0)
r.place(x=50,y=100)

g = Scale(master, from_=255, to=0)
g.place(x=150,y=100)

b=Scale(master, from_=255, to=0)
b.place(x=250,y=100)

gomb=Button(master,command=getThrottle,text='Send to led strip')
gomb.place(x=150,y=250)

master.mainloop()
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, 12, NEO_GRB + NEO_KHZ800);

String inByte;
char str;
int sa[4], r=0, t=0;

void setup() 
{
  Serial.begin(9600);
  strip.begin();
  strip.show();
}

void loop() {
  if (Serial.available() > 0)
  {  
       inByte = Serial.readString();  
       int str_len = inByte.length() + 1; 
       char char_array[str_len];
       inByte.toCharArray(char_array, str_len);
           for (int i=0; i < inByte.length(); i++)
  { 
   if(inByte.charAt(i) == ' ') 
    { 
      sa[t] = inByte.substring(r, i).toInt(); 
      r=(i+1); 
      t++; 
    }
   }
  } 
  strip.setPixelColor(1, sa[0],sa[1],sa[2]); 
  strip.show();
}  
Arduino示例代码:

from tkinter import *
import serial
import serial.tools.list_ports as ports

master = Tk()
master.geometry('400x300')

for ee in list(ports.comports()):
                if ee.serial_number=='557363134383519112E0':
                    usb=ee.device
ser=serial.Serial(usb,baudrate=9600)

def getThrottle():
    data=str(r.get())+' '+str(g.get())+' '+str(b.get())+' '
    data=bytes(str(data),'utf8')
    ser.write(data)

r = Scale(master,from_=255,to=0)
r.place(x=50,y=100)

g = Scale(master, from_=255, to=0)
g.place(x=150,y=100)

b=Scale(master, from_=255, to=0)
b.place(x=250,y=100)

gomb=Button(master,command=getThrottle,text='Send to led strip')
gomb.place(x=150,y=250)

master.mainloop()
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, 12, NEO_GRB + NEO_KHZ800);

String inByte;
char str;
int sa[4], r=0, t=0;

void setup() 
{
  Serial.begin(9600);
  strip.begin();
  strip.show();
}

void loop() {
  if (Serial.available() > 0)
  {  
       inByte = Serial.readString();  
       int str_len = inByte.length() + 1; 
       char char_array[str_len];
       inByte.toCharArray(char_array, str_len);
           for (int i=0; i < inByte.length(); i++)
  { 
   if(inByte.charAt(i) == ' ') 
    { 
      sa[t] = inByte.substring(r, i).toInt(); 
      r=(i+1); 
      t++; 
    }
   }
  } 
  strip.setPixelColor(1, sa[0],sa[1],sa[2]); 
  strip.show();
}  
#包括
Adafruit_Neopix条=Adafruit_Neopix条(60,12,NEO_GRB+NEO_KHZ800);
字符串单位;
char-str;
int-sa[4],r=0,t=0;
无效设置()
{
Serial.begin(9600);
strip.begin();
strip.show();
}
void循环(){
如果(Serial.available()>0)
{  
inByte=Serial.readString();
int str_len=inByte.length()+1;
字符数组[str_len];
toCharArray(字符数组,str_len);
对于(int i=0;i

有什么好的建议吗?

现在您发送的是:RRR GGG BBB

我建议添加一些分隔符,让您的Arduino能够更轻松地解码。如: &RRR GGG BBB

当您的arduino看到一个“&”时,它知道存储数据直到看到一个“!”。当它出现时,您就知道您已经有了一个完整的数据集。从此处,拆分“”上的数据