Hi,
My goal is to get serial data from 2 scales using an RS232 cable.
I am using Arduino IoT 33.
I used Serial1(connecting to 0,1 pins) and it worked fine:
float weight;
void setup() {
Serial.begin(9600);
delay(2000);
Serial1.begin(9600);
}
void loop() {
#define PRINT_INTERVAL 1000
static unsigned long lastPrintTime = 0;
if ( millis() - lastPrintTime > PRINT_INTERVAL )
{
Serial.print("Weight: "); Serial.println(weight,4);
lastPrintTime = millis();
}
getWeight();
}
void getWeight() {
static String readString;
if (Serial1.available())
{
char c = Serial1.read();
readString += c;
if ( c == 'g' ) {
weight = readString.substring(9,16).toFloat();
readString = "";
}
}
}
When trying to use SoftwareSerial things didn't work at all. I connected RX,TX to pins 5,6 and used this code:
#include <Arduino.h>
#include "wiring_private.h"
Uart mySerial (&sercom0, 5, 6, SERCOM_RX_PAD_1, UART_TX_PAD_0);
void SERCOM0_Handler()
{
mySerial.IrqHandler();
}
float secondWeight;
void setup() {
Serial.begin(9600);
delay(2000);
pinPeripheral(5, PIO_SERCOM_ALT);
pinPeripheral(6, PIO_SERCOM_ALT);
mySerial.begin(9600);
}
void loop() {
#define PRINT_INTERVAL 1000
static unsigned long lastPrintTime = 0;
if ( millis() - lastPrintTime > PRINT_INTERVAL )
{
Serial.print("Second Weight: "); Serial.println(secondWeight,4);
lastPrintTime = millis();
}
getSecondWeight();
}
void getSecondWeight() {
static String readString;
if (mySerial.available())
{
char c = mySerial.read();
readString += c;
if ( c == 'g' ) {
secondWeight = readString.substring(9,16).toFloat();
readString = "";
}
}
}
Nothing happens, what is my mistake? Eventually I want to use both Serial1 and mySerial together to get the input of both scales.
Thanks