/* 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 字節(jié)交換順序
/* 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
========================================================
串口:從溫舊夢
非重疊模式優(yōu)點 .直接
.易移植
缺點 .線程中讀、寫相互堵塞
.線程間相互堵塞(包括讀之間,寫之間,讀寫之間)
單片機,嵌入式如vxworks,任務中相對簡單的程序中常用
重疊模式 優(yōu)點 .靈活,效率高(后臺執(zhí)行操作)
.多線程間,線程中,讀寫操作不會相互堵塞
缺點 .不那么直接
.不易于移植
EV_RXCHAR:事件通知模式用在非重疊模是一個比較好的方式,因為程序不用輪詢串口,以節(jié)省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;
}
問題出現在:當多個字節(jié)連續(xù)的發(fā)送,第1個byte到,引起EV_RXCHAR 事件發(fā)生,
WaitCommEvent指示可讀.第2個byte緊接著到,EV_RXCHAR只是在內部置位,
當第1個byte讀完,調用WaitCommEvent時,系統(tǒng)再次指示EV_RXCHAR 事件發(fā)生,
可讀第2個byte,問題出現在當第2個字符到達,而沒有被讀出時,第3個字符到
達了,此時系統(tǒng)也期望將對應于第3個字符的EV_RXCHAR置位,但是系統(tǒng)中的
EV_RXCHAR標志位已經被第2個已經置位了,于是就被忽略,當第2個自負讀出
后,只能在系統(tǒng)緩沖區(qū),等待第4個字符到,而讀出第3個字符,這樣代碼和系統(tǒng)
就失去同步。
(我覺得這種情況在一般情況下不會出現,在單片機、
嵌入式應該是一個一個字符往外蹦,)
而且WaitCommEvent會導致很多其它事件丟失,在高速情況下。
解決辦法是:利用ClearCommError判斷緩沖中的字符個數,一次性讀完所有到達字符