Linux中通过串口通信发送16进制数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6555541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending Hexadecimal data through Serial Port Communication in Linux
提问by Prashant Singh
I have got a task of sending hexadecimal data to my COMPORT in linux. I have written this simple C code, but it sends only a decimal number. Can anyone help me in sending an hexadecimal bit.
我的任务是在 linux 中将十六进制数据发送到我的 COMPORT。我写了这个简单的 C 代码,但它只发送一个十进制数。谁能帮我发送一个十六进制位。
Here is the code I have written
这是我写的代码
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
int number,n;
void main(void){
open_port();
}
int open_port(void)
{
int fd; /* File descriptor for the port */
fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyACM0 - ");
}
else{
printf("Port Opened successfully\n");
number = 1;
while(number!=55){
scanf("%d",&number);
n = write(fd, "ATZ\r", number);
if (n < 0)
fputs("write() of 4 bytes failed!\n", stderr);
}
}
return (fd);
}
Please help
请帮忙
Thanks in advance :) :)
提前致谢 :) :)
采纳答案by Yann Ramin
write
is defined as:
write
定义为:
ssize_t write(int fd, const void *buf, size_t count);
That is, it sends count
bytes to fd
from buf
. In your case, the data is always the string "AZTR\r", plus undefined data after that (if count is > 5). Your program sends neither hexadecimal nor decimal data.
也就是说,它将count
字节发送到fd
from buf
。在您的情况下,数据始终是字符串“AZTR\r”,加上之后的未定义数据(如果计数 > 5)。您的程序既不发送十六进制数据,也不发送十进制数据。
Do you want to send binarydata or a string of hexadecimalcharacters?
您要发送二进制数据还是一串十六进制字符?
For option one, you can use: write(fd, somebuffer, len);
, where some buffer is a pointer to any set of bytes (including ints, etc).
对于选项一,您可以使用: write(fd, somebuffer, len);
,其中某些缓冲区是指向任何字节集(包括整数等)的指针。
For option two, first convert your data to a hexadecimal string using sprintf
with %02X
as the format string, then proceed to write
that data to the port.
对于选项二,首先使用sprintf
with%02X
作为格式字符串将您的数据转换为十六进制字符串,然后继续write
将该数据发送到端口。
回答by wallyk
There are several problems with the code:
代码有几个问题:
- The text read from the console is interpreted as decimal (
"%d"
); if you want it to be interpreted as hexadecimal, use"%x"
. - The
write()
is pathological. The third argument is the number of bytes to write, not the value. It should be eithern = write (fd, "ATZ\r", 4); // there are 4 bytes to write to init the modem
- 从控制台读取的文本被解释为十进制 (
"%d"
);如果您希望将其解释为十六进制,请使用"%x"
. - 这
write()
是病理性的。第三个参数是要写入的字节数,而不是值。它应该是n = write (fd, "ATZ\r", 4); // there are 4 bytes to write to init the modem
or
或者
char buf[10];
n = sprintf (buf, "%x", number); // convert to hex
n = write (fd, buf, n); // send hex number out port
回答by Skaag Argonius
This function will take a hex string, and convert it to binary, which is what you want to actually send. the hex representation is for humans to be able to understand what is being sent, but whatever device you are communicating with, will probably need the actual binary values.
此函数将接受一个十六进制字符串,并将其转换为二进制,这就是您要实际发送的内容。十六进制表示是为了让人类能够理解正在发送的内容,但是无论您正在与之通信的设备是什么,都可能需要实际的二进制值。
// Converts a hex representation to binary using strtol()
unsigned char *unhex(char *src) {
unsigned char *out = malloc(strlen(src)/2);
char buf[3] = {0};
unsigned char *dst = out;
while (*src) {
buf[0] = src[0];
buf[1] = src[1];
*dst = strtol(buf, 0, 16);
dst++; src += 2;
}
return out;
}