neilpa / circulate

Reverse engineered iOS library for controlling the Anova 2 via Bluetooth
MIT License
59 stars 13 forks source link

set date command doesn't work as described #2

Closed palagraph closed 9 years ago

palagraph commented 9 years ago

Here is my experience: command > response read date > 14 10 13 04 13 set date > Invalid Command set date 14 > 0e 0a 0d 04 0e //hex representation of the date in read date the same response for: set date 14 11 10 > 0e 0a 0d 04 0e set date 141110 > 0e 0a 0d 04 0e set date 1234567890 > 0e 0a 0d 04 0e set date 12345678901 > doesn't call "onCharacteristicChange" at all, no Invalid command, no nothing; though if i send an empty command after that it returns the same 02 0a 0d 04 0e set date 14 11 10 22 23 > nothing returned, empty command after that returns 0e 0a 0d 04 08

It may be that this is specific to my implementation (I am running on Android).

P.S.Nope. Run it with your code on iPhone. No response, disconnected.

palagraph commented 9 years ago

I found solution to date and program problems. For some reason one needs to split commands into chunks of 21 bytes (not counting CR). Sending first 21 bytes we don't receive response, now send the rest and you'll get correct response! |set date 14 11 10 22> //nothing returned | 33 > 14 11 10 22 40 |read date>14 11 10 22 40

The same deal with set program. Easy enough to split chunks in the code!

neilpa commented 9 years ago

Oh, this is essentially the same as the response chunking. They're also split and you needed to wait for the terminating newline.

neilpa commented 9 years ago

Although, I don't remember hitting this when testing out myself. Were you suffixing you're commands with a \r or a \n. I think it's required to do the former as you can see here

https://github.com/neilpa/circulate/blob/master/Library/AnovaDevice.m#L137

That may be the source of your issue.

palagraph commented 9 years ago

I end it with CR (0x0d) (it's Android, so it's Java). I am playing with my code right now - verifying the length of chunk.

It seems the same is with your code. I tested with [self sendCommand:@"set date 14 11 10 19 40"];

palagraph commented 9 years ago

OK. Here is perfectly working code (well it works for set date, set program easily grows to 65 charcters, so I have to loop by chunks):

                String commandText = mCommandValue.getText().toString();
                int chunk = 20;
                if (commandText.length() < chunk + 1){
                    byte[] bytecommand = new byte[commandText.length() + 1];
                    System.arraycopy(commandText.getBytes(Charset.forName("US-ASCII")), 0,
                            bytecommand, 0, commandText.length());
                    bytecommand[commandText.length()] = 0x0d;
                    mBluetoothLeService.writeCharacteristic(mAnovaCharacteristic, bytecommand);
                } else {
                    byte[] bytecommand = new byte[chunk+1];
                    System.arraycopy(commandText.getBytes(Charset.forName("US-ASCII")), 0,
                            bytecommand, 0, chunk);
                    bytecommand[chunk] = 0x0d;
                    byte[] bytecommand2 = new byte[commandText.length() - chunk + 1];
                    System.arraycopy(commandText.getBytes(Charset.forName("US-ASCII")), chunk,
                            bytecommand2, 0, commandText.length() - chunk);
                    bytecommand2[commandText.length() - chunk] = 0x0d;
                    mBluetoothLeService.writeCharacteristic(mAnovaCharacteristic, bytecommand);
                    mBluetoothLeService.writeCharacteristic(mAnovaCharacteristic, bytecommand2);