我主要使用 National Instruments 的 Labwindows CVI 作为我的编译器。
我必须为设备创建一个接口(interface),并开始为所有不同的寄存器查找结构。
// Interface.h
typedef enum Color{
White,
Black,
Blue
};
typedef struct Register1{
int Bit1:1;
int Bit2:2;
Color col;
} Register1;
// Interface.c
BuildSendMessage(Register1 temp)
{
unsigned int iTemp;
iTemp = temp.Bit1 << 7 + temp.Bit2 << 5 + temp.col;
}
// sample.c
Register1 reg1;
reg1.Bit1 = 0;
reg1.Bit2 = 1;
reg1.col = White;
// Pass to function where message is built up
BuildSendMessage(reg1);
然后,我在调用函数中填充该结构,然后将该结构传递到一个函数中,在该函数中我执行所有位移、将地址等附加到消息上,并通过通信接口(interface)将其发送出去。
这可以改进吗?我应该隐藏更多信息并让函数填充结构吗?我已经做了很多阅读,这肯定需要一些改进。
最佳答案
What I am trying to do is I have a device that I talk to over tcp it has a document explaining what each register does.
…
I then populate the struct in the calling function and then pass the struct into a function where I do all the bit shifting, append the address, etc. onto the message and ship it out over the comm interface.
Can this be improved and should I do more information hiding and have a function populate the struct?
来自Wikipedia :
In computer science, information hiding is the principle of segregation of the design decisions in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decision is changed. The protection involves providing a stable interface which protects the remainder of the program from the implementation (the details that are most likely to change).
设备中每个寄存器的功能不太可能改变,如果它最终应该改变,程序的其他部分可能无论如何都需要修改,所以做“更多信息隐藏”是没有意义的。
关于c - Ansi-C C99 接口(interface)与结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33975922/