Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.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,因此,我有一个python程序,它使用pySerial通过串口与我的arduino uno通信: upload_数组=[0,1,0,0] 串行=串行。串行('COM4',9600) 尽管如此: 做一件事 SERIAL.write(b“f'{upload\u array[0]},{upload\u array[1]},{upload\u array[2]},{upload\u array[3]}\n') 我的arduino有这样的代码来读取传入数组: int incoming_array[4];

因此,我有一个python程序,它使用pySerial通过串口与我的arduino uno通信:

upload_数组=[0,1,0,0]
串行=串行。串行('COM4',9600)
尽管如此:
做一件事
SERIAL.write(b“f'{upload\u array[0]},{upload\u array[1]},{upload\u array[2]},{upload\u array[3]}\n')
我的arduino有这样的代码来读取传入数组:

int incoming_array[4];

void loop(){
  
  if (Serial.available() > 4){
    
    String incoming = Serial.readStringUntil('\n');
    for (int i; i < 4; i++){
      incoming_array[i] = incoming.substring(i,i).toInt();
    }
   }
   do_a_thing();
}
int传入_数组[4];
void循环(){
如果(Serial.available()>4){
字符串传入=Serial.readStringUntil('\n');
for(int i;i<4;i++){
传入的_数组[i]=传入的.substring(i,i).toInt();
}
}
做一件事;
}
但是,当我尝试对数组执行任何操作时,它的行为就像它是空的一样。 我能做些什么来解决这个问题

编辑:拼写错误的方法

我遇到了这样的问题。 以下是我在arduino论坛上的解决方案:

但下面是一个示例代码:

阿杜伊诺:

//Arduino forum 2020 - https://forum.arduino.cc/index.php?topic=714968


int myArray[15]; //this value is the upgratable data
byte* 

ddata = reinterpret_cast<byte*>(&myArray); // pointer for transferData()
size_t pcDataLen = sizeof(myArray);
bool newData=false;

void setup() {
    Serial.begin(115200);//baudrate
}

void loop() {
    checkForNewData();
    if (newData == true) {
        newData = false;
    }
    toPy(myArray[0],myArray[1],myArray[2],myArray[3],myArray[4],myArray[5],myArray[6],myArray[7],myArray[8],
    myArray[9],myArray[10],myArray[11],myArray[12],myArray[13],myArray[14],0,0,0,random(100)); //here write the send data
    }

void checkForNewData () {
    if (Serial.available() >= pcDataLen && newData == false) {
        byte inByte;
        for (byte n = 0; n < pcDataLen; n++) {
            ddata [n] = Serial.read();
        }
        while (Serial.available() > 0) { // now make sure there is no other data in the buffer
             byte dumpByte =  Serial.read();
             Serial.println(dumpByte);
        }
        newData = true;
    }
}


void toPy(int a,int b,int c,int d,int e,int f,
          int g,int h,int i,int j,int k,int l,
          int m,int n,int o,int p,int q,int r,int s)//19 datas
{
  //rpidata="[1,2,3,4,5,6,7,8,9,10,111,0]";
String data="["+String(a)+","+String(b)+","+String(c)+","+String(d)+","+String(e)+","+String(f)+","+String(g)+","+String(h)+","+String(i)+","+
String(j)+","+String(k)+","+String(l)+","+String(m)+","+String(n)+","+String(o)+","+String(p)+","+String(q)+","+String(r)+","+String(s)+"]";
  delay(50);Serial.println(data);
}
如果您发送的不是int,或者不是15个元素,请在(ser.write)行中进行写操作,例如“20l”。请阅读以下内容:


祝你好运

这甚至不应该编译。没有函数替代。请检查您的代码是否有拼写错误,然后再发布到这里。是否有任何特定的原因使您将0和1的数组作为字符串发送?只需发送这些数字。你可以看看这个。@Pieget我正在发送1和0的数组来告诉arduino哪些管脚可以调高,哪些管脚可以调低。每个管脚对应于阵列的索引,因此arduino程序读取阵列并打开或关闭相应的指示灯(该系统工作正常,我制作了一个预设阵列,没有使用任何串行通信,程序正常工作。)我将它们作为字符串发送,因为pySerial不支持println(),所以我必须以字节的形式发送它。但是,我不知道如何在arduino上将字节转换为整数,所以我发送了一个字符串。
#Arduino forum 2020 - https://forum.arduino.cc/index.php?topic=714968

import serial
from struct import *
import sys
import time
import random
import ast


try:
    ser=serial.Serial(baudrate='115200', timeout=.5, port='com8')    #!!!
except:
    print('Port open error')

time.sleep(5)#no delete!
while True:
    try:
        ser.write(pack ('15h',0,1,2,3,4,666,6,7,444,9,10,2222,12,13,random.randint(0,100)))#the 15h is 15 element, and h is an int type data
                                                                    #random test, that whether data is updated
        time.sleep(.01)#delay
        dat=ser.readline()#read a line data
        
        if dat!=b''and dat!=b'\r\n':
            try:                #convert in list type the readed data
                dats=str(dat)
                dat1=dats.replace("b","")
                dat2=dat1.replace("'",'')
                dat3=dat2[:-4]
                list_=ast.literal_eval(dat3) #list_ value can you use in program
                print(dat3)
            except:
                print('Error in corvert, readed: ', dats)
        time.sleep(.05)
    except KeyboardInterrupt:
        break
    except:
        print(str(sys.exc_info())) #print error
        break

#the delays need, that the bytes are good order