Problem interfacing Arduino MEGA while working correctly on UNO

Hello,

Does anyone know if there is some subtle differences in the Serial communication between MEGA and UNO? Because I have the exacte same code on the Arduino Firmware that send data on a Java software and that works perfectly with an UNO but nothing is received on the MEGA. Unfortunatelly, I need to use the MEGA because I developed a special shield for the MEGA...

Here are the two pieces of code (Firmware and Software). It is basically a synchronous protocol to request data from the software. The Java software use the JSSC library to communication on the serial port.

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


unsigned long timestamp;

void setup()
{
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
  
  
  // put your setup code here, to run once:
  Serial.begin(115200);
  
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
  
  
  delay(100);
}

void loop()
{
  
  serialEvent(); //call the function
  // print the string when a newline arrives:
  if (stringComplete)
  {
    //Serial.println(inputString);
    // clear the string:
    
    if(inputString.compareTo("/data\n") == 0)
   {
      timestamp = millis();

      Serial.print("/data");
      Serial.print("/");
      Serial.print(timestamp);
      Serial.print("/");
      Serial.print("3");
      Serial.print("/");
      Serial.print("3");
      Serial.print("/");
      Serial.print("3");
      Serial.print("/");
      Serial.print("3");
      Serial.print("/");
      Serial.print("3");
      Serial.print("/");
      Serial.print("3");
      Serial.print("/");
      Serial.print("3");
      Serial.print("/");
      Serial.print("3");
      Serial.print("/");
      Serial.print("3");
      Serial.print("/");
      Serial.print("3");
      Serial.print("/");
      Serial.print("3");
      Serial.print("/");
      Serial.print("3");
      Serial.print("/");
      Serial.print("3");
      Serial.print("/");
      Serial.print("3");
      Serial.print("/");
      Serial.print("3");
      Serial.print("/");
      Serial.print("3");
      Serial.println();       
    }
    else
    {
      Serial.println("nil");
    }
    
    inputString = "";
    stringComplete = false;
  }


}

// ******************
// *** FONCTIONS ****
// ******************

void serialEvent()
{
  while (Serial.available())
  {
    // get the new byte:
    char inChar = (char)Serial.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;
    }
  }
}
System.out.println("transmit data");
            
            serialPort.writeString("/data\n");
            
            
            String received;
            received = serialPort.readString();
            System.out.println(received);
            Sdata = received.split("/");
            
            System.out.println("data read");

When I run the Java code and an UNO, I read the value, when I use the MEGA I just read NULL. Of course, in both case, if I use the Arduino Serial Monitor and resquest data, I received them OK.

Thanks a lot for your time and help, I don't really know what to do at this stage... David

There is no difference between serial on the Uno and on the Mega that would affect com's. the problem must be on the java end. Are you using the correct port?

Mark

How was your serialPort object instantiated in java?

Hi all,

First, thanks a lot for your reply, it make the situation much more encouraging ;)

Okay, in both case, I am using the same port COM (similar one to the one used in Arduino soft to program it) which is: /dev/cu.usbmodem1411

Here is the way I open the port:

First, once the main object is instanced, I create a serial object from JSSC

serialPort = new SerialPort("/dev/cu.usbmodem1411");

And then, when I connect to the port:

try {
            System.out.println("Port opened: " + serialPort.openPort());
            System.out.println("Params setted: " + serialPort.setParams(115200, 8, 1, 0));

            
        } catch (SerialPortException ex) {
            Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);

            connection = false;
        }

No two arduino have the same port name on a mac.

No two arduino have the same port name on a mac.

I have found that the port name on a mac is associated with the USB port, not the Arduino. I have a USB hub, and I sometimes have two Arduino boards plugged in at the same time. But if I remove them and interchange them, the two port names remain, but now communicate with the exchanged boards.

Yes, it seems so, which ever Arduino I plug on the same USB port will have the same port.

By the way, in my case, it doesn't seem to be a matter of connection because I pass those lines OK

System.out.println("Port opened: " + serialPort.openPort());
            System.out.println("Params setted: " + serialPort.setParams(115200, 8, 1, 0));

as I received the messages in the console with both Arduino,

the problem really happen later, so either that the send message "/data" doesn't work so the arduino doesn't send back anything or either the readString() doesn't work because at the end I receive a NULL

jrdoner:
I have found that the port name on a mac is associated with the USB port, not the Arduino. I have a USB hub, and I sometimes have two Arduino boards plugged in at the same time. But if I remove them and interchange them, the two port names remain, but now communicate with the exchanged boards.

I have never used any USB hubs on my mac (now stuck after upgrade to el cap). Anyway, on my mac and an older one, it has always been an alphanumeric sequence that is the last few characters of the USB devices unique ID. If you don’t use a hub, do you still see this identical name instead of unique names?

Just to be 100% sure, I did again a test with a similar firmware on an UNO and a MEGA (another board with no shield attached by the way). Both COM port have the same name: "/dev/cu.usbmodem1411"

With the UNO, I receive the expected string: "/data/112/3/3/3/3/3/3/3/3/3/3/3/3/3/3/3/3" with the MEGA, I receive a NULL.

And again, if tested with Arduino Serial Monitor, I will receive "/data/112/3/3/3/3/3/3/3/3/3/3/3/3/3/3/3/3" with both case.

Only the reception with my Java software seems affected.

Some updates:

To debug this issue, I put a long delay (100ms) between the sending of the string "/data" and the reading of the returned string. With this long delay, it turns out that it sometime works on the MEGA (but not at all runs, ~25% of the time). This made me thing that the UART timeframe on the MEGA might have some jitter or something like this and so it is sometime recognized as ok by JSSC (Port COM on Java) but sometimes not... I tried to change de baudrate from 115200 to 19200 but it kept the same random working condition.

The current lead I'm following to try to debug is the when I'm in a state where it is not working, only the RX led blink, so the Arduino should receive something but does not replay anything... Other interesting fact, when it is in a working state (i.e. on the UNO or sometimes on the MEGA), the led L is off while it is turned on when the transmission is not working.

This being said, this post would maybe better fit in the thread "Installation & Troubleshooting" as it seems really linked to the Arduino MEGA UART specifically. Could a moderator do the change?

Thanks a lot, Best Regards, David

I have a Mega, 2 Unos, a Leonardo, 2 Sparkfun ProMicros and a few breadboard Atmega 328a. I have never noticed any difference between the serial communication with my PC - using Linux on the PC and Arduino IDE 1.5.6. I also have some Python programs that communicate with the Arduinos.

...R