python - Arduino to Raspberry Pi serial communication creates only random chars after a few seconds -


for project need raspberry pi communicate several peripheral components, 2 of them arduinos. 1 causes problem pro mini 3.3 v atmega328. arduino receives input 2 sensors , transfer data raspberry pi via serial. python code serial-package used on raspberry establishes connection every 50 ms.

when input raspberry printed, first few lines correct after two, 3 seconds printed lines random chars.

my python code looks this:

import serial ser = serial.serial('/dev/ttys0', 115200, timeout=1) if ser.isopen():     ser.close()  ser.open()  ...  # loop try:         ser.write("1")                     ser_line = ser.readline()         print ser_line ... 

the arduino code:

#include <wire.h> #include "sparkfunhtu21d.h" #include <fdc1004.h>  fdc1004 fdc(fdc1004_400hz); htu21d myhumidity;  int capdac = 0;  void setup() {     wire.begin();     serial.begin(115200);     myhumidity.begin(); }   void loop() {     string datastring = "";      datastring += string(myhumidity.readhumidity());     datastring += "  ";     datastring += string(myhumidity.readtemperature());     datastring += "  ";      (uint8_t = 0; < 4; i++){                 uint8_t measurement = 0;         uint8_t channel = i;          fdc.configuremeasurementsingle(measurement, channel, capdac);         fdc.triggersinglemeasurement(measurement, fdc1004_400hz);         //wait completion         delay(15);          uint16_t value[2];         if (! fdc.readmeasurement(measurement, value)) {             int16_t msb = (int16_t) value[0];             int32_t capacitance = ((int32_t) 457) * ((int32_t) msb);               capacitance /= 1000; //in femtofarads             capacitance += ((int32_t) 3028) * ((int32_t) capdac);              datastring += string(capacitance);             datastring += "  ";              int16_t upper_bound = 0x4000;             int16_t lower_bound = -1 * upper_bound;              if (msb > upper_bound) {                 if (capdac < fdc1004_capdac_max)                      capdac++;              } else if (msb < lower_bound && capdac > 0) {                 capdac--;                         }         }     }      serial.println(datastring);         delay(20);        // delay in between reads stability } 

the output of loop looks like:

print output

so output loses accuracy , become random chars after 6 lines , output doesn't recover. when print serial output in arduino's serial monitor output stays correct time. after several tests run out of ideas. has solution problem or experienced similar behavior?


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -