1
|
// Modbus_C.cpp: implementation of the CModbus_TrainC class.
|
2
|
//
|
3
|
//////////////////////////////////////////////////////////////////////
|
4
|
|
5
|
#include "stdafx.h"
|
6
|
#include "Modbus_TRAINc.h"
|
7
|
|
8
|
#ifdef _DEBUG
|
9
|
#undef THIS_FILE
|
10
|
static char THIS_FILE[]=__FILE__;
|
11
|
#define new DEBUG_NEW
|
12
|
#endif
|
13
|
|
14
|
//////////////////////////////////////////////////////////////////////
|
15
|
// Construction/Destruction
|
16
|
//////////////////////////////////////////////////////////////////////
|
17
|
CModbus_TrainC::~CModbus_TrainC()
|
18
|
{
|
19
|
}
|
20
|
|
21
|
void CModbus_TrainC::Init(S_PROTOCOLCFG * pcfg )
|
22
|
{
|
23
|
PRawCtrl = pcfg->PRawCtrl;
|
24
|
pRxBuf = pcfg->pRxBuf;
|
25
|
pTxBuf = pcfg->pTxBuf;
|
26
|
pCmdMem = pcfg->pCmdMem;
|
27
|
pRtu = pcfg->pRtu;
|
28
|
pLink = pcfg->pLink;
|
29
|
pTable = pcfg->pTable;
|
30
|
pZfDataCtrl = pcfg->pZfDataCtrl;
|
31
|
// pHis = pcfg->pHis;
|
32
|
cnt_sequence=0;
|
33
|
m_LastSendTime = GetNowSecond();
|
34
|
|
35
|
//??ʼ????ǰͨ??????
|
36
|
m_current_com_status = DEF_MSG_COMMDOWN ; //DEF_MSG_COMMUP??????????DEF_MSG_COMMDOWN??????????
|
37
|
|
38
|
|
39
|
m_ResponseFlag = 0;
|
40
|
|
41
|
m_initFlag = 1;
|
42
|
|
43
|
}
|
44
|
|
45
|
//??Լ??????
|
46
|
sint32 CModbus_TrainC::TxProc()
|
47
|
{
|
48
|
/* if(m_ResponseFlag == 1)
|
49
|
{
|
50
|
|
51
|
}
|
52
|
*/
|
53
|
return 1;
|
54
|
}
|
55
|
|
56
|
void CModbus_TrainC::initTrain()
|
57
|
{
|
58
|
S_VstData vst;
|
59
|
vst.DataType = DEF_VST_TRAIN;
|
60
|
vst.BuffLen = 12;
|
61
|
//memset(vst.VstBuf,0,12);
|
62
|
int i=0;
|
63
|
for(i=0; i<12; i++)
|
64
|
{
|
65
|
vst.VstBuf[i] = 0;
|
66
|
}
|
67
|
for(i=1;i<100;i++)
|
68
|
{
|
69
|
vst.VstBuf[0] = i;
|
70
|
PRawCtrl->PutAVstData(vst);
|
71
|
PrintLog(LOG_INFORMATION,"??ʼ????%d???г?????",i);
|
72
|
}
|
73
|
m_initFlag = 0;
|
74
|
}
|
75
|
sint32 CModbus_TrainC::MsgProc(sint32 msg)
|
76
|
{
|
77
|
if(msg == DEF_MSG_COMMDOWN)
|
78
|
{
|
79
|
m_current_com_status = DEF_MSG_COMMDOWN;
|
80
|
|
81
|
}
|
82
|
return 1;
|
83
|
|
84
|
}
|
85
|
|
86
|
//??Լ???պ???
|
87
|
sint32 CModbus_TrainC::RxProc()
|
88
|
{
|
89
|
|
90
|
if((m_initFlag == 1)||(m_current_com_status == DEF_MSG_COMMDOWN))
|
91
|
{
|
92
|
initTrain();
|
93
|
m_current_com_status = DEF_MSG_COMMUP;
|
94
|
}
|
95
|
|
96
|
sint32 rtuno = pLink->GetRtuNo();
|
97
|
unsigned char rtuaddr = pRtu->GetRtuAddr(rtuno);
|
98
|
|
99
|
int nowtime=GetNowSecond();
|
100
|
uint32 buflen = pRxBuf->GetReadableSize();
|
101
|
|
102
|
E_RAW_ComStat commstatus = pLink->GetCommStatus();
|
103
|
if ( commstatus==CMST_RX_CNT && (nowtime-m_LastSendTime) >= 20)
|
104
|
{
|
105
|
//??ʱ???ط???ֱ????????һ???ն?
|
106
|
pRxBuf->Move(buflen);
|
107
|
pLink->RegisterFrm(FRAME_RX_TIMEOUT);
|
108
|
pLink->SetCommStatus(CMST_TX_CNT);
|
109
|
PrintLog(LOG_VIOLATION, " ???ݳ?ʱ\n");
|
110
|
return -1;
|
111
|
}
|
112
|
|
113
|
uint8 buf[1024];
|
114
|
uint32 datanum =0;
|
115
|
// if(0<buflen)PrintLog(LOG_INFORMATION,"ȡ?????ݳ???%d",buflen);
|
116
|
while (buflen >= 21)
|
117
|
{
|
118
|
if(buflen>1024)buflen=1024;
|
119
|
pRxBuf->Read(buf, buflen, DEF_BUFF_NOMOVE );
|
120
|
|
121
|
|
122
|
if((buf[0]==0xEB)&&(buf[1]==0x90)&&buf[7]==66)//????????train
|
123
|
{
|
124
|
S_VstData vst;
|
125
|
|
126
|
int datalen =(buf[4]<<8) + buf[5];
|
127
|
//????????????????
|
128
|
PrintLog(LOG_INFORMATION,"ȡ???г????ݳ???%d",datalen);
|
129
|
|
130
|
vst.DataType = DEF_VST_TRAIN;
|
131
|
vst.BuffLen = buf[8];
|
132
|
for(int i=0; i<vst.BuffLen; i++)
|
133
|
{
|
134
|
vst.VstBuf[i] = buf[9+i];
|
135
|
}
|
136
|
|
137
|
PRawCtrl->PutAVstData(vst);
|
138
|
m_ResponseFlag = 1;
|
139
|
//PrintLog(LOG_INFORMATION,"?յ??г?????");
|
140
|
PrintLog(LOG_INFORMATION,"?յ??г????????к?%d:?г???:%d,????:%d,???ߺ?:%d,ƫ????:%dCM,?յ?վ??:%d,??????%d,????%d",(buf[2]*100+buf[3]),buf[9],buf[10],buf[11]+(buf[12]<<8),(buf[16]<<24)+(buf[15]<<16)+(buf[14]<<8)+buf[13],buf[17],(buf[20]<<8)+buf[19],buf[9+9]);
|
141
|
// PrintLog(LOG_INFORMATION,"?յ??г????????к?%d:?г???:%d,????:%d,???ߺ?:%d,ƫ????:%dCM,?յ?վ??:%d,??????%d,????%d",(buf[2]*100+buf[3]),buf[9],buf[10],buf[11],(buf[14]<<8)+buf[13],buf[17-2],(buf[20-2]<<8)+buf[19-2],buf[9+9-2]);
|
142
|
|
143
|
pRxBuf->Move(datalen+6);
|
144
|
buflen = pRxBuf->GetReadableSize();
|
145
|
SendSomething();
|
146
|
}
|
147
|
else{
|
148
|
PrintLog(LOG_INFORMATION,"?յ????г?????,?Ƴ????ݳ???%d",buflen);
|
149
|
pRxBuf->Move(1);
|
150
|
}
|
151
|
|
152
|
}
|
153
|
|
154
|
return 1;
|
155
|
}
|
156
|
|
157
|
void CModbus_TrainC::SendSomething()
|
158
|
{
|
159
|
int bytelen = 0;
|
160
|
|
161
|
sint32 rtuno = pLink->GetRtuNo();
|
162
|
unsigned char rtuadd = pRtu->GetRtuAddr(rtuno);
|
163
|
|
164
|
if(cnt_sequence>=65535)
|
165
|
cnt_sequence=0;
|
166
|
else
|
167
|
cnt_sequence++;
|
168
|
unsigned char buf[1024];
|
169
|
int buflen =0;
|
170
|
buf[buflen]=cnt_sequence/0x100;buflen++;//H Transaction Identifier
|
171
|
buf[buflen]=cnt_sequence%0x100; buflen++;//L
|
172
|
buf[buflen]=0; buflen++;
|
173
|
buf[buflen]=0; buflen++;
|
174
|
//buf[buflen]=HIBYTE(bytelen);
|
175
|
buflen++;
|
176
|
//buf[buflen]=LOBYTE(bytelen);
|
177
|
buflen++;
|
178
|
buf[buflen]=rtuadd; buflen++;
|
179
|
buf[buflen]=0; buflen++;//fun
|
180
|
|
181
|
buf[buflen] = 2; buflen++;bytelen++;
|
182
|
//??㷢?㶫??
|
183
|
buf[buflen]=cnt_sequence/0x100;buflen++;//H Transaction Identifier
|
184
|
buf[buflen]=cnt_sequence%0x100; buflen++;//L
|
185
|
|
186
|
buf[4]=HIBYTE(bytelen);
|
187
|
buf[5]=LOBYTE(bytelen);
|
188
|
|
189
|
// pLink->RegisterFrmCode(RAW_CODEDIR_DOWN,(char *)buf,buflen);
|
190
|
pTxBuf->Write(buf,buflen);
|
191
|
m_LastSendTime=GetNowSecond();
|
192
|
m_ResponseFlag = 0;
|
193
|
|
194
|
// pLink->SetCommStatus(CMST_NORMAL);
|
195
|
pLink->SetCommStatus(CMST_RX_CNT);
|
196
|
}
|
197
|
|
198
|
sint32 CModbus_TrainC::GetNowSecond()
|
199
|
{
|
200
|
CSeTime SE_T;
|
201
|
TCriterionTime tmptime;//sint32
|
202
|
SE_T.GetNow(&tmptime);
|
203
|
return (sint32)tmptime;
|
204
|
}
|
205
|
|
206
|
//??Լ????????
|
207
|
#ifdef __unix
|
208
|
extern "C" CProtocol* CreateProtocol(char *defpara)
|
209
|
#else
|
210
|
extern "C" __declspec(dllexport) CProtocol* CreateProtocol(char *defpara)
|
211
|
#endif
|
212
|
{
|
213
|
CProtocol *pEpv = new CModbus_TrainC;
|
214
|
return pEpv;
|
215
|
}
|