Go Down

Topic: Latency between arduino and python (Read 2271 times) previous topic - next topic

Power_Broker

I tried the example you sent, but nothing is printed on the python's terminal Printing
Many people including myself have gotten the example sketches to work - you're probably using the wrong USB port.
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

sterretje

but at the moment I don't understand the purpose of the 'b'
The one or two times that I dabled in Python, it shows data with a b in front of it; can't remember what it means, can't remember how I got rid of it or if I had to get rid of it, but I suspect that that is what OP is referring to.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Robin2

#32
Jun 21, 2020, 08:32 pm Last Edit: Jun 21, 2020, 08:33 pm by Robin2
The one or two times that I dabled in Python, it shows data with a b in front of it; can't remember what it means, can't remember how I got rid of it or if I had to get rid of it, but I suspect that that is what OP is referring to.
I use Python fairly regularly and I have not come across it. I have been assuming it something added by the OP's program.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.


Robin2

It's Python 3 thing.
I wonder how the OP got himself snarled up with something like that ?

Sometimes people seem to make an effort to make things complicated :)

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

berto906

My next question is how is the Arduino required to interpret that message - just an English language explanation, please.

If this was my project I think I would be sending it like this <b,34> and using the 3rd example in Serial Input Basics to receive it, but at the moment I don't understand the purpose of the 'b'

...R
As other user said, it's python adding the b, i actually only wanted to send the number.
Arduino take that number, convert in an int and the use it to move the linear motor in that position.
The linear motor can extend up to 50 mm, so ,for simplicity, I divided this space in 50 possible position, and every time arduino receive a new position set a variable equal to the new received value.
Then a open loop controller (based on the time that the linear motor takes to do 1 mm) send a signal to two digital pin , making the motor extend or shorten based on the actual position and te position to be reached.
Inside the loop the function that update the estimated position of the linear motor is continuously called.

Robin2

As other user said, it's python adding the b, i actually only wanted to send the number.
I think I see the confusion. This is the Python code in Reply #26
Code: [Select]
serialPort.write(stringWithMarkers.encode('utf-8'))
print ("THIS IS WHAT IS SENT : ", stringWithMarkers.encode('utf-8'))

It should be
Code: [Select]
serialPort.write(stringWithMarkers.encode('utf-8'))
print ("THIS IS WHAT IS SENT : ", stringWithMarkers)

There is no need to encode the string for use within Python

...R

Two or three hours spent thinking and reading documentation solves most programming problems.

Power_Broker

My apologies for earlier - turns out a recent update to pySerialTransfer accidentally had a bug. It's fixed in release 2.0.1. Make sure you update both pySerialTransfer and SerialTransfer.h. Once you've done that, you can use the following code to verify successful communication:

Python:
Code: [Select]
import time
from pySerialTransfer import pySerialTransfer as txfer


if __name__ == '__main__':
    try:
        link = txfer.SerialTransfer('COM17')
       
        link.open()
        time.sleep(2) # allow some time for the Arduino to completely reset
       
        while True:
            send_size = 0
           
            ###################################################################
            # Send a list
            ###################################################################
            list_ = [1, 3]
            list_size = link.tx_obj(list_)
            send_size += list_size
           
            ###################################################################
            # Send a string
            ###################################################################
            str_ = 'hello'
            str_size = link.tx_obj(str_, send_size) - send_size
            send_size += str_size
           
            ###################################################################
            # Send a float
            ###################################################################
            float_ = 5.234
            float_size = link.tx_obj(float_, send_size) - send_size
            send_size += float_size
           
            ###################################################################
            # Transmit all the data to send in a single packet
            ###################################################################
            link.send(send_size)
           
            ###################################################################
            # Wait for a response and report any errors while receiving packets
            ###################################################################
            while not link.available():
                if link.status < 0:
                    if link.status == txfer.CRC_ERROR:
                        print('ERROR: CRC_ERROR')
                    elif link.status == txfer.PAYLOAD_ERROR:
                        print('ERROR: PAYLOAD_ERROR')
                    elif link.status == txfer.STOP_BYTE_ERROR:
                        print('ERROR: STOP_BYTE_ERROR')
                    else:
                        print('ERROR: {}'.format(link.status))
           
            ###################################################################
            # Parse response list
            ###################################################################
            rec_list_  = link.rx_obj(obj_type=type(list_),
                                     obj_byte_size=list_size,
                                     list_format='i')
           
            ###################################################################
            # Parse response string
            ###################################################################
            rec_str_   = link.rx_obj(obj_type=type(str_),
                                     obj_byte_size=str_size,
                                     start_pos=list_size)
           
            ###################################################################
            # Parse response float
            ###################################################################
            rec_float_ = link.rx_obj(obj_type=type(float_),
                                     obj_byte_size=float_size,
                                     start_pos=(list_size + str_size))
           
            ###################################################################
            # Display the received data
            ###################################################################
            print('SENT: {} {} {}'.format(list_, str_, float_))
            print('RCVD: {} {} {}'.format(rec_list_, rec_str_, rec_float_))
            print(' ')
   
    except KeyboardInterrupt:
        try:
            link.close()
        except:
            pass
   
    except:
        import traceback
        traceback.print_exc()
       
        try:
            link.close()
        except:
            pass



Arduino:
Code: [Select]
#include "SerialTransfer.h"


SerialTransfer myTransfer;


void setup()
{
  Serial.begin(115200);
  myTransfer.begin(Serial);
}


void loop()
{
  if(myTransfer.available())
  {
    // send all received data back to Python
    for(uint16_t i=0; i < myTransfer.bytesRead; i++)
      myTransfer.txBuff[i] = myTransfer.rxBuff[i];
   
    myTransfer.sendData(myTransfer.bytesRead);
  }
}
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

berto906

My apologies for earlier - turns out a recent update to pySerialTransfer accidentally had a bug. It's fixed in release 2.0.1. Make sure you update both pySerialTransfer and SerialTransfer.h. Once you've done that, you can use the following code to verify successful communication: [...]
Thank you for notify the update. I've tried the program and now works.
I have two question:
When I send a float, the answer from Arduino contains more decimal then the float sent, is this behavior normal? Sending with :
Code: [Select]

float_ = round(random.uniform (0,50),2)
            float_size = link.tx_obj(float_)


result in having on the terminal

Code: [Select]

SENT: 47.78
RCVD: 47.779998779296875


This happens even with the basic example you sent.

The other question is regarding on how to handle the data that arduino received. If I send the float of before, where is stored? And how should convert it in a float to use inside arduino ?


Go Up