How do I send any byte as a single ascii character?

If I convert any byte value to ascii to send it over serial for example, lots of the numbers are lost. The ones below 32 are particularly important. I can cope with going up to 150 but I can't lose the lowest ones. The standard ascii set goes to 127 but is clearly incomplete in unique characters.

for(int sc = 0; sc < 255; sc++){

    Serial.print((char)sc);

  }

Serial monitor output:

e !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~��������������������������������������" 
and so on....

Can the IDE be made to use the extended ascii set?

Alternatively I can format and send all digits separately but it means losing speed in the application.

No, but you could use an external terminal emulator

How will sending a single character solve the problem ?

The data items to send are limited in number.

I'm making a workaround in case there is not extended set that covers the range 0-150. A routine to allocate a character to the numbers missing, and unpack at the other end. It will work but it's intensive on resources.

Why send a char ?
You could send the ASCII value and deal with it at the receiving end

1 Like

No the ascii value needs three digits, is that right?

If you use Serial.write() rather than Serial.print() then the ASCII value will be sent as a single byte

As a matter of interest, what baud rate are you using and are you using a hardware or software serial interface ?

Which Arduino board are you using ?

1 Like

I noticed the problem while debugging via serial, though the hardware uses Bluetooth. It's an ESP32 S3.

Sending lines pTxByte1Data->setValue(TX1ToSend); pTxByte1Data->notify();

where TX1ToSend is a string (ignore the 'byte' in the name).

At the other end is MIT App Inventor and it receives any zany character the ESP32 is asked to send, so it will work if I enumerate the missing characters at both ends.

1. If the value of sc is 65, what bit pattern would be sent over the UART port when the above line is executed? (ignore start, parity, and stop bits.)

2. Given sc = 65, what bit pattern would be sent over the UART port when the following code is executed? (ignore start, parity, and stop bits.)

Serial.print(sc);

The serial monitor is not capable of displaying non-printing characters, so you will not see anything for ASCII characters below 32 (which is itself a space).

The Bluetooth sending was not including those characters also, yet these are reaching the other end OK for example ":smiling_face::slight_smile::heart::diamond_suit::club_suit::spade_suit:".

I'm using a case statement to select characters and any which don't send OK I'll change.

There was a setting which could be altered in the preferences file for Arduino regarding character sets. But the instructions I've found for those seem to be for an old version only.

Before going any further I suggest that you post your full Arduino sketch, using code tags when you do

Where does the string that you are sending originate from and what does it represent ?

I will sort it myself. I do not want to post the sketch.

If you are going to send ASCII control characters, then you will need something on the receiving end that can understand those in the way you want. There are terminal programs that can print out a representation of a control character, or you need to create your own character set that has special characters for non-printing ASCII codes. If all you want is to send binary data, don't expect it to make any sense if displayed in a terminal as ASCII.

1 Like

The redefining of the characters below 32 did not work well. When using other characters the amount of data in each send began to increase because of overheads I think that those characters brought.

I will use multiple sends to get the packet across.

This would be so much easier if you explained exactly what you are trying to do. As explained earlier you can send the ASCII code for each character as a single byte if you write() it instead of using print(). That way the message will be much smaller

The library has options to send bytes but much fewer. With text there are up to 20 per send, so it would have been ideal if the 7 bit ascii set was more complete.

Hard luck.

Which library would that be ?

Are the following equivalent?

Serial.write(65);
Serial.print('A');

You know very well that they are, so what is your point ?

In that case, there is no saving in bytes.