I'm trying to output hex commands via serial, but I'm having a little trouble.
long int header = 709248400;
void setup()
{
Serial.begin(9600);
}
void loop()
{
int input;
int output;
input = 1;
output = 1;
take(input, output);
}
void take(int input, int output)
{
Serial.println(header, HEX);
int out1;
int out2;
out1 = 0x10 + (input - 1);
out2 = 0xA0 + (input - 1);
data[0] = out1;
data[1] = 0x00;
data[2] = 0x00;
data[3] = out2;
for(int i = 0; i < 4; i++)
{
Serial.print(data[i], HEX);
}
}
The code I am trying to get it to output is this:
2A 46 45 90
10 00 00 A0
This is what I'm getting:
2A 46 45 90
10 00 A0
Even if I manually serial.print the two 0x00, I only get one 0x00.
I'm trying to control a video router with an RS232 interface. I was able to dump the serial output of the program that controls it into a text file, where I got the hex data above for one of the commands. Am I on the right track? Any ideas? Thanks.
My guess based upon nothing more than your code and the behavior you are describing: Serial.print() does not print the leading zero of a hex number, so when you print 0x00 you get 0, and when you print two 0x00 in a row, you get 00.
Try printing 0x00 four times in a row and see if you get 00 00.
// display a hex nibble (a single hex digit) at your current cursor location
void OrangutanLCD::printHexNibble(unsigned char nibble)
{
if (nibble < 10)
send_data('0' + nibble);
else
send_data('A' + (nibble - 10));
}
// display a two-byte value in hex (0000 - FFFF) at the current cursor location
void OrangutanLCD::printHex(unsigned int word)
{
printHex((unsigned char)(word >> 8));
printHex((unsigned char)word);
}
// display a single-byte value in hex (00 - FF) at the current cursor location
void OrangutanLCD::printHex(unsigned char byte)
{
unsigned char val = byte >> 4;
printHexNibble(val); // display high byte high nibble
val = byte & 0x0F;
printHexNibble(val); // display high byte low nibble
}
You could pretty easily modify this to transmit hex over the UART. Just change send_data() in printHexNibble() to whatever serial function transmits a single character and change the functions to be straight C (i.e. remove the "OrangutanLCD::" part)