Before sending it i add an STX, ETX and a check sum using the following code.
void sendMessage (String inString)
{
// Declare local Vars
String outString;
String message;
long total = 0;
// Add stx and etx
outString = char(02) + inString + char(03);
int len = outString.length() + 1; //working out length of the message
byte ba[len];
outString.getBytes(ba, len);
for (int i = 0; i < outString.length(); i++) //XOR each char together
{
int a = (ba[i]);
total = total ^ a;
}
message = outString + char(total);
Serial3.print(message);
Serial.println("Message Sent To Panel: " + message);
total = 0;
outString = "";
}
However the Arduino seems to send : T! ON00:0023:59ÿ00:0000:00€00:0000:00€00:0000:00€è
The out putted string is different to the original string. What is the best way for me to send the string so all of the characters are displayed correctly?
Hi Thanks for the reply. It wont be easy for me to add the whole program as i would need to change a lot of the messages and passwords ETC. The only issue I am having is with this particular message. I understand its not helpful if you can only see part of the program but I thought it may be possible to get some help on the specific part.
I can go trough and remove the Passwords and messages if needed but if you have any ideas i could try before hand it would be fantastic.
I made a quick Sketch to test the message I am sending the code is below.
void setup() {
Serial.begin(9600);
Serial3.begin(1200);
}
void loop() {
sendMessage(" T! ON00:0023:59ÿ00:0000:00€00:0000:00€00:0000:00€");
delay(5000);
}
void sendMessage (String inString)
{
// Declare local Vars
String outString;
String message;
long total = 0;
// Add stx and etx
outString = char(02) + inString + char(03);
int len = outString.length() + 1; //working out length of the message
byte ba[len];
outString.getBytes(ba, len);
for (int i = 0; i < outString.length(); i++) //XOR each char together
{
int a = (ba[i]);
total = total ^ a;
}
message = outString + char(total);
Serial3.print(message);
Serial.println("Message Sent To Panel: " + message);
total = 0;
outString = "";
}
This is what the Arduino was returning:
Message Sent To Panel: T! ON00:0023:59ÿ00:0000:00€00:0000:00€00:0000:00€a
The code worked fine until i saved the sketch. After saving the sketch and uploading it again the Arduino was returning the following:
Message Sent To Panel: T! ON00:0023:59ÿ00:0000:00€00:0000:00€00:0000:00€È
The problem is that when the IDE writes the sketch to a file, it changes the encoding of the characters. When written to the file, the character ÿ is encoded in the file as the two characters ÿ and € is encoded as the three characters €
It is converting Unicode to UTF-8 (or vice versa, I forget which way round it is).
The IDE interprets these multi-character symbols correctly but the serial monitor obviously doesn't.
At the moment, I can't think of a way around it.
Unicode characters take 2 bytes each so won't be easily supported by an Arduino.
But the two characters you have used (€ and ÿ) can be represented by extended ASCII codes 128 and 255.
To include these in a string you need to use octal or hex. The octal numbers for these are 200 and 777, so using escape characters (with a backslash) will work (even after saving):
el_supremo:
Re: 777
Don't you mean 377? 777 octal is 9 bits.
Pete
Correct! My mistake (I've no experience with octal), but the compiler (or the processor?) ignores the overflowed bit and it still gives the correct character. It should also work with hex escapes ('\x80' and '\xFF') which I'm far more familiar with but I tried that and the string wasn't printed after the first escaped char.