Serial Communication between Arduino and Visual Studio (+QT)

Julian Grimm 1 Reputation point
2022-03-24T13:19:57.967+00:00

Hello everyone,
I'm experiencing a problem with my Communication between my Arduino and Visual Studio. I programmed my Arduino to send a specific sign with a value to show where to display said value.( I'm using a lab setup which is supposed to show different parameter values or read out the current temperature and display it to my GUI in Visual Studio) e.g. when I want to show the current temperature I send a "<" at the beginning and theres a SlotResponse function which detects the "<" and then knows to write the value into the "temp_ui" spinBox. This all works fine, but as soon as I use a Timer in Arduino the serial communciation gets buggy. At the beginning is sends the values correctly, but at a certain point the data gets split up while tranfering and my slotResponse function cant decipher the message and gets messed up and shows an error. I'm wondering if its possible to enter a delay somewhere or to tell the slotResponse message to wait for the whole message to be transferred. Maybe the Baudrate is set wrong, but I've tried many different ones and none seem to fit right. I used a switch case to call the function I need. When I call the function the Timer gets set and so the Update_Temperature() and Display_Variables_Plant()(which sends the Index, a ";" and the temperature value) functions get called every second (ISR raises interrupt_counter3 every second). It works for a certain amount of time but then it gets desynchronized, then it gets snychronized again until an error occurs which stops the program (It says that the index is out of range). Any help would be very much appreciated. I'll add some screenshots to give you guys a better idea of the program. (Visual Studio code snippet, Arduino Code Snippet and Errorwindow)

Arduino Code Snippet:

const char MEASURE[] PROGMEM = "&"; //is the tenth in the array)
const uint16_t t1_load = 0;
const uint16_t t1_comp = 62500;
String Semi;
String Total;
int ArrayIndex = 0;
double temperature_c = 0;  

void loop() {
  while (interrupt_counter3 == 1){
  ClearInputBuffer();
  Update_Temperature();                                                 
  Display_Variables_Plant();
  interrupt_counter3 = 0;
  }
  delay(10);


case 'U':
      Update_Temperature();                                                       // Get current temperature
      roomtemp = temperature_c;                                                   // Save it as RoomTemp

// Setting variables for data aquisition
      interrupt_counter3 = 0;                                                     // Counter variable
      ArrayIndex = 0;
      analogWrite(peltierPin, c * 2.55);
      TCCR1A = 0;
      TCCR1B &= ~(1 << WGM13);
      TCCR1B |=  (1 << WGM12);                                                     
      TCCR1B |=  (1 << CS12); 
      TCCR1B &= ~(1 << CS11);
      TCCR1B &= ~(1 << CS10);                                       
      TCNT1 = t1_load;                                                  
      OCR1A = t1_comp;  
      TIMSK1 = (1 << OCIE1A);                                                          
      sei();                                                    // Set global interrupt enabled, ISR is called every 1 ms 

      break;

ISR(TIMER1_COMPA_vect) {
   interrupt_counter3++;
}
void Update_Temperature()                            // Saves measured temperature to temperature_c
{
  temperature_c -= 2;
  return;
}

void Display_Variables_Plant()
{
  ClearInputBuffer();
  Semi = ";";
  Semi.concat(temperature_c);
  Total = ArrayIndex + Semi;
  ArrayIndex++;
  PrintCurve();Serial.println(Total);
  return;
}
}
void ClearInputBuffer() {
    Serial.read();
}
void PrintCurve()     // Equivalten to Serial.print("&");
{
  strcpy_P(buffer, (char *)pgm_read_word(&(string_table_serial[10]))); Serial.print(buffer);
}

Visual Studio Code Snippet:

void arduinoVirtualPIDPlugin::slotResponse(QString msg)
{   
    SerialMessage.clear();
    SerialMessage.append(msg);
    QString HelpingStr = SerialMessage; 
    QString HelpingStr2 = SerialMessage;

    int i = 0;
    while (SerialMessage.size() > 0) // Cecking Serial Response for keychars "<", "#" and "~"
    {
                if (SerialMessage[i] == '&') //Save Data to Vector for plotting the graph
        {

            HelpingStr.remove(0, 1);
            bool isNum;
            tempString->append(HelpingStr.split(';')[1]);   //Save TemperatureValue in array
            TempString->append(HelpingStr.split(';')[1]);
            HelpingStr2 = HelpingStr.split(';')[1];
            HelpingStr2.toDouble(&isNum);
            if (isNum) {
                ui.textEdit_console->append(HelpingStr);        //Print Timeindex and Temperature to console
                 //For later conversion needed --> updateQwtPlot 
                updateQwtPlot(PLOT_NAME);
                k++;
                HelpingStr.remove(0, HelpingStr.size());
                HelpingStr2.remove(0, HelpingStr2.size());
            }
            else {
                ui.textEdit_console->append("Peltier PWM Module: the data value is not a number: " + HelpingStr2 + "\n");
                HelpingStr.remove(0, HelpingStr.size());
                HelpingStr2.remove(0, HelpingStr2.size());
            }
            SerialMessage.remove(0, SerialMessage.size());
            i = 0;
        }
                else
        {
        i++;
        if (i > SerialMessage.size() - 1)
        {
            ui.textEdit_console->append(HelpingStr);
            ui.textEdit_console->append("this command cannot be read");
            HelpingStr.remove(0, HelpingStr.size());
            SerialMessage.remove(0, SerialMessage.size());
            i = 0;
        }
        }
Error Code:
Program:
C:\Users\Julian\Desktop\QT\6.15.2\msvc2019_64\bin\Qt5Cord.dll
Module 5.15.2
File:
C:\Users\Julian\Desktop\QT\6.15.2\msvc2019_64\include\QtCore\qlist.h
Line: 579

ASSERT failure in QList::operator: "index out of range", file
C:\Users\Julian\Desktop\QT\6.15.2\msvc2019_64\include\QtCore\qlist.h
Line: 579
Developer technologies C++
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.