Defyleiti
Defyleiti

Reputation: 555

Sending 2 bytes hexadecimal to a remote serial

I only need to send a hexadecimal like this to a remote serial for the device to accept it.

2 byte hexadecimal I need to send is:

181E

I can telnet to the remote serial and send that command:

telnet x.x.x.x port

181E

I get a response back which is okay.

How can I do this in linux c?

I want to use the write function.

err = write(socket,181E,2);

Or How can I store the 2 byte decimal to a variable so It will be read as 181E?

int this_is_2_bytes = 181E; // Is this correct?

err = write(socket, this_is_2_bytes, sizeof(this_is_2_bytes));

Upvotes: 0

Views: 310

Answers (3)

vishnuxxpxr
vishnuxxpxr

Reputation: 16

The write() function requires a pointer as the second argument.Store it in in character array.

Upvotes: 0

user207421
user207421

Reputation: 310883

You need to send a hexadecimal string. So,

const char cmd[] = "181E";
err = write(socket, cmd, strlen(cmd)); 

Upvotes: 1

L. Cornelius Dol
L. Cornelius Dol

Reputation: 64026

No, writing an int is not correct on all systems. Write a two-element byte array.

Upvotes: 0

Related Questions