Printing special chars to serial

Hi,

I am trying to send some specific chars using an array. The chars I am trying to send are ÿ(0x96) and €(0x80)

Ive tried some different ways of storing these chars in the array but none of seem to be printing the correct ASCII representation.

Bellow is my code...

char *  testChar[] {"Hello World", 0x80, 0x9f, "\x80", "\x9f", char(128), char(159), "\200", "\377"};

void setup() {
  Serial.begin(9600);
  uint8_t numStrings = sizeof(testChar) / sizeof(testChar[0]);
  String myString = "";
  for (uint8_t i = 0; i < numStrings; i++) 
  {
    myString += testChar[i];
  }
  sendMessage(myString);
  Serial.println("\200");
}

void loop() {
  // put your main code here, to run repeatedly:

}

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);
  Serial.println("SendMessage: " + message);
  total = 0;
  outString = "";
}

The above code displays this...

SendMessage: Hello World⸮⸮⸮nr⸮ޭ~⸮⸮O⸮⸮⸮⸮z⸮⸮G⸮⸮⸮⸮ g⸮yur_⸮G'[⸮7⸮⸮[⸮I⸮⸮⸮⸮⸮˝⸮⸮⸮z⸮}⸮z⸮⸮a⸮_⸮G'[⸮7⸮⸮[⸮I⸮⸮⸮⸮⸮˝⸮⸮⸮z⸮}⸮z⸮⸮a€⸮⸮

Any help would be much appreciated. Thanks

Where are the null terminators in testChar[]?

aarg:
Where are the null terminators in testChar[]?

:o whoops

ive changed my testChar[] to...

char *  testChar[] {"Hello World", 0x80, 0x9f, "\x80", "\x9f", char(128), char(159), "\200", "\377", "\0"};

Is that correct?

I still get the same problems on the serial monitor.

rich_330:
ive changed my testChar[] to...

char *  testChar[] {"Hello World", 0x80, 0x9f, "\x80", "\x9f", char(128), char(159), "\200", "\377", "\0"};

Is that correct?

Why are you initializing character pointers by strange char/integer values?

rich_330:
ive changed my testChar[] to...

char *  testChar[] {"Hello World", 0x80, 0x9f, "\x80", "\x9f", char(128), char(159), "\200", "\377", "\0"};

Is that correct?

No. Each array element is supposed to be a complete Cstring. So it has to be terminated with a null. You have not done that. Only the elements in quotation marks have it, because the compiler adds one to the end of a defined constant string automatically.

As Whandall has pointed out, the values not in quotation marks won't work at all because they aren't pointers. If you fix that, actually you don't need to think about null termination because it will be done for you.

aarg:
No. Each array element is supposed to be a complete Cstring. So it has to be terminated with a null. You have not done that. Only the elements in quotation marks have it, because the compiler adds one to the end of a defined constant string automatically.

Ok. So i have changed the testChar[] to...

char *  testChar[] {"Hello World", "\x80", "\x9f"};

but I still seem to get the wrong char on the serial monitor. Bellow is what I am seeing

SendMessage: Hello World⸮⸮>

Have you tried connecting with a "real" serial terminal software, like Putty or TeraTerm?

Also why do you need an array at all, you can just use one string... e.g.

char testCharString = "Hello World\x80\x9f";

aarg:
Have you tried connecting with a "real" serial terminal software, like Putty or TeraTerm?

Good idea!

Ive just tried Putty and I'm getting the same serial output

Your characters are not in ISO-8859-1. I can print some that are:

char testCharString[] = "Hello World\xff\xa5";

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
delay(1000);
Serial.println(testCharString);
}

void loop() {
}

The output is the expected:

Hello Worldÿ¥

Is there any way to get the 0x9f and 0x80 values?

I think I'm more after the hex value you to be sent rather char if that makes sense?

Im trying to interface with an existing 232 protocol.

rich_330:
Is there any way to get the 0x9f and 0x80 values?

I think I'm more after the hex value you to be sent rather char if that makes sense?

Im trying to interface with an existing 232 protocol.

No, you can't print those values because they are control codes in code page C1 (i.e. non-printable characters). Well, I showed you that you can print y-umlaut. But the Euro symbol doesn't exist in that code page.

So I cant send the Hex value?

Unfortunately the y-umlaut was different the expected one I copied them both into notepad ++ and compared them.

In the past i have used the octal value 200 and 377 but i don't seem to be able to get that to work either.

if i use this

  Serial.println("ÿ");

It will print out the serial the correct value. it just harder to do the maths on.

The device protocol I am using packs the address of the device with 0x80 so for address 1 i would need to send 0x81 etc

So why are you concerned that the values are non-printable by a serial monitor or client? Why would you even expect them to be printable? I thought that was the thing that didn't work... now it sounds like there is some other requirement that you haven't told us about that is failing. What doesn't work currently?

You sure could have saved us some time by mentioning this "device protocol" up front.

aarg:
You sure could have saved us some time by mentioning this "device protocol" up front.

Next time I will mention it. Thanks for the friendly advice.

I'm comparing this to another device that works. the device that works shows the correct symbol "ÿ(0x96)" in putty.

The bit that doesn't work is the fact i try and send 0x96 and receive a completely different value.

Thanks again for your time.

How do you know that your "device" is actually sending 0x96?

aarg:
How do you know that your "device" is actually sending 0x96?

I guess I don't... wouldn't sending a 0x96 be the same as sending the actual char?

Sorry if I'm being stupid...