-1

I am using a Bluetooth module HC-05 to Stream data from Arduino to My computer. In they computer I am using a python script to receive

They specifications: Baud rate: 9600

But I am getting an error. The right data got in the computer should be:

b'123.098x726.8393x123x\r\n'

Which I almost always get, but sometimes I get the following:

b'123.098x726.8393x123x123.098x726.8393x123\r\n'. wrong

So I don't know happens, in python I use the method reset_input_buffer() and reset_output_buffer() and in Arduino ide I use Serial.flush()

The error keep happens, would it be the baudrate? I increase it would it be the solutionn?

Thanks in advance

String str;
String strs[8];

int pwm=9;

int tech; String sync_fromApp = "?"; String sync_ok = "okx";

int cyc_num; int PWM_0; int PWM_i; int PWM_f;

float volt; float cur; int t;

void setup() { // put your setup code here, to run once: TCCR1B = TCCR1B & B11111000 | B00000001; Serial.begin(9600); Serial.setTimeout(5); pinMode(A0, INPUT); pinMode(pwm, OUTPUT); analogWrite(pwm, 130); delay(1000); }

void loop() { // put your main code here, to run repeatedly: int strCount = 0; if (Serial.available() > 0) {

str = Serial.readString();
str.trim();

if (str == sync_fromApp) {
  Serial.println(sync_ok);
  //Serial.flush();
}

while (str.length() > 0) {
  int index = str.indexOf('x');
  if (index == -1) {
    strs[strCount++] = str;
    break;
  }
  else {
    strs[strCount++] = str.substring(0, index);
    str = str.substring(index + 1);
  }
}

tech = strs[0].toInt();
float E0 = strs[1].toInt() / 1000.0;
float Ei = strs[2].toInt() / 1000.0;
float Ef = strs[3].toInt() / 1000.0;
cyc_num = strs[4].toInt();
int scan_rate = strs[5].toInt();
int t_chro = strs[6].toInt();
int t_step_chro = strs[7].toInt();
float e00 = ((995.0 / 1986.0) * 5.06 + (995.0 / 1004.0) * E0);
PWM_0 = e00 * (255 / 4.99); //Starting Voltage
float eii = ((995.0 / 1986.0) * 5.06 + (995.0 / 1004.0) * Ei);
PWM_i = eii * (255 / 4.99)-1; //Min Voltage
float eff = ((995.0 / 1986.0) * 5.06 + (995.0 / 1004.0) * Ef);
PWM_f = eff * (255 / 4.99)+1; //Max Voltage
long t = (4990000L) / (256L * scan_rate); //delay scan rate
//default:
analogWrite(pwm, PWM_0);
switch (tech) {
  //---Cyclic voltammetry---
  case 1:
    delay(3000);
    for (int n = 1; n <= cyc_num; n++) {
      for (int val = PWM_0; val < PWM_f; val++) {
        analogWrite(pwm, val);

        volt = 0.01974563 * val - 2.558; //0.01974563
        cur = 0.51469 * (analogRead(A0)) - 255.226; //254.226

        Serial.print(volt, 3);
        Serial.print('x');
        Serial.print(cur, 3);
        Serial.print('x');
        Serial.print(val);
        Serial.println('x');
        Serial.flush();
        delay(t);
      }

      for (int val = PWM_f; val > PWM_i; val--) {
        analogWrite(pwm, val);

        volt = 0.01974563 * val - 2.558;
        cur = 0.50669 * (analogRead(A0)) - 255.226;
        //cur_ave = analogRead(A0);
        Serial.print(volt, 3);
        Serial.print('x');
        Serial.print(cur, 3);
        Serial.print('x');
        Serial.print(val);
        Serial.println('x');
        Serial.flush();
        delay(t);
      }
      PWM_0 = PWM_i;
      //Serial.println(n);
    }
    Serial.println("Nx");
    Serial.flush();
    analogWrite(pwm, 130);
    tech = 0;
    break;
  default:
    break;
}

} else { digitalWrite(13, HIGH); delay(200); digitalWrite(13, LOW); delay(200); } }

Above left the arduino sketch.

Below is the python code

            self.ser.write(self.parametersToArduino.encode()) #command to start stream
            pass
    self.ser.reset_input_buffer()   #flush input buffer
    self.ser.reset_output_buffer()   #flush output buffer    
    COM.stop_button["state"] = "active"

    while self.threading:
        try:
            data.RowMsg = self.ser.readline()  # read data from arduino
            data.DecodeMsg()                   #decode data from arduino
            if len(data.RowMsg)>0:
                if b'N' in data.RowMsg:
                    #print("It's over")

                    self.threading = False
                    break
                data.upXData()  #update a list to plot
                data.upYData()  #update a list to plot
                self.ser.reset_input_buffer()
                self.ser.reset_output_buffer()
                pass
            pass
        except Exception as e:
            print(e)
            pass

DecodeMsg is the code below

    def DecodeMsg(self):
        temporal = self.RowMsg.decode('utf8')
        if len(temporal)>0:
            if "x" in temporal:
                self.msg = temporal.split("x")
                del self.msg[-1]
                pass
            print(self.msg) #debugg
            pass
        pass

1 Answers1

0

I'm not sure that this an answer, but it is easy to implement and may help you figure out what is going on.

I don't think it is a baud rate problem because the characters appear to be intact, but some are missing. I think it is a serial buffer overflow resulting in a mix of old and new characters.

To test this out, I would insert a counter into the beginning of the "packet" being sent. My suspicion is that when you see the error, you will be able to see that one packet is being messed up when it overflows. So, you might get something like this (where the first 3 digits are the packet number):

b'001123.098x726.8393x123x\r\n' b'001123.098x726.8393x123x123.098x726.8393x123\r\n b'003123.098x726.8393x123x\r\n'

It may not be definitive, but might help to more clearly see what is happening.

Before doing this, I would probably comment out: TCCR1B = TCCR1B & B11111000 | B00000001; which is increasing the PWM frequency on 9. That, just to see if this changes the behavior since it is so easy to test out.

Hope it helps, but I admit that it is just a suggestion.

BTW: interesting problem and I hope you will tell us what happens.

DrG
  • 409
  • 3
  • 7