Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] need help using multiple serial ports on Arduino MEGA

Status
Not open for further replies.

johnny78

Full Member level 4
Joined
Jun 28, 2017
Messages
216
Helped
1
Reputation
2
Reaction score
4
Trophy points
18
Activity points
1,784
hi Guys
this code should read serial data coming to serial 1 on arduino mega & display it on serial monitor using serial 0
would you check it please ?

thanks
Johnny

Code:
String inputString = "";         // a String to hold incoming data
bool stringComplete = false;  // whether the string is complete


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial1.begin(1200, SERIAL_7N1);
}

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    Serial.println(inputString);
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}

void serialEvent() {
  while (Serial1.available()) {
    // get the new byte:
    char inChar = (char)Serial1.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}
 

Hi,

Please give an error description.

Klaus
 

Hi,

Please give an error description.

Klaus
no error
but i dont see anything on the output on serial monitor or the tx led on arduino MEGA
& the output of the scale of 1200 7N1 is writing data im sure & tested on another device
i think the code is easy & it should work
right ?

is it possible this MEGA board serial ports are damaged or else?
im not sure of this chinese clone which looks bad also
i will try to get another one tomorrow
or give me an idea to test the serial ports on the board

thanks
Johnny
 
Last edited:

The function "serialEvent()" isn't being instantiated anywhere on your code.
 

The function "serialEvent()" isn't being instantiated anywhere on your code.
this info from Serial Event example in Arduini IDE

/*
SerialEvent occurs whenever a new data comes in the hardware serial RX. This
routine is run between each time loop() runs, so using delay inside loop can
delay response. Multiple bytes of data may be available.
*/

Johnny
 
Last edited:

Your code didn't work on my mega either.
There are several ways to do this. One possibility would be:

Code:
 // a String to hold incoming data
char inputString[20];   // allocate enough memory for expected incoming string length
bool stringComplete = false;   // whether the string is complete
int num = 0;   // point to next char in string array

void setup()
{ 
  Serial.begin(9600);
  Serial1.begin(1200, SERIAL_7N1);
}

void loop()
{
  // print the string when a newline arrives:
  if (stringComplete)
    {
    Serial.println(inputString); 
    stringComplete = false;
    }
  if(Serial1.available() )
    {   
    // get the new byte:
    char inChar = (char)Serial1.read();
    // place it into the inputString:
    inputString[num++] = inChar;
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n')
      {
      stringComplete = true;
      num = 0;
      }
   }
}
 

    johnny78

    Points: 2
    Helpful Answer Positive Rating
Your code didn't work on my mega either.
There are several ways to do this. One possibility would be:
Your code is working

the Serial Event example works only with Serial i guess because the example is working
so i guess the serial event isnt working with Serial1

if you guys check this will be great

/*
SerialEvent occurs whenever a new data comes in the hardware serial RX. This
routine is run between each time loop() runs, so using delay inside loop can
delay response. Multiple bytes of data may be available.
*/

thanks
Johnny
 

There is a line that I left out on the code I posted above.

Code:
inputString[num] = 0;   // terminate the string with a 0

It should be inserted in the section
Code:
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n')
      {
      stringComplete = true;
      inputString[num] = 0; // terminate the string with a 0
      num = 0;
      }

void serialEvent() may have needed to be void serialEvent1().
 

    johnny78

    Points: 2
    Helpful Answer Positive Rating
void serialEvent() may have needed to be void serialEvent1().
i didnt notice this thanks Alot its working now with serial event the same old code i posted here

one Final issue

i read the scale on arduino Mega Serial1 & display the result on Serial monitor
this is a reading from the scale
S S 189.71 g

i have used
Code:
lcdData.replace("N", "");
  lcdData.replace("S", "");
  lcdData.replace(" ", "");

Im using ATMEGA32 with mighty core on Arduino IDE & displaying on 5 seven segment displays

i get 0.00 on the display

when testing a Float type Var it displays the 5 digits exactly same as the number

Code:
float test = 976.58  ;

sevseg.setNumberF(test, 2);

i think its something about conversion of types
need your help guys

thanks
Johnny
--- Updated ---

thank
i didnt notice this thanks Alot its working now with serial event the same old code i posted here

one Final issue

i read the scale on arduino Mega Serial1 & display the result on Serial monitor
this is a reading from the scale
S S 189.71 g

i have used
Code:
lcdData.replace("N", "");
  lcdData.replace("S", "");
  lcdData.replace(" ", "");

Im using ATMEGA32 with mighty core on Arduino IDE & displaying on 5 seven segment displays

i get 0.00 on the display

when testing a Float type Var it displays the 5 digits exactly same as the number

Code:
float test = 976.58  ;

sevseg.setNumberF(test, 2);

i think its something about conversion of types
need your help guys

thanks
Johnny
its ok i just converted it to float Type

its done
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top