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
|
pLink->SetCommStatus(CMST_RX_CNT);
|
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_RX_CNT);
|
109
|
PrintLog(LOG_VIOLATION, " ???ݳ?ʱ\n");
|
110
|
m_LastSendTime = GetNowSecond();
|
111
|
return -1;
|
112
|
}
|
113
|
|
114
|
uint8 buf[1024];
|
115
|
uint32 datanum =0;
|
116
|
char stmp[90] = { 0 }; int i = 0;
|
117
|
// if(0<buflen)PrintLog(LOG_INFORMATION,"ȡ?????ݳ???%d",buflen);
|
118
|
while (buflen >= 21)
|
119
|
{
|
120
|
if(buflen>1024)buflen=1024;
|
121
|
pRxBuf->Read(buf, buflen, DEF_BUFF_NOMOVE );
|
122
|
|
123
|
|
124
|
if((buf[0]==0xEB)&&(buf[1]==0x90)&&buf[7]==66)//????????train
|
125
|
{
|
126
|
S_VstData vst;
|
127
|
|
128
|
int datalen =(buf[4]<<8) + buf[5];
|
129
|
//????????????????
|
130
|
PrintLog(LOG_INFORMATION,"ȡ???г????ݳ???%d",datalen);
|
131
|
|
132
|
vst.DataType = DEF_VST_TRAIN;
|
133
|
vst.BuffLen = buf[8];
|
134
|
buf[9] %= 100;
|
135
|
for(i=0; i<vst.BuffLen; i++)
|
136
|
{
|
137
|
vst.VstBuf[i] = buf[9+i];
|
138
|
}
|
139
|
|
140
|
PRawCtrl->PutAVstData(vst);
|
141
|
m_ResponseFlag = 1;
|
142
|
//PrintLog(LOG_INFORMATION,"?յ??г?????");
|
143
|
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]);
|
144
|
// 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]);
|
145
|
pLink->RegisterFrm(FRAME_RX_SUC);
|
146
|
pRxBuf->Move(datalen+6);
|
147
|
buflen = pRxBuf->GetReadableSize();
|
148
|
SendSomething();
|
149
|
}
|
150
|
else{
|
151
|
memset(stmp,0,90);
|
152
|
if (buflen > 30)buflen = 30;
|
153
|
for (i = 0; i < buflen; i++)
|
154
|
{
|
155
|
sprintf(stmp + 3 * i, "%02x ", buf[i]);
|
156
|
}
|
157
|
PrintLog(LOG_INFORMATION, "?յ????г?????,?Ƴ????ݳ???%d??%s", stmp);
|
158
|
pRxBuf->Move(1);
|
159
|
}
|
160
|
|
161
|
}
|
162
|
|
163
|
return 1;
|
164
|
}
|
165
|
|
166
|
void CModbus_TrainC::SendSomething()
|
167
|
{
|
168
|
int bytelen = 0;
|
169
|
|
170
|
sint32 rtuno = pLink->GetRtuNo();
|
171
|
unsigned char rtuadd = pRtu->GetRtuAddr(rtuno);
|
172
|
|
173
|
if(cnt_sequence>=65535)
|
174
|
cnt_sequence=0;
|
175
|
else
|
176
|
cnt_sequence++;
|
177
|
unsigned char buf[1024];
|
178
|
int buflen =0;
|
179
|
buf[buflen]=cnt_sequence/0x100;buflen++;//H Transaction Identifier
|
180
|
buf[buflen]=cnt_sequence%0x100; buflen++;//L
|
181
|
buf[buflen]=0; buflen++;
|
182
|
buf[buflen]=0; buflen++;
|
183
|
//buf[buflen]=HIBYTE(bytelen);
|
184
|
buflen++;
|
185
|
//buf[buflen]=LOBYTE(bytelen);
|
186
|
buflen++;
|
187
|
buf[buflen]=rtuadd; buflen++;
|
188
|
buf[buflen]=0; buflen++;//fun
|
189
|
|
190
|
buf[buflen] = 2; buflen++;bytelen++;
|
191
|
//??㷢?㶫??
|
192
|
buf[buflen]=cnt_sequence/0x100;buflen++;//H Transaction Identifier
|
193
|
buf[buflen]=cnt_sequence%0x100; buflen++;//L
|
194
|
|
195
|
buf[4]=HIBYTE(bytelen);
|
196
|
buf[5]=LOBYTE(bytelen);
|
197
|
|
198
|
// pLink->RegisterFrmCode(RAW_CODEDIR_DOWN,(char *)buf,buflen);
|
199
|
pTxBuf->Write(buf,buflen);
|
200
|
m_LastSendTime=GetNowSecond();
|
201
|
m_ResponseFlag = 0;
|
202
|
|
203
|
// pLink->SetCommStatus(CMST_NORMAL);
|
204
|
pLink->SetCommStatus(CMST_RX_CNT);
|
205
|
}
|
206
|
|
207
|
sint32 CModbus_TrainC::GetNowSecond()
|
208
|
{
|
209
|
CSeTime SE_T;
|
210
|
TCriterionTime tmptime;//sint32
|
211
|
SE_T.GetNow(&tmptime);
|
212
|
return (sint32)tmptime;
|
213
|
}
|
214
|
|
215
|
//??Լ????????
|
216
|
#ifdef __unix
|
217
|
extern "C" CProtocol* CreateProtocol(char *defpara)
|
218
|
#else
|
219
|
extern "C" __declspec(dllexport) CProtocol* CreateProtocol(char *defpara)
|
220
|
#endif
|
221
|
{
|
222
|
CProtocol *pEpv = new CModbus_TrainC;
|
223
|
return pEpv;
|
224
|
}
|