Arduino Serial Communications

Hi all, I am trying to send a hex value on the serial port - but cant quite work out how to go about doing this.

For example, the input value is 255, I want the hex value FF to be transmitted on the wire. (1 input, 01 transmitted).

Ive tried a conversion to hex, but its not running as expected - the values dont seem to be correct.

Can anybody point me in the right direction?

Scott

p.s. Ive tried serial.print(value,HEX); to no avail.

What type is value? Where are you receiving the value?

Serial data can be transmitted as values (for values that are byte sized) or as string representations of values.

It sounds like you want to send the value as a string, where the string is the hexadecimal representation of the value. if that's the case:

char valStg[6];
sprintf(valStg, "%X", value);

will get you that string.

its to control a serial DMX device.
The device expects to recieve Hex pairs to set the params. ex 01FF (channel 1 to 255).

I've run the code as above, but something odd still seems to be happening.
I needed to verify that the data being sent is correct, so i have a com cable hooked up to serial1 on the mega.

When I run the code below, FF is echoed in the IDE as I would expect, but 73 73 00 is echoed over the other com port (2 wire, running realterm to capture data) 0 now im confused!

void setup(){
char valStg[6]; //final
int value=255; //starting

Serial.begin(9600); //open soft serial
Serial1.begin(9600); //open hardwired

sprintf(valStg, "%X", value); //convert data

Serial.write(valStg); //write soft
Serial1.print(valStg); //write hard
}

Serial.write(valStg); //write soft
Serial1.print(valStg); //write hard

One write, one print? Sending character data requires print. Which output is correct?

The ide output is correct (write) - the print is giving me 73 73 00 as the data.

Just had a thought, ive got the serial port hooked directly to the mega serial1 and gnd - would I need a max233 or similar in the middle of this setup?

You are using two different methods to send serial data to the two ports - write and print.

One works, one doesn't.

It really shouldn't require too much brain power to figure out a solution.

ive got the serial port hooked directly to the mega serial1 and gnd

What "serial port"? Whatever device you have receiving the data should be hooked to the TX and RX pins for Serial1 on the Mega. Grounds need to be connected, too.

Yes - but no matter which I use, the data isnt consistent.

Here is how it is configured.

Mega > USB > PC (monitor open on com22)

Serial1tx > Other PC Rx (rx > tx and gnd > gnd)

Code modified as follows;

void setup(){
char valStg[6]; //final
int value=255; //starting

Serial.begin(9600); //open soft serial
Serial1.begin(9600); //open hardwired

sprintf(valStg, "%X", value); //convert data

Serial.print(valStg); //write soft
Serial1.print(valStg); //write hard
}

But the serial console within the IDE is showing the recieved data as FF (as I expect), but the other PC is showing the recieved data as 73 73 00 - ssNULL- when I would expect to see FF as the data?

Serial.begin(9600); //open soft serial
Serial1.begin(9600); //open hardwired

This puzzles me - are the comments transposed?

No - first is opening the software port back to the IDE console, second is to open the hardwired connection I have to the other PC from the Mega.

Sorry about this - Im just tyring to figure out why its behaving like it is!

Is there a pattern to the output on Serial1? The 73, 73, 0 is strange. 70, 70, 0 I would understand.

How about setting up a loop, and looping through all values from 0 to 255? It would be interesting to see if there is a pattern.

loop done; output is as follows - I've only included the first 10 of each.

IDE Capture:
01 02 03 04 05 06 07 08 09 10

Serial1 Capture:
FD FB F9 F7 F5 F3 F1 EF ED EB

Im really confused now!

Scott

p.s. Code used (modified on a second run to Serial1):

void setup(){
int x=0;
Serial.begin(9600);
for(x=0;x<255;x++){
Serial.print(x,BYTE);
}
}