简体   繁体   English

Arduino-如何从serial.read()提供结构?

[英]Arduino - how to feed a struct from a serial.read()?

I am a beginner and I am trying to feed a struct table with 4 members typed BIN with a pointer, then send them to another one, serial2. 我是一个初学者,我正在尝试用一个具有指针的BIN类型的4个成员喂一个结构表,然后将它们发送给另一个成员,serial2。 I fail to do so. 我没有这样做。

I receive 4 chars from serial1.read() , for example 'A' '10' '5' '3'. 我从serial1.read()收到4个字符,例如' serial1.read() '。 To decrease the size of the data, I want to use a struct: 为了减少数据的大小,我想使用一个结构:

struct structTable {
  unsigned int page:1; // (0,1)
  unsigned int cric:4; // 10 choices (4 bits)
  unsigned int crac:3; // 5 choices (3 bits)
  unsigned int croc:2; // 3 choices (2 bits)
};

I declare and set: instance and pointer 我声明并设置:实例和指针

struct structTable structTable;
struct structTable *PtrstructTable; 
PtrstructTable = &structTable;

Then I try to feed like this: 然后,我尝试这样喂食:

for(int i = 0; i<=4; i++) {
  if(i == 1) {
    (*PtrProgs).page = Serial.read();
  if(i == 2) {
    (*PtrProgs).cric = Serial.read();

And so on. 等等。 But it's not working... 但这不起作用...

I tried to feed a first char table and tried to cast the result: 我尝试提供第一个char表并尝试强制转换结果:

(*PtrProgs).page = PtrT[1], BIN;

And now, I realize I can not feed 3 bits in one time! 现在,我意识到我不能一次喂3个位! doh! 卫生署! All this seems very weak, and certainly a too long process for just 4 values. 所有这一切似乎都很薄弱,对于仅4个值来说肯定是一个过长的过程。 (I wanted to keep this kind of struct table for more instances). (我想为更多实例保留这种结构表)。

Please, could you help me to find a simpler way to feed my table? 拜托,您能帮我找到一种更简单的方法来喂养我的桌子吗?

According to the Arduino reference I just looked up Serial::read , the code returns data byte-by-byte (eight bits at a time). 根据Arduino参考,我只是查找Serial :: read ,代码逐字节返回数据(一次返回8位)。 So probably you should just read the data one byte (eight bits at a time) and do your unpacking after the fact. 因此,可能您应该只读取一个字节(一次八位)的数据,然后在事后解压缩。

In fact you might want to use a union (see eg this other stackoverflow post on how to use a union ) so that you can get the best of both worlds. 实际上,您可能想要使用一个union (例如,有关如何使用union 信息 ,请参见其他stackoverflow文章 ),以便您可以充分利用这两个方面。 Specifically, if you define a union of your definition with the bits broken out and a second part of the union as one or two bytes, you can send the data as bytes and then decode it in the bits you are interested in. 特别是,如果你定义一个union与破出位和第二部分你定义的union为一个或两个字节,您可以发送数据字节,然后在你感兴趣的比特进行解码。

UPDATE UPDATE

Here is an attempt at some more details. 这是一些更多细节的尝试。 There are a lot of caveats about unions - they aren't portable, they are compiler dependent, etc. But this might be worth trying. 关于联合有很多注意事项-它们不是可移植的,它们是与编译器相关的,等等。但这可能值得尝试。

typedef struct {
  unsigned int page:1; // (0,1)
  unsigned int cric:4; // 10 choices (4 bits)
  unsigned int crac:3; // 5 choices (3 bits)
  unsigned int croc:2; // 3 choices (2 bits)
} structTable;

typedef union {
  structTable a;
  uint16_t b;
} u_structTable;

serial.Read(val1);
serial.Read(val2);

u_structTable x;
x.b = val1 | (val2<<8);
printf("page is %d\n", x.a.page);

You can only send full bytes over the serial port. 您只能通过串行端口发送完整的字节。 But you can also send raw data directly. 但是您也可以直接发送原始数据。

void send (const structTable* table)
{
    Serial.write((const char*)table, sizeof(structTable));  // 2 bytes.
}

bool receive(structTable* table)
{
    return (Serial.readBytes((char*)table, sizeof(structTable)) == sizeof(structTable));
}

You also have to be aware that sizeof(int) is not the same on all CPUS 您还必须注意,在所有CPUS上,sizeof(int)都不相同

A word about endianness. 关于字节序的一句话。 The definition for your struct for the program at the other end of the serial link, if running on a CPU with a different endianness would become: 如果在具有不同字节序的CPU上运行,则串行链接另一端的程序结构定义将变为:

struct structTable {
  unsigned short int croc:2; // 3 choices (2 bits)
  unsigned short int crac:3; // 5 choices (3 bits)
  unsigned short int cric:4; // 10 choices (4 bits)
  unsigned short int page:1; // (0,1)
};

Note the use of short int, which you can also use in the arduino code to be more precise. 注意short int的使用,您也可以在arduino代码中使用它,以便更精确。 The reason is that short int is 16 bits on most CPUs, while int may be 16,32 or even 64 bits. 原因是在大多数CPU上,short int是16位,而int可能是16,32甚至64位。

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

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