【问题标题】:How to map termios bytes to struct?如何将 termios 字节映射到结构?
【发布时间】:2017-12-24 08:58:53
【问题描述】:

我想将 Libc 函数 tcgetattr 返回的 termios 字节映射到 C# 中的类。

在 C 中 termios 定义为:

#define NCCS    12

typedef unsigned cc_t;
typedef unsigned speed_t;
typedef unsigned tcflag_t;

struct termios {
  cc_t      c_cc[NCCS];
  tcflag_t  c_cflag;
  tcflag_t  c_iflag;
  tcflag_t  c_lflag;
  tcflag_t  c_oflag;
  speed_t   c_ispeed;
  speed_t   c_ospeed;
};

以下是串行端口的 termios 字节。它们之间的唯一区别是 B9600 与 B38400 使用 Libc cfsetspeed 设置的波特率(平台是运行 Raspbian Stretch 的 Raspberry PI):

Byte#   B38400  B9600
0       0   0
1       5   5
2       0   0
3       0   0
4       5   5
5       0   0
6       0   0
7       0   0
8       191 189
9       12  12
10      0   0
11      0   0
12      59  59
13      138 138
14      0   0
15      0   0
16      0   0
17      3   3
18      28  28
19      127 127
20      21  21
21      4   4
22      0   0
23      0   1
24      0   0
25      17  17
26      19  19
27      26  26
28      0   0
29      18  18
30      15  15
31      23  23
32      22  22

B9600 和 B38400 之间的唯一区别是索引 = 8 的字节和索引 = 8 的字节的位模式是有意义的,因为 B9600 = 0xd 和 B38400 = 0xf。 189 的最后一个字节是 0xd,而 191 os 的最后一个字节是 0xf,所以看起来是正确的。但是,我不知道如何理解如何将字节映射到 C 结构。当速度改变时,c_ispeed 和 c_ospeed 的字节不应该改变吗?

谁能解释如何将字节索引映射到 C 结构?

【问题讨论】:

    标签: c# serial-port termios


    【解决方案1】:

    试试下面的代码。由于速度是第三个整数,因此字节的顺序看起来不对:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
             public struct Termios {
    
              public uint c_cc1 { get; set; }
              public uint c_cc2 { get; set; }
              public uint c_cflag { get; set; }
              public uint c_iflag { get; set; }
              public uint  c_lflag  { get; set; }
              public uint c_oflag { get; set; }
              public uint c_ispeed { get; set; }
              public uint c_ospeed { get; set; }
            };
            static void Main(string[] args)
            {
                //Byte#   B38400  B9600
                byte[,] input = {
                                     {0,5, 0,0,5,0,0,0,191, 12,0,0,59,138, 0,0,0,3,127,21,4,0,0,0,17,19,26,0,18,15, 23,22},
                                     {0,5, 0,0,5,0,0,0,189, 12,0,0,59,138, 0,0,0,3,127,21,4,0,1,0,17,19,26,0,18,15, 23,22}
                                 };
    
                Termios[] termios = new Termios[2];
    
                for(int i = 0; i < 2; i++)
                {
                    termios[i].c_cc1 = BitConverter.ToUInt32(input.Cast<byte>().Skip(i * 32).ToArray(),0);
                    termios[i].c_cc2 = BitConverter.ToUInt32(input.Cast<byte>().Skip(i * 32).ToArray(), 4);
                    termios[i].c_cflag = BitConverter.ToUInt32(input.Cast<byte>().Skip(i * 32).ToArray(), 8);
                    termios[i].c_iflag = BitConverter.ToUInt32(input.Cast<byte>().Skip(i * 32).ToArray(), 12);
                    termios[i].c_lflag = BitConverter.ToUInt32(input.Cast<byte>().Skip(i * 32).ToArray(), 16);
                    termios[i].c_oflag = BitConverter.ToUInt32(input.Cast<byte>().Skip(i * 32).ToArray(), 20);
                    termios[i].c_ispeed = BitConverter.ToUInt32(input.Cast<byte>().Skip(i * 32).ToArray(), 24);
                    termios[i].c_ospeed = BitConverter.ToUInt32(input.Cast<byte>().Skip(i * 32).ToArray(), 28);
                }
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 2020-01-30
      • 1970-01-01
      • 1970-01-01
      • 2018-11-09
      • 2021-02-11
      • 2016-12-27
      • 2020-08-15
      • 1970-01-01
      相关资源
      最近更新 更多