简体   繁体   English

将2个字节的十六进制发送到远程串行

[英]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: 我需要发送的2个字节的十六进制是:

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? 如何在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? 或如何将2字节的十进制数存储到变量中,以便将其读取为181E?

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

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

You need to send a hexadecimal string . 您需要发送一个十六进制字符串 So, 所以,

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

No, writing an int is not correct on all systems. 不,在所有系统上写int均不正确。 Write a two-element byte array. 编写一个两个元素的字节数组。

write()函数需要一个指针作为第二个参数,并将其存储在字符数组中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM