Arduino-Python : Serial.read() problem...


good evening,
i working on project , since have been trying solve following problem 2 days....  =(
i need python programme send command, characters arduino. these characters must stored  in arduino in order used later. however, although sent characters seem correct, arduino reads '\xff'

here python code :

code: [select]

#!/usr/bin/python3
# -*-coding:latin-1 -*

                                   
from algolambda import *
import serial
import time


ser = serial.serial(port= 'com4:', baudrate = 115200, timeout=2) #initialize serial comms @ 115200 baud

ser.close()
ser.open()

continuer = 'y'

if(ser.isopen()): #test whether port open

   while(continuer == 'y'):
       angstrom = int(input("please select wavelength...")) #choose wavelength
       print("type of angstrom : " + str(type(angstrom))) #checking type of angstrom parameter
##        if (lambdape(angstrom) none):
##            print("aucune valeur de retour lambdape()")
##            return none


##                  set binmod , binpe parameter

           
       pe = lambdape(angstrom)#        shall angstrom // 256
       print("pe : " + str(pe)) #checking
 
       binmod, binpe = convbin(angstrom)      
 
       chainepe = ""
       for in binpe:
           chainepe = chainepe+str(i) # converting pe string
       print("binpe: "+ chainepe)


##                  command arduino : send chainepe must stored in arduino

       
       ser.write(b'p')
       ser.write(bytes(str(chainepe),'latin-1'))

##                      reading on arduino's serial monitor

       try:
           line = "1"
           j=0
           while(j<5): # in case buffer problem
               line = ser.readline() #reading on arduino's serial monitor
               print(line)
               j+=1
       except(keyboardinterrupt,systemexit): #leaving pgr = ctrl+c
           ser.close() # closing  port
       except: # other interruptions
           print("erreur inconnue")

       if (lambdamodulo(angstrom) == none):
           exit("aucune valeur de retour lambdamodulo()")
   
       modulo = lambdamodulo(angstrom)
       print("modulo du codage de longueur d'onde : " + str(modulo)) # checking whether have angstrom%256
       
       chainemod = ""      #initializing
       for in binmod:        #converting list string
           chainemod = chainemod + str(i)                


##              command arduino : send chainemod must stored in arduino

       
       print("binmod: "+ chainemod)    #checking
       ser.write(b'm')     #sending command arduino
       ser.write(bytes(chainemod,'latin-1'))   #sending bytes them arduino


##                      reading on arduino's serial monitor


       try:
           line = "1"
           j=0
           while(j<5):
               line = ser.readline()
               print(line)
               j+=1
       except(keyboardinterrupt,systemexit):
           ser.close()
       except:
           print("erreur inconnue")
           
       continuer = input("voulez-vous continuer ? y/n\n")



here algolambda.py :

code: [select]


#!/usr/bin/python3
# -*- coding:latin-1 -*-

def lambdape(angstrom):
"lambdape() calcule la partie entière de la longueur d'onde choisie"
if(angstrom>10000 or angstrom<=0):
print("choix de longueur d'onde hors limite")
return none
else:
angstrom = angstrom//256

return angstrom


def lambdamodulo(angstrom):
"lambdamodulo() calcule la longueur d'onde choisie modulo 256"
if(angstrom>10000 or angstrom<=0):
print("choix de longueur d'onde hors limite")
return none
else:
angstrom = angstrom%256

return angstrom


def convbin(angstrom):
"convbin() converts angstroms en binaries"
angstrompe = lambdape(angstrom)
angstrommod = lambdamodulo(angstrom)

binpe = []; binmod = []

for in [0,1,3,4,5,6,7]:
binmod.append(angstrommod & 1)
binpe.append(angstrompe & 1)
#print(binmod); print(binpe); print()
angstrommod = angstrommod >> 1;
angstrompe = angstrompe >> 1

print(binmod); print(binpe)

return binmod,binpe



now... arduino code... :

code: [select]


 int modulo[9]= {0,0,0,0,0,0,0,0,0}; //to store lambda_final%256
 int pe[9]  = {0,0,0,0,0,0,0,0,0}; // store lambda_final//256
 
 void setup()
{
 serial.begin(115200);
 serial.println("\ntest communication python/arduino : set lambda");
}

void get_modulo()
{
//  if(serial.available()>0){              //serial.available() seems = 0.. unknown reasons
   char temp[8];                          //to store incoming bytes... , should temp[9] way...  
   for(int i=0; i<8; i++){
     temp[i] = serial.read();
     serial.print(temp[i]);              //this returns \xff\xff\xff....
   }
//  }
 serial.println("");
}

void get_partie_entiere()
{
//.  if(serial.available()>0){            //serial.available() seems = 0.. unknown reasons
    char temp[8];                        //to store incoming bytes... , should temp[9] way...
   serial.print("partie entiere:");
   for(int i=0; i<8; i++){
     temp[i] = serial.read();
     serial.print(temp[i]);            //this returns \xff\xff\xff....
   }
   serial.println("debug");
 //}
 serial.println("");
}


void loop()
{
                               //waiting commands on serial port python program
 if (serial.available()>0)
 {
   char inbyte = serial.read();
   switch(inbyte)
   {
     case 'm':
       get_modulo();
       break;
       
     case 'p':
       get_partie_entiere();
       break;
   }
   serial.flush(); //waits transmission of outgoing serial data complete
 }
}  



and here :

please select wavelength...3
type of angstrom : <class 'int'>
pe : 0
[1, 1, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
binpe: 0000000
b'\n'
b'test communication python/arduino : set lambda\r\n'
b'partie entiere:\xff\xff\xff\xff\xff\xff\xff\xffdebug\r\n'             //here supposed binpe, 0000000
b'\r\n'
b''
modulo du codage de longueur d'onde : 3
binmod: 1100000
b'\xff\xff\xff\xff\xff\xff\xff\xff\r\n'                                                   //here supposed binmod, 1100000
b''                                                                    
b''
b''
b''
voulez-vous continuer ? y/n


i tried communicate arduino through simple code. following example working expected :

python code :

code: [select]


#!/usr/bin/python3
# -*-coding:latin-1 -*

                       
# sending chars arduino through python, , print them on arduino's serial monitor
# program reads printed on serial monitor et prints in shell

import serial
import time


ser = serial.serial(port= '/com4:', baudrate = 9600, timeout=2) #définition de la liaison série

ser.close()
ser.open()

continuer = 'y'
mot="101"
while(continuer == 'y'):
   if(ser.isopen()): #test que le port est ouvert
       ser.write(b"w")
       ser.write(bytes(str(mot),'latin-1')) #on envoie une donnée à l'arduino

       if(continuer =='y'):
           line = ser.readline() #lecture d'une ligne sur le moniteur série
           print(line) #on écrit la ligne du moniteur série sur le terminal
       continuer = input("voulez-vous continuer ? y/n\n")

ser.close()



arduino code :

code: [select]


char temp[3];
 void setup()
{
 serial.begin(9600);
 serial.println("\ntest communication python/arduino : serial monitor");
}

void loop()
{
 if (serial.available()>0)
 {
   char inbyte = serial.read();
   serial.flush();
   switch(inbyte)
   {
     case 'w':
       for(int i=0; i<3; i++){
         temp[i] = serial.read();
         serial.print(temp[i]);
       }
   }
     serial.println("");
 }
 delay(3000);
}



result :

b'\n'
voulez-vous continuer ? y/n
y
b'test communication python/arduino : serial monitor\r\n'
voulez-vous continuer ? y/n
y
b'101\r\n'
voulez-vous continuer ? y/n
y
b'101\r\n'
voulez-vous continuer ? y/n
y
b'101\r\n'
voulez-vous continuer ? y/n


could please me solve \xff problem ?... :~

i have similar functional example may you.  including arduino sketch , client side python



Arduino Forum > Using Arduino > Programming Questions > Arduino-Python : Serial.read() problem...


arduino

Comments

Popular posts from this blog

VIDIOC_S_FMT error 16, Device or resource busy - Raspberry Pi Forums

using a laptop skeleton to build a pi laptop - Raspberry Pi Forums

Forum for Joomla? - Joomla! Forum - community, help and support