Error when Serial Communicating

I am using Serial Comms to communicate between two Arduinos. I read somewhere that its is better to transmit and receive using BYTE datatype and that is working fine. However, after some time, the receiver arduino crashes and the serial monitor freezes and the following error shows up.

Sketch uses 4,246 bytes (13%) of program storage space. Maximum is 30,720 bytes.
Global variables use 258 bytes (12%) of dynamic memory, leaving 1,790 bytes for local variables. Maximum is 2,048 bytes.
Exception in thread "EventThread COM5" Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
	at java.lang.StringCoding$StringDecoder.decode(Unknown Source)
	at java.lang.StringCoding.decode(Unknown Source)
	at java.lang.StringCoding.decode(Unknown Source)
	at java.lang.String.<init>(Unknown Source)
	at java.lang.String.<init>(Unknown Source)
	at processing.app.Serial.serialEvent(Unknown Source)
	at jssc.SerialPort$EventThread.run(SerialPort.java:1112)
java.lang.OutOfMemoryError: Java heap space
	at javax.swing.text.GapContent.getPositionsInRange(Unknown Source)
	at javax.swing.text.GapContent$RemoveUndo.<init>(Unknown Source)
	at javax.swing.text.GapContent.remove(Unknown Source)
	at javax.swing.text.AbstractDocument.handleRemove(Unknown Source)
	at javax.swing.text.AbstractDocument.remove(Unknown Source)
	at javax.swing.text.AbstractDocument.replace(Unknown Source)
	at javax.swing.text.JTextComponent.setText(Unknown Source)
	at processing.app.SerialMonitor.close(SerialMonitor.java:107)
	at processing.app.AbstractMonitor$1.windowClosing(AbstractMonitor.java:57)
	at java.awt.AWTEventMulticaster.windowClosing(Unknown Source)
	at java.awt.Window.processWindowEvent(Unknown Source)
	at javax.swing.JFrame.processWindowEvent(Unknown Source)
	at java.awt.Window.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$200(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
	at javax.swing.text.GapContent.getPositionsInRange(Unknown Source)
	at javax.swing.text.GapContent$RemoveUndo.<init>(Unknown Source)
	at javax.swing.text.GapContent.remove(Unknown Source)
	at javax.swing.text.AbstractDocument.handleRemove(Unknown Source)
	at javax.swing.text.AbstractDocument.remove(Unknown Source)
	at javax.swing.text.AbstractDocument.replace(Unknown Source)
	at javax.swing.text.JTextComponent.setText(Unknown Source)
	at processing.app.SerialMonitor.close(SerialMonitor.java:107)
	at processing.app.AbstractMonitor$1.windowClosing(AbstractMonitor.java:57)
	at java.awt.AWTEventMulticaster.windowClosing(Unknown Source)
	at java.awt.Window.processWindowEvent(Unknown Source)
	at javax.swing.JFrame.processWindowEvent(Unknown Source)
	at java.awt.Window.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$200(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "cc.arduino.packages.discoverers.NetworkDiscovery timer" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "JmDNS(128-61-40-205.local.).Timer" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" Exception in thread "JmDNS(fe80:0:0:0:415a:a9fc:ac54:17cc%13.local.).Timer" java.lang.OutOfMemoryError: Java heap space

The code I am using is given below -

void setup() {
  Serial.begin(115200);
  
}

void loop() {
  if (Serial.available() > 0)
  {
    infrom=Serial.read();
    inputs=(int)infrom;
    if(inputs==1 || inputs==2 ||inputs==3)
    {
      whichch=inputs;
    }
    else
    {
      if(whichch==1)
      {dc1in=inputs;}
      else if(whichch==2)
      {dc2in=inputs;}
      else if(whichch==3)
      {dc3in=inputs;}
      else
      {}
      whichch=0;
    }
    
    Serial.println(inputs);
    Serial.println('\n');
    inputs=0;
  }}

Does anyone know how to fix it?

    infrom=Serial.read();
    inputs=(int)infrom;

These variables are not defined.

      else
      {}

There's some useful code. NOT!

Is the same useless code on both Arduinos? Are you using Serial to talk to the other Arduino OR to talk to the PC?

All the variables are defined. I thought that you would assume that as its not a vital piece of information.

Also that else is being used later when I write the rest of the code. I ran into a problem with the serial comms and thus haven't filled it up yet.

The error you posted was a java error. I think I've seen that before. Look through and make sure you didn't leave the end quote off a string somewhere.

I thought that you would assume that as its not a vital piece of information.

Well, something is causing one end of the serial communications channel to crash. Nothing is unimportant when it comes to troubleshooting.

    Serial.println(inputs);
    Serial.println('\n');
    inputs=0;
  }}

The above is overloading the serial monitor.

Mark

I am using it to communicate between two arduinos. The Sending code is below -

float ch1=7.392,ch2=6.678,ch3=8.255;//,ch4=7.785,ch5=2.213;
byte tr1,tr2,tr3;//,tr4,tr5;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  tr1 = (byte)(5 + (250*(ch1-6.1)/(8.7-6.1)));
  tr2 = (byte)(5 + (250*(ch2-5.9)/(8.73-5.9)));
  tr3 = (byte)(5 + (250*(ch3-4.59)/(10.35-4.59)));
  byte ch = 1;
  Serial.write(ch);
  Serial.write(tr1);
  //Serial.println(tr1);
  delay(10);
  
  
  ch = 2;
  Serial.write(ch);
  Serial.write(tr2);
  //Serial.println(tr2);
  delay(10);
  
  
  ch = 3;
  Serial.write(ch);
  Serial.write(tr3);
  //Serial.println(tr3);
  //Serial.println();
  delay(10);
}

holmes4:     Serial.println(inputs);     Serial.println('\n');     inputs=0;   }}

The above is overloading the serial monitor.

Mark

Could you tell me what I can do to prevent this. I wanted to see what the communication is happening. Should I decrease the baud rate?

Should I decrease the baud rate?

Without decreasing the amount of data being sent? How would that help? The comment was made to get you to at least consider reducing the amount of data being sent. Perhaps by only sending the data that has changed. Or by sending the data once per second or once per minute or once per day - whatever interval is appropriate. Continuously almost never is.

My application need data to be sent continuously. I am sending a stream of 6 numbers once every 20 ms. So you are saying that this method is not proper for such an application? Should I consider using some other technique.

Also the crashing of the serial monitor does not mean that the arduino has stopped transmitting data, or does it?

I thought that if you decrease the baud rate, the data will be sent at a slower rate and thus the cache will be given time to refresh. Is that not the case?

Sending a data spew may be flooding and overflowing the receiving buffers.

My application need data to be sent continuously. I am sending a stream of 6 numbers once every 20 ms.

Once every 20 milliseconds and continuously are NOT the same thing.

Just for shits and giggles, try changing the interval to once every 20 seconds. If that works, then reduce the interval until it no longer works (reliably). Then, you'll know what the maximum rate is.

Sending 1, 2, and 3 as three of the 6 values that are always sent in the same order seems pointless.

If you're doing any serious work with serial comms to the computer, you should install a terminal emulator program on the computer and ditch the serial monitor. I use Tera Term.

PaulS:
Just for shits and giggles, try changing the interval to once every 20 seconds. If that works, then reduce the interval until it no longer works (reliably). Then, you’ll know what the maximum rate is.

I did that and if you decrease the time interval to less than 15ms, there is data loss in the channel and everything is not coming in order. BTW I decreased the baud rate to 19200.

PaulS:
Sending 1, 2, and 3 as three of the 6 values that are always sent in the same order seems pointless.

I am sending the numbers so that I know where it is starting and where it is ending. Yeah I understand what you are saying. I can just put a marker at the beginning and then put three numbers. Will try that.

aarg: If you're doing any serious work with serial comms to the computer, you should install a terminal emulator program on the computer and ditch the serial monitor. I use Tera Term.

Sure I will do that. But I wanted to know that the crashing of the Serial Monitor does not mean that the uP has stopped running the code, or does it?

BTW I decreased the baud rate to 19200.

Why? Talking slowly does not necessarily increase understanding.

Yeah I understand what you are saying. I can just put a marker at the beginning and then put three numbers. Will try that.

Resulting in a 50% decrease in data. Not a bad thing.

But I wanted to know that the crashing of the Serial Monitor does not mean that the uP has stopped running the code, or does it?

It means that the PC end of the channel had problems. You can infer nothing from that about what the Arduino is doing.

bigwetfartman:
I am using Serial Comms to communicate between two Arduinos.

I see a Java error. Java errors only happen on your PC which suggests a problem communicating between a Arduino and a PC and NOT an error between two Arduinos.

I see code for one Arduino which seems just to send back to the Serial Monitor whatever is sent from the serial monitor.

I see no diagram showing how the two Arduinos are connected - I suspect that is where the problem lies.

Also I don’t see any information about which is connected to the PC.

With proper code two Arduinos can communicate for ever.

Have a look at the code in serial input basics

…R

I am using Serial Comms to communicate between two Arduinos.

I've used the below test code to communicate between two arduinos.

//zoomkat 3-5-12 simple delimited ',' string tx/rx 
//from serial port input (via serial monitor)
//and print result out serial port
//Connect the sending arduino rx pin to the receiving arduino rx pin. 
//Connect the arduino grounds together. 
//What is sent to the tx arduino is received on the rx arduino.
//Open serial monitor on both arduinos to test

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >0) {
        Serial.print(readString); //prints string to serial port out
        Serial.println(','); //prints delimiting ","
        //do stuff with the captured readString 
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}