将序列化结构从8位系统复制到32位系统

时间:2016-12-06 22:26:15

标签: c struct microcontroller 32-bit 8-bit

我的电路板上有一个32位ARM CPU作为主处理器,一个8位AVR微控制器作为与之相连的从属设备。

我正在复制一个结构(从属设备将其转换为字节流,主设备接收并将其存储在具有相同定义的结构的内存位置)。

主服务器和从服务器都在自己的控制台上打印结构成员的值。但是值不匹配。

我已经发现问题正在发生,因为主服务器中结构的实际内存分配稍微改变以与16位边界对齐。即,小于16位的数据类型升级到16位。

结构中有一些8位整数(u8)实际上在主存储器的内存中分配了2个字节,而在从属中,它们按预期分配了1个字节。

不出所料,sizeof(STRUCT_NAME)会在每个系统中产生不同的结果。

我已经找到了解决方法,在必要时在输入数据中插入填充字节。

在这种情况下是否有任何标准方法来处理结构序列化?

我使用GCC作为ARM部件。

2 个答案:

答案 0 :(得分:2)

  <?php 
require("common.php"); 
if(empty($_SESSION['Id'])) 
{ 
    header("Location: login.php"); 
    die("Redirecting to login.php"); 
} 
else
   { 
     include 'top.php'; 

 if (!empty($_POST['headline'])){ 

     $type =  $_POST['type'];
     $sector =  $_POST['sector'];
     $target = $_POST['target_sector'];
     $headline = $_POST['headline'];
     $desc = $_POST['desc'];
     $fb = $_POST['fb'];
     $link = $_POST['website_link'];
     $twitter = $_POST['twitter'];



    echo "$type"; 
    echo "$headline"; 
    echo "<h2>$headline</h2>"; 


 ?><?php
 } 
else 
{ 
?> 

           <div class="container">
<br><br><br><br><br>

    <form name="registration" action="<?php echo $_SERVER['REQUEST_URI'];?>"        method="POST"/>
  <div class="form-group">
      <input id="logo_name" class="form-control" type="file" name="logo_name">
  </div>

    <div class="form-group">
        <select id="type" name="type" class="form-control">
          <option>B2B</option>
          <option>B2C</option>
        </select>
    </div>

    <div class="form-group">
        <select id="sector" name="sector" class="form-control">
          <option>B2B</option>
          <option>B2C</option>
        </select>
    </div>

    <div class="form-group">
        <select id="target_sector" name="target_sector" class="form-control">
          <option>B2B</option>
          <option>B2C</option>
        </select>
     </div>

      <div class="form-group">
        <input class="form-control" type="text" id="headline" name="headline" placeholder="Advertisement Title">
     </div>

     <div class="form-group">
        <textarea class="form-control" type="text" rows="5" id="desc" name="desc" placeholder="Advertisment Text"></textarea>
     </div>

     <div class="form-group">
        <input class="form-control" type="text" id="website_link" name="website_link" placeholder="Website Link">
     </div>

     <div class="form-group">
        <input class="form-control" type="text" id="fb" name="fb" placeholder="Facbook URL">
     </div>

     <div class="form-group">
        <input class="form-control" type="text" id="twitter" name="twitter" placeholder="Twitter URL">
     </div>

      <div class="form-control">
        <input type="submit"/>
      </div>
</form>

对齐到1个字节。

答案 1 :(得分:0)

明确定义数据在传输过程中的表示方式,然后明确地对字节流进行转换编程。

您可以使用shift和mask操作来完成此操作。通过一些常用数字类型的辅助函数,这是非常易于管理的。

通过这种方式,您可以避免不同的字节顺序和结构打包引起的所有可移植性问题。

相关问题