Python Arduino纳米连接?

Python Arduino纳米连接?,python,arduino,Python,Arduino,下面的代码应该将一个数字转换成二进制代码,然后连接arduino nano,根据哪个二进制代码,4盏灯应该亮起。如果二进制代码为1,则所有指示灯只应点亮。在控制台中它可以工作,但我的函数没有连接到arduino nano 我的Arduino nano上的代码可以很好地工作,同时可以手动输入 有人能帮我吗 import time import serial ser = serial.Serial('COM6',115200, timeout=0.5) number = input('A dec

下面的代码应该将一个数字转换成二进制代码,然后连接arduino nano,根据哪个二进制代码,4盏灯应该亮起。如果二进制代码为1,则所有指示灯只应点亮。在控制台中它可以工作,但我的函数没有连接到arduino nano

我的Arduino nano上的代码可以很好地工作,同时可以手动输入

有人能帮我吗

import time
import serial

ser = serial.Serial('COM6',115200, timeout=0.5)

number = input('A decimal number please:',)

def decimalToBinary(num):       # decimal into a binary code
    return bin(num)

                    #[2:] zur entfernung von 0b

while True:
    data = ser.readline()     # try to get the connection to arduino nano

    index = 0
while index < len(bin):      # separate the binary code in individual numbers
    binary = bin[index]
    print(binary)

def light_up():

   if index[0] == 1:
       ser.write(b'u')          #light up the first light

   if index[1] == 1:
       ser.write(b'd')       # light up the second light

   if index[2] == 1:
      ser.write(b't')      # light up the third light

   if index[3] == 1:
      ser.write(b'c')      # light up the fourth light
导入时间
导入序列号
ser=串行。串行('COM6',115200,超时=0.5)
数字=输入('请输入十进制数字:',)
def decimalToBinary(num):#十进制转换为二进制代码
返回仓(个)
#[2:]zur entfernung von 0b
尽管如此:
data=ser.readline()#尝试连接到arduino nano
索引=0
而index

light_up()

我认为避免字符发送,而是发送一个带有pin值的数组。例如,你可以发送模拟值和其他复杂的数据

如果我不理解你的问题,请发表评论。 下面是一个示例代码:

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


int myArray[4]; //this value is the upgratable data !!lenght of 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;
    }
    digitalWrite(2,myArray[0]);
    digitalWrite(3,myArray[1]);//etc
    }

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;
    }
}

虽然为True:data=ser.readline()
我对Python知之甚少,但这会停止吗?我认为这是一个无穷无尽的过程。但是我已经尝试过不使用while循环,它也不起作用。这只是一次尝试。
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 ('4h',0,1,0,0))#the 4h is 4 element, and h is an int type data (read the documentation, and use bool variable, if not good work), and 0,1,0,0 will be the variables of myArray
        
        time.sleep(.1)#delay
    except KeyboardInterrupt:
        break
    except:
        print(str(sys.exc_info())) #print error
        break

#the delays need, that the bytes are good order