Hi everyone, first time poster here despite being a long time googler / lurker,
I've written some code to listen to the 19200 baud serial datastream sent from a Victron MPPT solar charge controller and display this information on a LCD display. I implemented this on a Mega and make use of the Serial1 hardware UART to interface to the Victron. I'd like to move this simple sketch to an Uno or even a Nano as the code is small enough to fit in the latter and for physical packaging it would be much nicer in the smaller form factor. The problem I'm having is when I change the serial reads from using 'serial1' to 'serial' the Mega doesn't read and display the data. I'd like to point out I have no debugging or console output to the serial0 or default UART as in this application the UART is to be exclusively used for listening to the Victron solar charge controller. As a workaround I implemented it using softwareserial on an Uno however the BER (Bit Error Rate) at this baud rate is quite high and I'd much rather make use of the hardware UART as it'll clean the data up considerably.
To illustrate I'm changing these lines:
void setup() {
Serial1.begin(19200); // Init MPPT serial port
..
..}
void ReadMPPT(){
if (Serial1.available()) {
char inChar = Serial1.read();
..
..}
To read:
void setup() {
Serial.begin(19200); // Init MPPT serial port
..
..}
void ReadMPPT(){
if (Serial.available()) {
char inChar = Serial.read();
..
..}
Even with no USB attached to the Arduino I still get no data, I've also confirmed my TX and RX pins are oriented correctly. The device continually broadcasts so I only need the TX pin from the controller connected to the RX pin on the Arduino, plus a ground reference of course. For reference's sake I can change the 'serial1' statements to read 'serial2' or 'serial3' and move to the appropriate UART pin on the Mega and everything works as it should, I just can't get it working on Serial0 ('Serial'). The idea of debugging this on the Mega being once I have it working on Serial0 I can then move the code to an Uno or Nano.
Is there something 'special' about Serial0 which is preventing this from working? Any input / suggestions appreciated!
In MEGA Board, RX0/TX0 (Serial0) is engaged with Serial Monitor and IDE. So, users do not prefer to connect it with any other UART peripheral being afraid of data corruption, and this is exactly happening in your case when you move from Serial1 to Serial0.
You are planning to shift to NANO; it is fine. If you are not using the UART Port (RX0/TX1) of NANO for Serial Monitor, then the following arrangements are available for you.
1. In your MEGA codes, just replace the 'Serial1' word by 'Serial'.
2. Upload the sketch to NANO.
3. Connect your UART peripheral with RX0/TX1 pins of NANO via MAX232 converter. (If you are receiving TTL UARTFrame from your peripheral, then you don't need MAX232 TTL <----> RS232 converter.)
I can't see any reason for the code not to work, IF the part you didn't share was written correctly.
A Leonardo is the same size as the UNO, but pins 0 and 1 are a separate Serial1 instance from the Serial instance, which is not connected to any usable pins.
A Teensy has multiple hardware serial ports, and is small enough to easily get lost. Ask me how I know that.
If you need a smaller board than the Mega that still has the hardware serial interface available although you need the debugging via the USB connection, the Leonardo or Micro are alternatives. Both use Serial as the USB connection to the PC and Serial1 as the hardware serial interface available on pin 0 and 1.
GolamMostafa:
..
You are planning to shift to NANO; it is fine. If you are not using the UART Port (RX0/TX1) of NANO for Serial Monitor, then the following arrangements are available for you.
1. In your MEGA codes, just replace the 'Serial1' word by 'Serial'.
2. Upload the sketch to NANO.
3. Connect your UART peripheral with RX0/TX1 pins of NANO via MAX232 converter. (If you are receiving TTL UARTFrame from your peripheral, then you don't need MAX232 TTL <----> RS232 converter.)
This is exactly what I have done as explained and as per the relevant code snippets above. I've tried it on the Mega, an Uno and also a Nano without success. Everything here is at the 5v TTL level, there's no RS232.
PaulS:
I can't see any reason for the code not to work, IF the part you didn't share was written correctly.
A Teensy has multiple hardware serial ports, and is small enough to easily get lost. Ask me how I know that.
The only thing that might be relevant because I haven't looked through the library code in detail is I'm using the <LiquidCrystal.h> library. Other than that, the remainder of the code is basic data filtering/sorting and simply writing the result to the LCD using lcd.setcursor and lcd.print() statements. Re the teensy going missing...
pylon:
If you need a smaller board than the Mega that still has the hardware serial interface available although you need the debugging via the USB connection, the Leonardo or Micro are alternatives. Both use Serial as the USB connection to the PC and Serial1 as the hardware serial interface available on pin 0 and 1.
Oh right, I'll keep that in mind, but as I don't require the serial0 UART for any debugging, can anybody explain why it is I can't read data through it in this particular appication where I'm feeding data directly in via pins 0 & 1?
GolamMostafa: 1. Your 'Victron MPPT Solar Charge Controller' transmits data in UART Frames. Is it at TTL level or RS232 Level?
As per above post: "Everything here is at the 5v TTL level, there's no RS232."
GolamMostafa: 2. Is the UART Frame of Step-1 carrying ASCII code or binary code?
It's all plain ASCII text, there's a block of text sent at about 1.5hz.
GolamMostafa: 3. How many frames do make a message? 4. Is there a beginning marker, data item separator, and end marker in your message? 5. Can you present a structure (format) of a message?
In this instance this isn't relevant as it's an ascii datastream and my code deciphers it and filters out the values I need just fine when the Arduino receives the serial datastream on serial1, serial2 or serial3.
GolamMostafa: 6. Have you ever managed to get a working system? If yes, which setup of the following? (1) MEGA (Serial1) + Serial Monitor + Victron MPPT (2) MEGA (Serial1) + Serial Monitor + MAX232 + Victron MPPT
From the OP: "The problem I'm having is when I change the serial reads from using 'serial1' to 'serial' the Mega doesn't read and display the data."
..
"For reference's sake I can change the 'serial1' statements to read 'serial2' or 'serial3' and move to the appropriate UART pin on the Mega and everything works as it should, I just can't get it working on Serial0 ('Serial')"
-- So my code works and happily reads the datastream from the Victron when I read the data via the Serial1, Serial2 or Serial3 UARTs on a Mega. When I change my code to read from 'Serial' (Serial0 if you want to think of it as that) the Arduino doesn't receive the data and consequently doesn't decode the data despite that being the only change to the code made.
It works as per (1) MEGA (Serial1) + Serial Monitor + Victron MPPT. As mentioned previously the output is 5v TTL and works as it should on any of serial1, serial2 or serial3. I just cannot get any data in via Serial (serial0).
I'm close to being out of options but I know this should work! (yes it's p#ssing me off). I think my next step is going to be to scope the output from the victron when attached to serial1 and compare it against the serial0 input pin in case there's something stupid like a voltage offset there preventing the micro from seeing the change in state for each bit transmitted. It's really the only thing that would explain this behaviour due to the fact that serial1-3 picks it up fine.
I'm close to being out of options but I know this should work! (yes it's p#ssing me off). I think my next step is going to be to scope the output from the victron when attached to serial1 and compare it against the serial0 input pin in case there's something stupid like a voltage offset there preventing the micro from seeing the change in state for each bit transmitted. It's really the only thing that would explain this behaviour due to the fact that serial1-3 picks it up fine.
The only difference between Serial1-3 and Serial is that on Serial the ATmega16U2 is attached by a 1k resistor. It will keep TX HIGH (UART default state), so if your Victron device is not able to sink about 5mA this might be the problem.