Zeyad Serag
Zeyad Serag

Reputation: 21

Send Hex data to a serial port?

I have a GPS connected to USB0 and I can read everything perfectly but now I am trying to write data to it. It works with sending and receiving Hexadecimal data. I tried to send data to the GPS. Let's say I want to send 0xB6 0x62 to the port how could I do it in C++. I used this but I don't know weatehr I am right or wrong could someone help me

int main()
{
    unsigned char bytestosend[2] = {0xB5, 0x62};

    write(fd,&bytestosend,2);
}

of course I open the port to the file descriptor fd.

Upvotes: 1

Views: 2669

Answers (1)

zmo
zmo

Reputation: 24812

basically, what you're doing is right given your GPS reads actually hexadecimal bytes. But it may as well read string encoded hexadecimal values, so be sure of that. So from what you tell, I'd say you're writing to it correctly.

And you don't say how you open the serial port, but be careful to use termios and set the connection up correctly, or you may get issues. You shall not just open the port like a standard file. If you haven't done it yet, have a good read of this!

Upvotes: 1

Related Questions