Sending hex value on RS232

Hi There.

I'm new to all this Arduino and programming stuf, so this might seam as a stupid question, so sorry in advance :slight_smile:

Anyways here is what I am trying to do :

I have a program called "Picolog" that interfaces to some specific hardware, instead of using the specific hardware I want to use the arduino to send a value.
My problem is that the value has to be sent in HEX, and has to start with 2D then followed by XX XX ( XX XX Being the values)

Below is my code so far :

int incomingByte = 0; // for incoming serial data
int pin = 7;
unsigned long duration;

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(pin, INPUT);
}

void loop() {

duration = pulseIn(pin, HIGH);

// send data only when you receive data:
if (Serial.available() >0) {
// read the incoming byte:
incomingByte = Serial.read();

if (incomingByte == 0x01){
Serial.write(0x10);
Serial.write(0x0a);

}

if (incomingByte == 0x1F || incomingByte == 0x97){

}

}
}

So far in the section :

if (incomingByte == 0x01){
Serial.write(0x10);
Serial.write(0x0a);
}

I have it working as the program I'm using needs a reply of HEX "10 0a", and this is what I am sending.
But now comes the tricky part and where I get lost.

I now need to get the value from "duration" sent as a hex value, but starting with 2D in HEX, so that when looking at it the transmitted data looks like this ( 2D FF FF) The FF is only a example.

Hope the above is usefull, and that someone can help me, I am lost and need this badly for a test...

Serial.print (x, HEX);?

AWOL :

Here is how the data looks when sniffing it on the code i wrote (Working HEX file )
and also a cutout of the code you suggested (NOT working HEX)

The code you suggested does not work with the program I have.....

Anybody ???

Working HEX.PNG

NOT working HEX.PNG

Your explanation of what you are trying to do is not at all clear.

I have it working as the program I'm using needs a reply of HEX "10 0a", and this is what I am sending.

The 0x0a is a line feed, do all the commands therefor need a line feed at the end?

How many bytes is the receiving side expecting and in what format?

Please read the how to use the forum sticky before posting ant more code.

You are wrong, then, about what you need to send. The receiver is NOT expecting the data in hex form. It DISPLAYS the data it gets in HEX form.

Just use Serial.write() to send the bytes.