[Solved] I need to return a single value from incoming serial data...
Posted: Thu Nov 19, 2020 5:27 am
Hey all !!! Hope you're doing well...
I am new to STM32 and Arduino, and I'm doing a project, which is so difficult for me. I'm trying to get weight data from digital weighing scale through STM32f1, and print current weight on a thermal printer. It's a prototype for my college project.
Setup : Digital weighing scale using Renesas chip connected to one channel of Max232, another channel of Max232 connected to STM32. STM32 is connected to PC via FTDI232 serial to USB converter. A push button is attached to STM32, to facilitate data printing on serial monitor when enabled.
I managed to get the weight data on serial monitor on button press. But the problem is the weight gets printed several times even if the button is pressed for 1 second. If I try some other method, it returns invalid characters infinitely.
For the prototype, I need only current weight data to return. But I have no idea on doing that.
I hope somebody has any suggestions or some help regarding this issue. Thanks in advance.
Forgive me if my English was bad. It isn't my mother tongue.
P.S. I'm attaching the code I wrote. I hope someone will be able to help me. If there is need for circuit/connection diagram, I can provide.
I tried to buffer incoming serial values to print current value. But it ended up in vain...
I used this tutorial for preliminary setup : "https://www.instructables.com/Getting-S ... duino-IDE/"
Arduino IDE: v1.8.13(Windows Store 1.8.42.0) from Microsoft store
Board Manager: STM32F1XX/GD32F1XX by stm32duino v2020.11.14
Additional board manager URL: "http://dan.drown.org/stm32duino/package ... index.json"
I am new to STM32 and Arduino, and I'm doing a project, which is so difficult for me. I'm trying to get weight data from digital weighing scale through STM32f1, and print current weight on a thermal printer. It's a prototype for my college project.
Setup : Digital weighing scale using Renesas chip connected to one channel of Max232, another channel of Max232 connected to STM32. STM32 is connected to PC via FTDI232 serial to USB converter. A push button is attached to STM32, to facilitate data printing on serial monitor when enabled.
I managed to get the weight data on serial monitor on button press. But the problem is the weight gets printed several times even if the button is pressed for 1 second. If I try some other method, it returns invalid characters infinitely.
For the prototype, I need only current weight data to return. But I have no idea on doing that.
I hope somebody has any suggestions or some help regarding this issue. Thanks in advance.
Forgive me if my English was bad. It isn't my mother tongue.

P.S. I'm attaching the code I wrote. I hope someone will be able to help me. If there is need for circuit/connection diagram, I can provide.

I tried to buffer incoming serial values to print current value. But it ended up in vain...

I used this tutorial for preliminary setup : "https://www.instructables.com/Getting-S ... duino-IDE/"
Arduino IDE: v1.8.13(Windows Store 1.8.42.0) from Microsoft store
Board Manager: STM32F1XX/GD32F1XX by stm32duino v2020.11.14
Additional board manager URL: "http://dan.drown.org/stm32duino/package ... index.json"
Code: Select all
int inByte;
const int buttonPin = PB10;
int buttonState;
char recArray[30];
int debouncecnt = 0;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
Serial2.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
if(buttonState == LOW)
{
debouncecnt++;
}
if (debouncecnt > 100000)
{
Serial.println(debouncecnt);
debouncecnt = 0;
int cnt = 0;
clearArray();
Serial2.flush();
while(true)
{
if(Serial2.available()>0)
{
byte tmp = Serial2.read();
if(tmp == '\r' || tmp == '\n')
{
break;
}
else
{
recArray[cnt] = tmp;
cnt++;
if(cnt > 30)
{
break;
}
}
}
}
Serial.println(recArray);
}
}
void clearArray()
{
for(int i = 0; i < 30; i++)
{
recArray[i] = '\0';
}
}