如何在 C+python+UART 中正确表示单词? [英] How to have the correct representation of words in C+python+UART?

查看:24
本文介绍了如何在 C+python+UART 中正确表示单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在串行通信中,我需要将该密文从 python 发送到能够使用 C 读取的 UART.

首先,在python端使用这种技术:

密文=(b'\x24\x70\xb4\xc5\x5a\xd8\xcd\xb7\x80\x6a\x7b\x00\x30\x69\xc4\xe0\xd8')ser.write(密文)

在C端,我将接收到的密文放入缓冲区.我测试如果第一个字节24,包的开始:

if (buffer_RX[0]=='\x24'){for (i=1;i<=17;i++)//sizeof(buffer_RX)==17{printf("%x \n",buffer_RX[i]);}}别的 {printf("这不是数据包!");}

我得到的结果是:

70b4c55ad8光盘b7806a7b03069c40d8

所以我有两个问题,- 拳头是为什么 \x00 以这种方式显示,我的意思是只有一个零 0?- 第二个是我怎么能有同样的表示:

const unsigned int 密文[4] = {0x70b4c55a, 0xd8cdb780, 0x6a7b0030,0x69c4e0d8};

如果我认为这是字节之间的连接,我的意思是可能:

 unsigned int 明文[1];明文[1]= buffer_RX[3]|buffer_RX[4]|buffer_RX[5];

但我不确定,我认为我有语法错误.

此表示将帮助我继续下一步.但我不知道我怎么能得到它.

解决方案

如果您想从 1 字节的块中创建 4 字节长的数据.

const unsigned int 密文[4] = {0x70b4c55a, 0xd8cdb780, 0x6a7b0030,0x69c4e0d8};

对字节进行 OR 是不够的,您必须将它们移到正确的位置.

你目前在做什么,(注意你过度索引 plaintext[1] 这应该是 plaintext[0]):

unsigned int 明文[1];明文[1]= buffer_RX[3]|buffer_RX[4]|buffer_RX[5];

在内存中有这个结果,假设 int 是 32 位,4 字节长:

-----------------------------------------------------------------|3.字节|2.字节|1. 字节 |0. 字节 |--------------------------------------------------------------------------------|0x00 |0x00 |0x00 |缓冲区_RX[3] |缓冲区_RX[4] |缓冲区_RX[5] |--------------------------------------------------------------------------------

所以你必须先用左移运算符将适当的字节移到正确的位置<<,然后是OR之后的字节.

这是一个例子:

#include int main(){无符号字符缓冲区_RX[4];buffer_RX[0] = 0x70;buffer_RX[1] = 0xb4;buffer_RX[2] = 0xc5;buffer_RX[3] = 0x5a;无符号整数明文[1];明文[0] = (buffer_RX[0]<<24) |(buffer_RX[1]<<16) |(buffer_RX[2]<<8) |缓冲区_RX[3];printf("明文:%08x\n", 明文[0]);返回0;}

输出:

内存中:

----------------------------------------------------------------------|3.字节|2.字节|1. 字节 |0. 字节 |----------------------------------------------------------------------|缓冲区_RX[0] |缓冲区_RX[1] |缓冲区_RX[2] |缓冲区_RX[3] |----------------------------------------------------------------------

您可以看到 buffer_RX[0] 已经移动了 24,即 3 个字节,因此它跳过前三个单元格,跳转到最后一个单元格.>

buffer_RX[1] by 16 这是2个字节,所以跳到前两个,buffer_RX[2] by 8 是 1 个字节,因此跳过第一个.并且 buffer_RX[3] 没有移位,因为它移到了第一位.

<小时>

关于0x00,它是用8 位表示的零.如果您打印它,那将是 0.如果您打印 0x0000,它也会是 0.它就是 printf 默认打印 0 的方式,它不会不必要地打印零.如果您在 32 位变量中有 0x70 那么它实际上是 0x00000070printf 将打印 0x70,因为不必要零被砍掉,除非你另有说明.这就是 %02x 的用武之地.在 %02x 中,02 告诉 printf 你想要显示 2字节无论如何它都会在 0x00

的情况下打印两个零

printf("%02x \n",buffer_RX[i]);

如果您想打印 0x00000070 的整个 32 位(4 字节),以下行将执行此操作:

printf("%08x \n",buffer_RX[i]);

In a serial communication, I need to send that ciphertext from python to UART which is able to read it by using C.

At first, In the python side use this technique:

ciphertext=(b'\x24\x70\xb4\xc5\x5a\xd8\xcd\xb7\x80\x6a\x7b\x00\x30\x69\xc4\xe0\xd8')
ser.write(ciphertext)

In the C side, I put the received ciphertext in a buffer. I test If the first byte 24, the Start of the packet:

if (buffer_RX[0]=='\x24')
{
   for (i=1;i<=17;i++) //sizeof(buffer_RX)==17
   {
       printf("%x \n",buffer_RX[i]);
   }
}
else {
   printf("It is not a packet!");
}

The result that I have is:

70
b4
c5
5a

d8
cd
b7
80

6a
7b
0
30

69
c4
e0
d8

So I have two questions, - The fist is why \x00 is displayed by that way, I mean just one zero 0? - The second is how could I have this same representation:

const unsigned int ciphertext[4] = {0x70b4c55a, 0xd8cdb780, 0x6a7b0030,0x69c4e0d8};

If I suppose that is concatenation between bytes, I mean that is maybe:

   unsigned int plaintext[1];
   plaintext[1]= buffer_RX[3]|buffer_RX[4]|buffer_RX[5];

But I am not sure, I think that I have a syntax error.

This representation will help me to continue next steps. But I don't know how could I have it please.

解决方案

If you want to create a 4 bytes long data from your 1 byte chunks.

const unsigned int ciphertext[4] = {0x70b4c55a, 0xd8cdb780, 0x6a7b0030,0x69c4e0d8};

It is not enough to OR the bytes, you have to shift them into the right position.

What you are currently doing, (note that you are over indexing plaintext[1] this should be plaintext[0]):

unsigned int plaintext[1];
plaintext[1]= buffer_RX[3]|buffer_RX[4]|buffer_RX[5];

has this result in the memory, assuming int is 32 bit, 4 byte long:

--------------------------------------------------------------------------------
| 3. byte | 2. byte | 1. byte |                   0. byte                      |
--------------------------------------------------------------------------------
|  0x00   |   0x00  |   0x00  |   buffer_RX[3] | buffer_RX[4] | buffer_RX[5]   |
--------------------------------------------------------------------------------

so you have to shift the appropriate bytes to the right position with the left-shift operator first <<, and OR the bytes after that.

Here is an example:

#include <stdio.h>

int main()
{
    unsigned char buffer_RX[4];

    buffer_RX[0] = 0x70;
    buffer_RX[1] = 0xb4;
    buffer_RX[2] = 0xc5;
    buffer_RX[3] = 0x5a;

    unsigned int plaintext[1];

    plaintext[0] = (buffer_RX[0]<<24) | (buffer_RX[1]<<16) | (buffer_RX[2]<<8) | buffer_RX[3];

    printf("plaintext: %08x\n", plaintext[0]);

    return 0;
}

The output:

In memory:

----------------------------------------------------------------------
|     3. byte     |     2. byte    |     1. byte    |     0. byte    |
----------------------------------------------------------------------
|  buffer_RX[0]   |  buffer_RX[1]  |  buffer_RX[2]  |   buffer_RX[3] |
----------------------------------------------------------------------

You can see that buffer_RX[0] has been shifted by 24 which is 3 byte so it skips the first three cell, jumps to the last one.

buffer_RX[1] by 16 which is 2 byte, so it skips to first two, buffer_RX[2] by 8 which is 1 byte, so it skips the first. And buffer_RX[3] was not shifted because it goes to the first place.


Regarding the 0x00, it is zero represented on 8 bits. If your print it, that will be simply 0. If you print 0x0000 it will be 0 as well. It just how printf prints 0 by default, it does not prints zeros unnecessarly. If you have 0x70 in a 32 bit variable then it is actually 0x00000070 but printf will print 0x70, because unnecessary zeros are chopped off, unless you tell otherwise. That is where %02x comes in. In the %02x the 02 tells printf that you want to display 2 bytes no matter what so it will print two zeros in case of 0x00

printf("%02x \n",buffer_RX[i]);

If you want to print the whole 32 bit (4 byte) of 0x00000070 the following line will do that:

printf("%08x \n",buffer_RX[i]);

这篇关于如何在 C+python+UART 中正确表示单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆