Can't get HardwareSerial Serial1(2400) working (F446RE)
Posted: Mon Nov 25, 2024 5:01 am
I can't seem to get HardwareSerial Serial1(2400) to work on my F446RE for some reason. (Won't work on Serial2 either.)
The goal is to simply listen to the data stream that my laser diameter sensor is putting out on RS-485. (2400 BAUD, 8 data, No parity, 1 Stop) I have tested the _exact_ same RS-485 board setup on a Mega just to be certain everything external to the F446RE is correct and fully functional. I even tried exchanging the Rx and Tx pins to be sure that wasn't the issue. No bueno.
I'm sure it is something simple that I have foolishly overlooked.
Here is my code:
The goal is to simply listen to the data stream that my laser diameter sensor is putting out on RS-485. (2400 BAUD, 8 data, No parity, 1 Stop) I have tested the _exact_ same RS-485 board setup on a Mega just to be certain everything external to the F446RE is correct and fully functional. I even tried exchanging the Rx and Tx pins to be sure that wasn't the issue. No bueno.

I'm sure it is something simple that I have foolishly overlooked.
Here is my code:
Code: Select all
/*
* Version 01 RS485 Laser RS-484 reading test program (with LCD added)
* Bill Dube, Nov 24, 2024
* Nucleo F446RE Ver 01
*
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
HardwareSerial Serial1(USART1); //D10/(PB6) =TX1 PB7 = RX1
#define VER 01
#define SERIAL_XMT 2 // DE control pin on RS 485 board
#define SERIAL_RCV 3 // RE control pin on RS 485 board
#define LCD_SCL PB8 // I2C clock pin
#define LCD_SDA PB9 // I2C data pin
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
byte getdata;
int ByteCount;
long LaserDiaReadingI =0;
float LaserDiaReadingF;
String DigitString, ReadingString;
unsigned long int TimeNow;
float SecondsElapsed;
void setup() {
lcd.init(); // initialize the lcd
lcd.backlight();
//Using Serial1 Port for RS485,
// Pin PB6 (D10) TX Connected to pin DI on RS-485 board
// Pin PB7 RX Connected to pin R0 on RS-485 board
// (This is the opposite to what is typically shown in tutorials, but this indeed is what works.)
Serial1.begin(2400); // Serial1 port = 2400 baud, 8 data, No parity, 1 stop
// Serial.begin(115200); // Start serial monitor port when running this on a Mega
// Pin D2 connected to pin DE on rs485 board
// Pin D3 connected to pin RE on rs485 board
pinMode(SERIAL_XMT, OUTPUT);//DE Controling pin of RS-485
pinMode(SERIAL_RCV, OUTPUT);//RE Controling pin of RS-485
// Set DE and RE to logic LOW
digitalWrite(SERIAL_XMT,LOW);//DE=LOW Receive Enabled M1
digitalWrite(SERIAL_RCV,LOW);//RE=LOW Receive Enabled M1
delay(100); // Give a bit of time for board to get its serail pins sorted...
lcd.setCursor(0,0);
lcd.print("RS485 Ver= ");
lcd.println(VER);
delay(1000); // Pause to leave the starting splash up on the monitor for a second...
TimeNow = millis();
} // end setup()
void loop() {
getdata=0;
if(Serial1.available()){ //if there is a byte is available on the RS485
getdata=Serial1.read();
if (getdata == 0xF1) { // We have the beginning of a reading
ByteCount=1; // Reset the byte counter
// lcd.setCursor(0,0); // Time stamp stuff for using with Mega and serial monitor
// lcd.print("Time=");
// SecondsElapsed = (millis() - TimeNow) / 1000.0; // divide by a float to force the type conversion
// lcd.print(SecondsElapsed,3);
lcd.setCursor(0,0); // Row 1 col 1
lcd.print("Laser Reading = ");
lcd.setCursor(0,1); // Row 1 col 2
lcd.print(" "); // clear it out with blanks
lcd.setCursor(0,1); // Row 1 col 2
lcd.print(LaserDiaReadingF, 3); // Write what we get back from the laser
ReadingString =String(""); // Clear out for a new reading
}
else
{
DigitString =String(""); // Clear out the digit string
if (getdata < 16){
DigitString =String(0,HEX);
}
//Serial.print(getdata,HEX);
DigitString +=String(getdata,HEX);
if (ByteCount <=3 ) { // only save first 3 bytes
ReadingString = DigitString + ReadingString; // Put the latest data in the _front_
ByteCount++; // increment the byte counter
}
if (ByteCount == 4) {
LaserDiaReadingI = ReadingString.toInt();
LaserDiaReadingF = LaserDiaReadingI / 1000.0;
ByteCount++;
}
}
} // if Serial1 avalible
} // end loop()