Explain in English (Problemmethod OutputStream.write(byte[],int,int))

Hi guys,

I am using am code called multiwii on my quadcopter and I want to make it communicate with my phone. There is already an application thats doing this but I need to make my own so I looked into how that application was made. Here's what I got:

bt.write("K" + (char) Throttle);
                bt.write("J" + (char) Roll);
                bt.write("H" + (char) Pitch);
                bt.write("G" + (char) Yaw);
                bt.write("F" + (char) ma);

I don't seem to be sure, does it work like this? bt.write("K") bt.write(Throttle)

and so on?

When I try to integrate the code in my processing sketch, I get this error:

Code:

ons.write("K" + Throttle); // Throttle
        ons.write("J" + Roll);
method OutputStream.write(int) is not applicable
    [javac]       (actual argument String cannot be converted to int by method invocation conversion)
    [javac]     method OutputStream.write(byte[],int,int) is not applicable
    [javac]       (actual and formal argument lists differ in length)
    [javac]     method OutputStream.write(byte[]) is not applicable
    [javac]       (actual argument String cannot be converted to byte[] by method invocation conversion)

So im guessing, the ons.write(ons is outputStream) doesnt like characters like "K" ?

String constants are just char pointers, really. And you can add an integer to a char pointer, to move the pointer. For example:

Serial.write("abcdefgh" + 4); // writes "efgh"

Unfortunately, you move the string pointer further than the size of the string, because the string is only 1 character long. If you instead want to output the integer value converted to string, look into itoa(), or sprintf(), or perhaps a write() function that writes integers:

bt.write("K"); bt.write((int)Throttle);

Thanks for your quick reply, but I dont seem to get it. My knowdledge of arduino programming is limited so please be patient :D. What does a char pointer do? It just tells you where to start? I've tried to comprehend your explanation and searched online but cant seem to get it right. Also, can't the "K" be a handshaking methode? Also, it isnt important that the "K" is before the variable? Thanks

What does a char pointer do?

It points to a character. That is it is a number that is an address in memory and that address contains a character.

What does a char pointer do?

It points to the start of a block of char addresses.

Also, can't the "K" be a handshaking methode?

Sorry, I don't understand this question.

Your original (flawed) code was trying to perform string concatenation in a way that appears to you to be logical, but is not a supported way of doing it. The actual things being concatenated have nothing to do with the compiler's inability to process your statement.

Also, it isnt important that the "K" is before the variable?

It may, or may not, be important to the device on the other end of whatever it is you are writing to, but it has nothing to do with the fact that you can not concatenate disparate types and expect to get a string that can be sent.

Print::print() knows how to convert a lot of things to strings, and send them. But, there are limits. The most important one is that there must be just one thing, unless the thing is an array of characters that is NULL terminated.

Print::write() is even pickier, in that it only knows how to send bytes (or chars, which are the same size) one at a time or as an array. It does not convert non-string data to string data as print() does. You must do that before the call to the function.

Actually, now that I re-read the question, it seems he's actually asking why his JAVA program is not compiling. The JAVA program is supposed to communicate with an Arduino. However, the question you're asking has everything to do with how to program in JAVA for a PC, and nothing at all to do with how to program for an Arduino, so you're asking in the wrong forum.

Curiously, my answer (which assumed the posted code was C code for the Arduino) actually is still mostly right: Java does not have an "operator+()" on the String class that takes a char argument. You might want a StringBuilder or some kind of string formatter class to build your string first, but the best place to ask about that is some Java programming forum, not here.

bt.write("K" + (char) Throttle);

Without seeing more of the original code, it's difficult to tell whether this is more like:

Serial.write('K'); Serial.write((char)Throttle);

Serial.write('K'); Serial.print(Throttle);

(ie whether the value following the command character is sent as a binary byte, or as ascii text...)

Sorry for the late reply, i`ve been busy and didnt have time to work on this. At first your replys looked like : ??? x? x +1 ? ???? ??. Then I used google and it was better. However, like jwatte said, the code that I posted is in java(processing) and its what I need to communicate with the arduino.(lets say that I cant modify the code on the arduino). From the original code, this is what I think he does: He adds the char K to a (now he writes char)255byte value. Then he retransforms all of that in byte value. Why? What is the end result?

Also, can't the "K" be a handshaking methode? Sorry, I don't understand this question.

Yeah, handshaking wasnt the best term. By handshaking, I meant that one device sends a value to another(ex : 255) and when the other device receives it, it send 255 aswell. This way, they are on the same page(before sending ex: throttle).I thought that the K was doing the same thing to the arduino.

Any thoughtt?