Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.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 通过串行通信发送多个字节_Python_Arduino_Bytearray_Serial Communication - Fatal编程技术网

Python 通过串行通信发送多个字节

Python 通过串行通信发送多个字节,python,arduino,bytearray,serial-communication,Python,Arduino,Bytearray,Serial Communication,我将Arduino和Python3.4与pySerial一起使用,并试图通过串行通信发送多个字节,但由于我的代码正在运行,所以出现了问题,但出于调试目的,我在某些地方使用的打印语句正在输出我不期望的字符串。我已经尝试过编写这段代码的简化版本,并且它工作正常,所以我不确定我做错了什么! 另外,为了解决这个问题,我加入了一个if语句,它大大降低了代码的速度。任何关于如何简化代码以更快和/或如何发送和接收正确数据的建议都将不胜感激 Python: #sources: #http://stackover

我将Arduino和Python3.4与pySerial一起使用,并试图通过串行通信发送多个字节,但由于我的代码正在运行,所以出现了问题,但出于调试目的,我在某些地方使用的打印语句正在输出我不期望的字符串。我已经尝试过编写这段代码的简化版本,并且它工作正常,所以我不确定我做错了什么! 另外,为了解决这个问题,我加入了一个if语句,它大大降低了代码的速度。任何关于如何简化代码以更快和/或如何发送和接收正确数据的建议都将不胜感激

Python:

#sources:
#http://stackoverflow.com/questions/676172/full-examples-of-using-pyserial-package for more info
#http://pyserial.sourceforge.net/pyserial_api.html

import serial 
import time

#sets the connection parameters, relook at when know more
ser = serial.Serial(
    port ='COM4', 
    baudrate = 9600, 
    parity = serial.PARITY_ODD, 
    stopbits = serial.STOPBITS_TWO, 
    bytesize = serial.EIGHTBITS,
    timeout = 5
    )

def decToPercent (decimal):
    #maps value from 0-1 to 0-255 and makes into an interger, then to hex
    intPercent = round(decimal*255)
    return intPercent

ser.isOpen()    #returns true?

firstContact = False 
inByte = 0
wrist = 0.9         #setup of variables which I will get from dictionary of other code once integrate
elbow = 0       #assuming variables will be included in (0-1)   

wristPerc = decToPercent(wrist)
elbowPerc = decToPercent(elbow)

forceBytes = bytearray([wristPerc, elbowPerc])      #puts the motor values into an array as hex
print(forceBytes)

#set up initial contact with the arduino 
inByte = ser.read(1)
print(inByte)

while True:
    if (firstContact == False):

        if inByte == b'A' :
            firstContact = True
            ser.write('a'.encode())
            inByte = ser.read(1)

    else:

        ser.write(forceBytes)
        time.sleep(0.05)
        print(ser.readline())
最初,在使用此代码后,我启动了代码并返回了整个while语句,但是我将其取出,因为我认为这会使代码流稍微弄乱输出:

    #starts the process over again, clears so that have time to get information
    firstContact = False        
    inByte = ser.read(1)
    print(inByte)
Arduino代码:

//See Arduino Cookbook chapter 4 for info on sending and recieving multiple messages in one
//See example of serial call response for strategy on how to process information faster
int motorPinLeft = 10;
int motorPinRight = 11; 
int motorSpeedLeft = 0;
int motorSpeedRight = 0;

int fieldIndex = 1;
char values[2];    //array holding values for the fields we expect 
char newByte = 0;

void setup() {
  pinMode(motorPinLeft, OUTPUT);
  pinMode(motorPinRight, OUTPUT);
  Serial.begin(9600);
  while(!Serial){}; //waits for arduino to be ready, not neaded for duo
  establishContact();
}

void loop() 
{
  while (Serial.available()>0)    //checks to see if anything needs to be sent,
  {                            //denoted by recieveing signal from pySerial
      char inByte = Serial.read();

      switch (inByte)
      {
        case 97:
          inByte = 0;
          break;

        default:

          values[0] = inByte;
          //could just call Serial.read and asign 2nd byte, but loop allows to send more variables later
          if (fieldIndex < 2)
          {
            newByte = Serial.read();
            values[fieldIndex] = newByte;
            fieldIndex++;
          }

          else
          {
            Serial.print(values[0], DEC);    //debugging purposes 
            Serial.print(values[1], DEC);

            motorSpeedLeft = values[0];
            motorSpeedRight = values[1];

            //Serial.print(motorSpeedLeft);
            //Serial.print(motorSpeedRight);

            analogWrite(motorPinLeft, motorSpeedLeft);
            analogWrite(motorPinRight, motorSpeedRight);


            delay(3000);     //  to make the motor vibrate for 1 second

            motorSpeedRight = 0;
            motorSpeedLeft = 0;

            analogWrite(motorPinLeft, motorSpeedLeft);
            analogWrite(motorPinRight, motorSpeedRight);

            delay(1000);
          }
      }

      //Serial.print('A');


  }
}


//allows arduino to be ready before pySerial sends any messages
void establishContact()
{
  while (Serial.available()<=0) 
  {
    Serial.print('A');
    delay(300);
  }
}
每次出现打印语句时,我的马达都在运行,所以我不确定是不是打印语句弄乱了,而不是实际的串行通信


我不确定的另一件事是,在arduino代码中的switch case语句中,我使用ascii代码表示“a”(97),而不是实际输入“a”。这两种方法都可以使用吗?

在进行了更多的修改之后,我通过在arduino代码中从Serial.print()切换到Serial.write()获得了打印正确输出的代码,并且因为我非常确定这是主要问题,所以我回到了我的原始代码,没有使用if语句来缩短代码运行时间。我在打印这两个输出时确实遇到了一些问题,但通过以下方式:

delay(10) 

在Serial.write语句之间,它修复了这个问题

“我用ascii码来表示‘a’(97),而不是实际的输入‘a’。两者都应该工作吗?”是的,因为它们完全一样。
delay(10)