0

I transported two float values with pyserial to COM3 and Arduino accepted the values.

But it displayed the first value correctly on LCD, the second value can't be read and showed "nan" on LCD.

Python code:

import serial
import struct
ser =serial.Serial("COM3", 115200, timeout=2)

##======Get  Ready================
print("Connecting to Arduino.....")
for i in range (1,10):
    rv=ser.readline()
    print("Loading...")
    #Debug print (rv) # Read the newest output from the Arduino
    print (rv.decode("utf-8")) 
    ser.flushInput()
    sleep(1) # Delay for one tenth of a secon
    Str=rv.decode("utf-8")
    #Debug print(Str[0:5])
    if Str[0:5]=="Ready":  
          print("Get Arduino Ready !")
          break
##------------------------------------------------------
d = 77.25
a = 111.32
ser.write(struct.pack('>2f',d,a))

Arduino code:

#include <LiquidCrystal.h>
float x;
byte *xdata = (byte *) & x;
float y;
byte *ydata = (byte *) & y;
int num = 0;
char coor[2];

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  Serial.begin(115200); // set the baud rate
  Serial.println("Ready"); // print "Ready" once
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
}

void loop(){
  while(Serial.available() > 0){
    for(int i = 0; i < 2; i++){
      coor[i] = Serial.read();
      }
    if(coor[0] == 'B' ){
      xdata[3] = coor[0];
      ydata[3] = coor[1];
      num = 1;
    } else if(num == 1){
      xdata[2] = coor[0];
      ydata[2] = coor[1];
      num++;
    } else if(num == 2){
      xdata[1] = coor[0];
      ydata[1] = coor[1];
      num++;
    } else if(num == 3){
      xdata[0] = coor[0];
      ydata[0] = coor[1];
      num = 0;
    // use the values
    Serial.write(xdata,4);
    Serial.write(ydata,4);
    lcd.print(x);
    lcd.setCursor(8,0);
    lcd.print(y);
      }
    }

The LCD monitor will show 77.25 and nan. It can't show 111.32 correctly.

  • 2
    you read two bytes if at least one is available. read the two bytes with Serial.readBytes(coor, 2). it will wait for the second byte – Juraj Jul 21 '18 at 10:55
  • Or wait for there to be 2 bytes available before reading them... – Majenko Jul 21 '18 at 12:30
  • sorry,i still can't unserstand the following code," Serial.readBytes(coor, 2) ",where to write. – Vaan Jul 22 '18 at 2:51
  • I replaced "coor[i] = Serial.read();" with "Serial.readBytes(coor, 2)",but it still can't work. – Vaan Jul 22 '18 at 3:23
  • and you removed the for loop? arduino.cc/reference/en/language/functions/communication/serial/… – Juraj Jul 22 '18 at 9:10
0

I don't understand why you read the bytes this way. Your original version was reading only the bytes of the first float into xdata and -1 into ydata. (read returns -1 if no data is available, it doesn't wait.) Now when two bytes are read with readBytes, first two bytes of the first float are written one into xdata[0] and second into ydata[0].

read first float with readBytes(xdata, 4) and the the second with readBytes(ydata, 4)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.