Can you please clarify the data type of what you want to send ? From your question it would seem that you want to sends string of characters so why not just print it ?
Sure; I have an system (EFI ECM) and companion PC application that sends out a serial stream to a pc USB port where the application picks up the USB serial stream and displays the received data as gauges. I want to set up an arduino to send out a stream for testing a bluetooth and com0com/hub4com setup. So I need the serial bits of the hex values sent out via the USB connection of the arduino and have them picked up by the application. The application converts the serial bit stream to decimal values and displays their decimal values on the gauge(s).
As I understand it, you have this 'test' string in HEX to send.
You need to
a) convert each pair of the hex chars to a single byte and then
b) write( ) that byte to the Serial connection
Below is an example sketch.
with TESTING not comment out it converts the hex string to bytes and prints the binary version.
with
//#define TESTING commented out
it send the binary to the Serial output.
Here is some sample output
testOutput is sfTest cap:37 len:37 '55AA000280080824802040011101000000000'
testOutput does not have even numbers of chars. Ignoring last char!!
01010101 10101010 00000000 00000010 10000000 00001000 00001000 00100100 10000000 00100000 01000000 00000001 00010001 00000001 00000000 00000000 00000000 00000000
Here is the sketch
// download SafeString library from Arduino library manager
// or from the tutorial page
// https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html
//
#include "SafeString.h"
// uncomment this define to send test output to Serial in char form
#define TESTING
char testOutput[] = "55AA000280080824802040011101000000000";
//char testOutput[] = "05AA";
void printBinToDebug(uint8_t b) {
cSF(sfBin, 8); // 8 bin digits
sfBin.print(b, BIN);
while (!sfBin.isFull()) {
sfBin.prefix('0');
}
SafeString::Output.print(sfBin);
}
void setup() {
Serial.begin(9600);
for (int i = 10; i > 0; i--) {
Serial.print(' '); Serial.print(i);
delay(500);
}
Serial.println();
#ifdef TESTING
SafeString::setOutput(Serial); // commenting this out removes error msgs and debug printing
#endif
// wrap the testOutput in a SafeString from processing
cSFA(sfTest, testOutput); // or createSafeStringFromArray(sfTest,testOutput); cSFA is just shorthand
sfTest.debug(F(" testOutput is "));
if ((sfTest.length() % 2) != 0) {
SafeString::Output.println(F(" testOutput does not have even numbers of chars. Ignoring last char!!"));
}
cSF(sf2hex, 2); // as SafeString to hold two hex chars
size_t startIdx = 0;
size_t endIdx = startIdx + 2;
long hexValue;
while (endIdx <= sfTest.length()) {
sfTest.substring(sf2hex, startIdx, endIdx);
if (sf2hex.hexToLong(hexValue)) {
// valid hex
#ifdef TESTING
printBinToDebug(hexValue); SafeString::Output.print(' ');// for testing
#else
Serial.write((uint8_t)hexValue); // send as unsigned 8 bits <<<<<< this is the real data when not TESTING
#endif
} else {
//invalid skip this one
SafeString::Output.print(sf2hex); SafeString::Output.println(F(" is not valid hex"));
}
startIdx = endIdx;
endIdx += 2; // next pair
}
}
void loop() {
}
I understand from this post that to send a hex value out of the serial/usb port, you need to use the form "Serial.write(0xAA);", one byte at a time or as an array. I have 273 bytes to send in the form shown above, so I'd have to re-code the string with commas and the '0x' string. I'd like to find a library that I can call to do this for me by sending it the raw hex string.
I guess I could have done manually it in the time it took to explain it, but I'm looking for a general answer. Sorry.
The function that you're looking for is strtoul. You take two characters of your text, put them in a 3 character array and convert with strtoul. Take the next two characters and repeat.
Serial.write() sends the raw value of the byte, so if you use Serial.write(0x45); then 69 (decimal) will be sent. The 0x is only necessary if what you have is in hex
Given "55AA000280080824802040011101000000000" please confirm that you want to send 0x55 then 0xAA then 0x00 etc ?
'5' cannot go out as 0101 unless accompanied by 4 more bits, so I presume that "51" should go out as 01010001 or perhaps you mean that it should go out as 00000101 followed by 00000001, which is unlikely but possible.