python - Sending multiple bytes through Serial Communication -


i using arduino , python 3.4 pyserial , trying send multiple bytes through serial communication, having issues since code running, print statements have in places debugging purposes outputting strings not expecting. have tried writing simplified version of code , works correctly, not sure doing wrong! also, in attempt fix this, put in if statement in has slowed down code lot. suggestions on how streamline code faster and/or how send , receive right data appreciated!

python:

#sources: #http://stackoverflow.com/questions/676172/full-examples-of-using-pyserial-package more info #http://pyserial.sourceforge.net/pyserial_api.html  import serial  import time  #sets connection parameters, relook @ 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 0-1 0-255 , makes interger, hex     intpercent = round(decimal*255)     return intpercent  ser.isopen()    #returns true?  firstcontact = false  inbyte = 0 wrist = 0.9         #setup of variables dictionary of other code once integrate elbow = 0       #assuming variables included in (0-1)     wristperc = dectopercent(wrist) elbowperc = dectopercent(elbow)  forcebytes = bytearray([wristperc, elbowperc])      #puts motor values array hex print(forcebytes)  #set initial contact 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()) 

initially had code start , go through whole while statement after using code, took out because think making flow of code mess outputs little:

    #starts process on again, clears have time information     firstcontact = false             inbyte = ser.read(1)     print(inbyte) 

arduino code:

//see arduino cookbook chapter 4 info on sending , recieving multiple messages in 1 //see example of serial call response strategy on how process information faster int motorpinleft = 10; int motorpinright = 11;  int motorspeedleft = 0; int motorspeedright = 0;  int fieldindex = 1; char values[2];    //array holding values fields expect  char newbyte = 0;  void setup() {   pinmode(motorpinleft, output);   pinmode(motorpinright, output);   serial.begin(9600);   while(!serial){}; //waits arduino ready, not neaded duo   establishcontact(); }  void loop()  {   while (serial.available()>0)    //checks see if needs sent,   {                            //denoted recieveing signal pyserial       char inbyte = serial.read();        switch (inbyte)       {         case 97:           inbyte = 0;           break;          default:            values[0] = inbyte;           //could call serial.read , asign 2nd byte, loop allows 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);     //  make motor vibrate 1 second              motorspeedright = 0;             motorspeedleft = 0;              analogwrite(motorpinleft, motorspeedleft);             analogwrite(motorpinright, motorspeedright);              delay(1000);           }       }        //serial.print('a');     } }   //allows arduino ready before pyserial sends messages void establishcontact() {   while (serial.available()<=0)    {     serial.print('a');     delay(300);   } } 

output:

bytearray('b\xb2\x00') b'a' b'0k\xfe' b'-\x13j10k\xfe' b'-\x13j10k\xfe' 

my motor running each time print statement occurs, not sure if print statements messing up, rather actual serial communication?

one additional thing unsure of, in switch-case statement in arduino code, using ascii code 'a' (97) instead of putting 'a'. should both work?

after more messing around, got code print out right outputs switching serial.print() serial.write() in arduino code, , since i'm pretty sure major issue, went original code, without if statement cut down time took run code. did have little trouble printing out both outputs, putting:

delay(10)  

in between serial.write statements, fixed that.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -