Go Down

Topic: print vs write in Java (Read 1 time) previous topic - next topic

mynameisjohnj

Hi Guys,

I'm trying to port Jeff rowberg's MPU6050 teapot sketch from processing into Java,  but I've run into a problem. Rowberg has his arduino do Serial. print statements for debugging and information purposes,  and uses serial. write to output data.

https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050/Examples/MPU6050_DMP6

processing seems to have no trouble printing to the serial monitor whilst handling data from writes, but in Java they all get clumped together. I've been following this class

http://playground.arduino.cc/Interfacing/Java

which uses a bufferedreader to handle serial events and create strings from the data,  data that comes comes from both serial. print and serial. write. I can parse these strings into ints or floats or whatever, but that seems very clunky. is there a way to have serial. print statements go straight to the monitor and serial. write statements be read in as data?  this is what processing is doing inherently, so I feel like Java can handle it.


thanks,  and sorry for not putting in real code. I'm on a tablet, but if desired I'll gladly dig out the code from those links - it's the SerialEventHandler in Java and any print or write statements in arduino.

PaulS

I'm not sure that I follow your question. It does tend to ramble.

The print() method converts the value to a string and sends it to the serial port. The write() method does not convert the value to a string. It writes the value as-is to the serial port.

It is up to the device on the other end to know whether the data was sent to the serial port as ASCII data (using print()) or as binary data (using write()).

If it is the Arduino that is mixing print() and write(), then Processing, or your Java application, needs to know which parts of the serial data stream were output which way.

If it is Processing, or your Java application, that is mixing print() and write(), then the Arduino will need to know which parts of the serial data stream were output which way.

In general, it's best not to mix and match.
The art of getting good answers lies in asking good questions.

mynameisjohnj

Fair Enough. Let me ask it like this:

How would you print debugging statements from the arduino without them getting in the way of your data?

I think I was mistaken. When Rowberg outputs data, he starts if off with a '$' character. The method below seems to handle this data by checking for the '$' character, but I have no idea how he handles regular string print statements.

Code: [Select]
void serialEvent(Serial port) {
    interval = millis();
    while (port.available() > 0) {
        int ch = port.read();
            // DA - Added this next if statement because we seem to be getting
        //  off byte boundary with initialization warnings
        if(!gotFirstDataByte) {
          if (ch == '$') {
            gotFirstDataByte = true;
          } else {
            return;
          }
        }
        //print((char)ch);
        if (aligned < 4) {
            // make sure we are properly aligned on a 14-byte packet
            if (serialCount == 0) {
                if (ch == '$') aligned++; else aligned = 0;
            } else if (serialCount == 1) {
                if (ch == 2) aligned++; else aligned = 0;
            } else if (serialCount == 12) {
                if (ch == '\r') aligned++; else aligned = 0;
            } else if (serialCount == 13) {
                if (ch == '\n') aligned++; else aligned = 0;
            }
            //println(ch + " " + aligned + " " + serialCount);
            serialCount++;
            if (serialCount == 14) serialCount = 0;
        } else {
            if (serialCount > 0 || ch == '$') {
                teapotPacket[serialCount++] = (char)ch;
                if (serialCount == 14) {
                    serialCount = 0; // restart packet byte position
                   
                    // get quaternion from data packet
                    q[0] = ((teapotPacket[2] << 8) | teapotPacket[3]) / 16384.0f;
                    q[1] = ((teapotPacket[4] << 8) | teapotPacket[5]) / 16384.0f;
                    q[2] = ((teapotPacket[6] << 8) | teapotPacket[7]) / 16384.0f;
                    q[3] = ((teapotPacket[8] << 8) | teapotPacket[9]) / 16384.0f;
                    for (int i = 0; i < 4; i++) if (q[i] >= 2) q[i] = -4 + q[i];
                   
                    // set our toxilibs quaternion to new data
                    quat.set(q[0], q[1], q[2], q[3]);
                }
            }
        }
    }
}


what's confusing me is where he actually prints in processing; he mentions initialization warnings, but after reading them in he never calls Println. He does, however, call print from the Arduino several times without using the '$' character, and I don't know how that gets through to the serial monitor.

This post was probably more confusing than the last...if it doesn't make sense don't worry about it. The bottom line is that I'm going to implement my own method by checking for the '$' character and doing what I need to, but if anyone wants to offer some insight it would be welcome.

PaulS

Quote
How would you print debugging statements from the arduino without them getting in the way of your data?

Debugging output is output. Keeping that separate from incoming data requires no effort.

Keeping debug output separate from important output is easy. All output is important. Some is important only for a short time (while debugging). Some is important at all times.

Perhaps I don't really understand the question.

Quote
what's confusing me is where he actually prints in processing; he mentions initialization warnings, but after reading them in he never calls Println. He does, however, call print from the Arduino several times without using the '$' character, and I don't know how that gets through to the serial monitor.

I'm afraid that the snippets you've posted so far don't contain enough code to be able to discuss your concerns.

Quote
This post was probably more confusing than the last...if it doesn't make sense don't worry about it.

I'm not worried, then.

Quote
The bottom line is that I'm going to implement my own method by checking for the '$' character and doing what I need to, but if anyone wants to offer some insight it would be welcome.

Typically, the Arduino would send debugging messages to the serial port, and the Serial Monitor would be used to provide input. Then, when it is clear that the Arduino processes the input correctly, the Serial Monitor application is closed, and some other application is used to send data to the Arduino.

If the Arduino does not, then, produce the correct results with the input that the application sends it, that data is captured in the sending application, and the debugging session(s) involving the Serial Monitor are repeated.

You could have the Arduino send something like "$This_is_important_data" or "%This_is_debug_data", and have the receiving application deal with the different packet types.
The art of getting good answers lies in asking good questions.

Go Up