2006.1.7 :底层C编写的常用 用法
/* The following macros process structure members: to determine the */
/* offset of a structure member from the beginning of the structure, and */
/* to detemine the size of a member of a structure. */
#define DEFS_OFFSET(s,m) (DEFS_UI16)&(((s *)0)->m)
#define DEFS_MEMBER_SIZE(s,m) sizeof(((s *)0)->m)
/* The following macros make it easier to specify binary numbers. For */
/* example, to specify a byte in binary: DEFS_BINARY(01100001) or to */
/* specify a word in binary: DEFS_BINARY_WORD(00111101,11001010) */
#define DEFS_BINARY_CONVERT(byte) (((byte & 0x10000000L) / 0x200000L) + \
((byte & 0x01000000L) / 0x040000L) + \
((byte & 0x00100000L) / 0x008000L) + \
((byte & 0x00010000L) / 0x001000L) + \
((byte & 0x00001000L) / 0x000200L) + \
((byte & 0x00000100L) / 0x000040L) + \
((byte & 0x00000010L) / 0x000008L) + \
((byte & 0x00000001L) / 0x000001L))
#define DEFS_BINARY(byte) DEFS_BINARY_CONVERT(0x##byte)
#define DEFS_BINARY_WORD(high,low) (DEFS_BINARY_CONVERT(0x##high) * 256) + \
2006.1.7 字节交换顺序
/* This is a set of routines for demonstrating the effects of
byte ordering between little endian and big endian machines
Big Endian (B_ENDIAN == TRUE):
MSB [ n | n+1 | n+2 | n+3 ] LSB
Little Endian (B_ENDIAN == FALSE):
MSB [ n+3 | n+2 | n+1 | n] LSB
little/big-endian is bitness as well. For example,
struct {
short b:5, --- we _want_ signed for the example ---
c:11;
};
is stored in little-endian (gint32el) ['+' denotes sign bit]:
tf_Bitstream view -->
0123456789A BCDEF
|-----c----+|--b-+|
Whereas bigendian (Motorola) stores it thus:
tf_Bitstream view -->
FEDCB A9876543210
|+-b--|+----c-----|
So, you have to invert bits as well to convert endianness.
#define Gettf_Bit(Val,Idx) ( ( (Val) & (1<<(Idx)) ) )
#define Settf_Bit(Val,Idx,tf_Bit) ( ( (Val) & (~(1<<Idx)) ) | ((tf_Bit)<<(Idx)) )
for ( i = 0; i < sizeof(Val)*8; i++)
NewVal = Settf_Bit(NewVal,sizeof(Val)*8-i-1, Gettf_Bit(Val,i));
*/
DNP
DnpMasterStart
--> taskSpawn ("tDnpMaster", priority, VX_FP_TASK, 0x8000,
(FUNCPTR) DnpMasterTask,
(int) ini_name, 0, 0, 0, 0, 0, 0, 0, 0, 0)
dnpMasterTask ---> dnp_init()
---> ca_task_initialize()
---> ca_add_fd_registration()
---> 从配置文件中得到dnp的配置信息
---> pdpdvrs_initDNP()
---> addr_init()
---> dnp_loop()
DnpMasterAddDevice
DnpMasterAdd8550
DnpMasterAddText
DNP PEER TO PEER
一些debug信息
物理层
.利用查表法来确定链路层的长度
--->. pdpphys_initPhysRecMgr 物理层初始化,分配内存
. valid_address()
. pdpphys_parseDNPframe()
DnpMasterTask
========================================================
串口:从温旧梦
非重叠模式优点 .直接
.易移植
缺点 .线程中读、写相互堵塞
.线程间相互堵塞(包括读之间,写之间,读写之间)
单片机,嵌入式如vxworks,任务中相对简单的程序中常用
重叠模式 优点 .灵活,效率高(后台执行操作)
.多线程间,线程中,读写操作不会相互堵塞
缺点 .不那么直接
.不易于移植
EV_RXCHAR:事件通知模式用在非重叠模是一个比较好的方式,因为程序不用轮询串口,以节省CPU的thread quantum
方式如下:
DWORD dwCommEvent;
DWORD dwRead;
char chRead;
if (!SetCommMask(hComm, EV_RXCHAR))
// Error setting communications event mask.
for ( ; ; ) {
if (WaitCommEvent(hComm, &dwCommEvent, NULL)) {
if (ReadFile(hComm, &chRead, 1, &dwRead, NULL))
// A byte has been read; process it.
else
// An error occurred in the ReadFile call.
break;
}
else
// Error in WaitCommEvent.
break;
}
问题出现在:当多个字节连续的发送,第1个byte到,引起EV_RXCHAR 事件发生,
WaitCommEvent指示可读.第2个byte紧接着到,EV_RXCHAR只是在内部置位,
当第1个byte读完,调用WaitCommEvent时,系统再次指示EV_RXCHAR 事件发生,
可读第2个byte,问题出现在当第2个字符到达,而没有被读出时,第3个字符到
达了,此时系统也期望将对应于第3个字符的EV_RXCHAR置位,但是系统中的
EV_RXCHAR标志位已经被第2个已经置位了,于是就被忽略,当第2个自负读出
后,只能在系统缓冲区,等待第4个字符到,而读出第3个字符,这样代码和系统
就失去同步。
(我觉得这种情况在一般情况下不会出现,在单片机、
嵌入式应该是一个一个字符往外蹦,)
而且WaitCommEvent会导致很多其它事件丢失,在高速情况下。
解决办法是:利用ClearCommError判断缓冲中的字符个数,一次性读完所有到达字符