2017-12-24 5 views
0

Libc関数tcgetattrによって返されたtermiosバイトをC#のクラスにマップしたいと思います。 C用のtermiosで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バイトです。それらの間の唯一の違いは、のlibc cfsetspeed持つB38400セット(プラットフォームはラズベリーPIはRaspbianストレッチを実行している)対ボーレートB9600です:

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、 B9600 = 0xdおよびB38400 = 0xfであるため、インデックス= 8のバイトのビットパターンは意味をなさない。 189の最後のバイトは0xdで、191の最後のバイトは0xfなので、正しく見えます。しかし、私はどのようにCの構造体にバイトをマップするのかを理解する方法を正確にはわかりません。速度が変更された場合、c_ispeedとc_ospeedのバイト数は変更されませんか?

どのようにバイトインデックスをC構造体にマップするか説明できますか?

答えて

-1

下記のコードを試してください。速度が3番目の整数であるため、バイトの順序は正しくありません。

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); 
      } 

     } 
    } 
} 
関連する問題