Project

General

Profile

Feature #3320 » ModbusTcpc17pis.cpp

yan hong, 01/22/2024 02:40 PM

 
1
// CModbusTcpc17pis.cpp: implementation of the CModbusTcpc17pis class.
2
//
3
//////////////////////////////////////////////////////////////////////
4
//#ifdef WIN32
5
//#include "stdafx.h"
6
//#endif
7
#ifndef WIN32
8
#include <iconv.h>
9
#endif
10

    
11
#include "ModbusTcpc17pis.h"
12

    
13
#ifdef _DEBUG
14
#undef THIS_FILE
15
static char THIS_FILE[] = __FILE__;
16
#define new DEBUG_NEW
17
#endif
18

    
19
#include "thirdparty/json.h"
20
//////////////////////////////////////////////////////////////////////
21
// Construction/Destruction
22
//////////////////////////////////////////////////////////////////////
23

    
24
CModbusTcpc17pis::CModbusTcpc17pis() {
25
	m_nStationNo = 10;
26
	m_nNo = 0;
27
}
28

    
29
CModbusTcpc17pis::~CModbusTcpc17pis() {
30

    
31
}
32

    
33
void CModbusTcpc17pis::Init(S_PROTOCOLCFG *pcfg) {
34
	PRawCtrl = pcfg->PRawCtrl;
35
	pRxBuf = pcfg->pRxBuf;
36
	pTxBuf = pcfg->pTxBuf;
37
	pCmdMem = pcfg->pCmdMem;
38
	pRtu = pcfg->pRtu;
39
	pLink = pcfg->pLink;
40
	pTable = pcfg->pTable;
41
	pZfDataCtrl = pcfg->pZfDataCtrl;
42
	//	pHis		=	pcfg->pHis;
43
	pLink->SetCommStatus(CMST_TX_CNT);
44
	m_nLastSendTime = GetNowSecond();
45
	m_nRcvTimeOuts = pLink->GetRxTimeouts();
46

    
47
	if (m_nRcvTimeOuts <= 0) {
48
		m_nRcvTimeOuts = 20;
49
	}
50
	pRxBuf->Move(pRxBuf->GetReadableSize());
51
	pTxBuf->Move(pTxBuf->GetReadableSize());
52

    
53
	m_nLastSendTime = 0;
54
}
55

    
56
sint32 CModbusTcpc17pis::RxProc() {
57
	sint32 rtuno = pLink->GetRtuNo();
58
	unsigned char rtuaddr = pRtu->GetRtuAddr(rtuno);
59

    
60
	uint8  buf[1024];
61
	int datalen = 0;
62

    
63
	int buflen = pRxBuf->GetReadableSize();
64
	int nowtime = GetNowSecond();
65

    
66
	E_RAW_ComStat coms = pLink->GetCommStatus();
67
	if (coms == CMST_RX_CNT && (nowtime - m_nLastSendTime) >= 2/* m_nRcvTimeOuts*/) {
68
		//??ʱ???ط???ֱ????????һ???ն?
69
		pRxBuf->Move(buflen);
70
		pLink->RegisterFrm(FRAME_RX_TIMEOUT);
71
		pLink->SetCommStatus(CMST_TX_CNT);
72
		PrintLog(
73
			LOG_VIOLATION,
74
			"%d???ն? rtuaddr:%d ???ݳ?ʱ\n",
75
			rtuno, rtuaddr);
76
		return -1;
77
	}
78
	if (buflen < 9)		//?????ݻ????ݲ??????ı???·ͨѶ״̬???????ȴ?
79
		return -1;
80

    
81
	if (buflen > 510)
82
		buflen = 510;
83
	datalen = pRxBuf->Read(buf, buflen, DEF_BUFF_NOMOVE);
84

    
85
	if (datalen < (buf[5] + 6))		//?????ݻ????ݲ??????ı???·ͨѶ״̬???????ȴ?
86
		return -1;
87

    
88
	if (buf[7] == 0x04) {
89
		pLink->RegisterFrm(FRAME_RX_SUC);
90
		ProcMeasure(buf + 7, buf[5]);//վ??,????,????,??????????豸״̬
91
		//		printf("վ??%x-%x,????%x-%x,????%x-%x,?????%x-%x\n",buf[10],buf[9],buf[12],buf[11],buf[14],buf[13],buf[16],buf[15]);
92
		pRxBuf->Move(buf[5] + 6);
93
		CmdRet(1);
94
		//pRxBuf->Move(datalen);
95
		pLink->SetCommStatus(CMST_TX_CNT);
96
		return 1;
97
	} else if (buf[7] == 0x10) {
98
		pLink->RegisterFrm(FRAME_RX_SUC);
99
		PrintLog(
100
			LOG_INFORMATION,
101
			"%d???ն? rtuaddr:%d ???յ?????سɹ?\n",
102
			rtuno, rtuaddr);
103
		CmdRet(1);
104
		pRxBuf->Move(datalen);
105
		pLink->SetCommStatus(CMST_TX_CNT);
106
		return 1;
107
	} else if ((buf[7] & 0x80) == 0x80) {
108
		pLink->RegisterFrm(FRAME_RX_SUC);
109
		PrintLog(
110
			LOG_INFORMATION,
111
			"%d???ն? rtuaddr:%d ???յ??????ʧ??\n",
112
			rtuno, rtuaddr);
113
		CmdRet(0);
114
		pRxBuf->Move(datalen);
115
		pLink->SetCommStatus(CMST_TX_CNT);
116
		return 1;
117
	} else {
118
		pLink->RegisterFrm(FRAME_RX_SUC);
119
		PrintLog(
120
			LOG_INFORMATION,
121
			"%d???ն? rtuaddr:%d ???յ??????otherʧ??\n",
122
			rtuno, rtuaddr);
123
		CmdRet(0);
124
		pRxBuf->Move(datalen);
125
		pLink->SetCommStatus(CMST_TX_CNT);
126
		return 1;
127

    
128
	}
129

    
130
	return 1;
131
}
132

    
133
int CModbusTcpc17pis::GetNowSecond() {
134
	CSeTime     SE_T;
135
	TCriterionTime tmptime;
136
	SE_T.GetNow(&tmptime);
137
	return (int)tmptime;
138
}
139

    
140
void CModbusTcpc17pis::ProcMeasure(uint8 *buf, int buflen) {
141
	sint32 rtuno = pLink->GetRtuNo();
142
	uint16 ycyxNo;
143
	uint8 value0, value1;
144

    
145
	if (0x04 == buf[0]) {
146
		for (int j = 0; j < buf[1] / 2; j++)//ֻȡ???ֽ?
147
		{
148
			value0 = buf[2 + 2 * j + 1];
149
			value1 = buf[2 + 2 * j];
150
			for (int k = 0; k < 8; k++) {
151
				ycyxNo = 16 * j + k;
152
				PRawCtrl->PutAYx(rtuno, ycyxNo + 1, value0 & 1);
153
				value0 >>= 1;
154
				ycyxNo = 8 + 16 * j + k;
155
				PRawCtrl->PutAYx(rtuno, ycyxNo + 1, value1 & 1);
156
				value1 >>= 1;
157
			}
158
		}
159

    
160
	}
161
}
162

    
163
sint32 CModbusTcpc17pis::TxProc() {
164
	E_RAW_ComStat coms = pLink->GetCommStatus();
165
	if (coms != CMST_TX_CNT && coms != CMST_NORMAL)
166
		return 0;
167

    
168
	if (ProcCmd()) {
169
		return 1; //?????ⲿ?·?????
170
	}
171

    
172
	if ((GetNowSecond() - m_nLastSendTime) > 1) {
173
		CallMeasure();
174
	}
175

    
176
	return 1;
177
}
178

    
179
BOOL CModbusTcpc17pis::ProcCmd() {
180
	sint32 rtuno = pLink->GetRtuNo();
181
	unsigned char rtuaddr = pRtu->GetRtuAddr(rtuno);
182
	int i = 0;
183
	S_RAWCMD rawcmd;
184

    
185
	while (pCmdMem->GetCmdNum(rtuno) > 0) {
186
		if (pCmdMem->GetACmd(rtuno, &rawcmd) == 0)
187
			break;
188
		PrintLog(LOG_INFORMATION, "?յ?%d???ն? rtuaddr:%d ????", rtuno, rtuaddr);
189

    
190
		if (rawcmd.type == DC_K_CMD_PORTAL) {
191
			//??ȡJson??????
192
			std::string raw_data = (char*)(rawcmd.data);
193
			Json::Reader reader(Json::Features::strictMode());
194
			Json::Value root;
195
			if (reader.parse(raw_data, root)) {
196
				int type = root["type"].asUInt();
197
				if (type == 1) {
198
					pCmdMem->DelACmd(rtuno);
199
					break;
200
				}
201
				m_CmdSrc = rawcmd.src;
202

    
203
				m_nNo++;
204
				BYTE buf[512];
205
				int len = 0;
206
				memset(buf, 0, 512);
207
				*(buf + len) = HIWORD(m_nNo); len++;
208
				*(buf + len) = LOWORD(m_nNo); len++;
209
				*(buf + len) = 0x00; len++;
210
				*(buf + len) = 0x00; len++;
211
				*(buf + len) = 0x00; len++;
212
				*(buf + len) = 0xff; len++;
213
				*(buf + len) = 0x01; len++;
214
				*(buf + len) = 0x10; len++;//Function code
215
				*(buf + len) = 0x00; len++;
216
				*(buf + len) = 0x00; len++;
217
				*(buf + len) = 0x00; len++;
218
				*(buf + len) = 0x7c; len++;//124word
219
				*(buf + len) = 0xf8; len++;//248[13-260]//13+248=261bytes???
220
				*(buf + len) = HIWORD(m_nNo); len++;
221
				*(buf + len) = LOWORD(m_nNo); len++;
222

    
223
				int ctype = root["ctype"].asUInt();
224
				int opt = root["opt"].asUInt();
225
				switch (ctype) {
226
				case 1:		//????????
227
					if (opt == 1) {
228
						*(buf + len + 0) = 0x00;
229
						*(buf + len + 1) = 0x01;//???????? 
230
						*(buf + len + 2) = 100;//????1-100
231
						*(buf + len + 3) = 0;//??????ģʽ
232
					} else {
233
						*(buf + len + 0) = 0x00;
234
						*(buf + len + 1) = 0x01;//????ȡ??
235
						*(buf + len + 2) = 1;//????1-100
236
						*(buf + len + 3) = 1;//??ȡ??????ģʽ
237
					}
238
					break;
239
				case 2:		//????ģʽ
240
					if (opt == 1) {
241
						*(buf + len + 0) = 0x00;
242
						*(buf + len + 1) = 0x00;//???????? 
243
						*(buf + len + 2) = 100;//????1-100
244
						*(buf + len + 3) = 0;//??????ģʽ
245
					} else {
246
						*(buf + len + 0) = 0x00;
247
						*(buf + len + 1) = 0x00;//????ȡ??
248
						*(buf + len + 2) = 1;//????1-100
249
						*(buf + len + 3) = 1;//??ȡ??????ģʽ
250
					}
251
					break;
252
				case 3:		//???⹤??
253

    
254
					break;
255
				case 7:		//????????
256
					*(buf + len + 0) = 0x00;
257
					*(buf + len + 1) = 0x02;//????????
258
					if (opt > 100)
259
						*(buf + len + 2) = 100;//????1-100
260
					else
261
						*(buf + len + 2) = opt;//????1-100
262
					*(buf + len + 3) = 1;//??ȡ??????ģʽ
263
					break;
264
				case 8:		//??????Ļ
265
					*(buf + len + 0) = 0x00;
266
					*(buf + len + 1) = 0x04;//4-LCD??????ģʽ
267
					*(buf + len + 2) = 0;
268
					*(buf + len + 3) = opt + 1;//1-??????2??????
269
					break;
270
				}
271
				len += 4;
272
				int zone = root["zone"].asUInt();
273
				for (int k = 0; k < 4; k++)//վ???????С????С??????
274
				{
275
					PRawCtrl->PutAYx(rtuno, 65 + k, zone & 1);
276
					*(buf + len) = 0; len++;
277
					*(buf + len) = zone & 1; len++;
278
					zone >>= 1;
279
				}
280
				int wlen = 0;	//ת????ֽ???
281
				if (ctype == 1 || ctype == 2 || ctype == 3) {
282
					std::string text = root["text"].asString();
283

    
284
					char tmp[2048] = { 0 };
285
					int n = 2048 - 1;
286
					strncpy(tmp, text.c_str(), n);
287
					char *inbuf = (char*)tmp;
288
					size_t inlen = strlen(inbuf);
289
					int m = 2048 * 4;
290
					size_t outlen = m;
291
					uint16 unicode[2048 * 4] = { 0 };
292
					char* outbuf = (char*)unicode;
293
					char* from_charset = "gb2312";
294
					char* to_charset = "utf-16";
295
					iconv_t cd;
296
					char** pin = &inbuf;
297
					char** pout = &outbuf;
298
					cd = iconv_open(to_charset, from_charset);
299
					if (cd == 0)
300
						PrintLog(LOG_VIOLATION, "iconv false!\n");
301
					memset(outbuf, 0, outlen);
302
					if (iconv(cd, pin, &inlen, pout, &outlen) == -1) {
303
						PrintLog(LOG_VIOLATION, "iconv false!inlen=%d,outlen=%d,Msg=%s,MsgLen=%d\n", inlen, (int)outlen, inbuf, text.length());
304
					}
305
					wlen = m - outlen;
306
					if (wlen >116*2) {
307
						wlen = 116 * 2;
308
						memcpy(buf + len, (uint8*)(unicode), wlen);
309
						len += wlen;
310
						memset(buf + len, 0, 2);
311
						len += 2;
312
					} else {
313
						memcpy(buf + len, (uint8*)(unicode), wlen);
314
						len += wlen;
315
						memset(buf + len, 0, 117 * 2 - wlen);
316
						len += 117 * 2 - wlen;
317
					}
318
					iconv_close(cd);
319

    
320
				} else {
321
					memset(buf + len, 0, 117 * 2);
322
					len += 117 * 2;
323
				}
324
				char prttmp[1000] = { "TxCMD->" };
325
				for (i = 0; i < len; i++)
326
					sprintf(prttmp + 3 * i + 7, "%02x ", buf[i]);
327
				PrintLog(LOG_INFORMATION, prttmp, rtuno);
328
				//*/
329

    
330
				pTxBuf->Write(buf, len);
331
				pLink->SetCommStatus(CMST_NORMAL);
332
				m_nLastSendTime = GetNowSecond();
333
				PrintLog(LOG_INFORMATION, "%d???նˣ?ң??????ֱ???·?", rtuno);
334

    
335
				pCmdMem->DelACmd(rtuno);
336
				return TRUE;
337
			}
338

    
339

    
340
			/*
341
			if (rawcmd.type == DC_K_CMD_PISINFO)
342
			{
343
			CSI_D_PIS_CMD *PisCmd;
344
			PisCmd	=	(CSI_D_PIS_CMD *)rawcmd.data;
345
			m_CmdSrc = rawcmd.src;
346

    
347
			m_nNo++;
348
			BYTE buf[512];
349
			int len = 0;
350
			memset(buf,0,512);
351
			*(buf+len)	= HIWORD(m_nNo);len++;
352
			*(buf+len)	= LOWORD(m_nNo);len++;
353
			*(buf+len)	= 0x00;len++;
354
			*(buf+len)	= 0x00;len++;
355
			*(buf+len)	= 0x00;len++;
356
			*(buf+len)	= 0xff;len++;
357
			*(buf+len)	= 0x01;len++;
358
			*(buf+len)	= 0x10;len++;//Function code
359
			*(buf+len)	= 0x00;len++;
360
			*(buf+len)	= 0x00;len++;
361
			*(buf+len)	= 0x00;len++;
362
			*(buf+len)	= 0x7c;len++;//124word
363
			*(buf+len)	= 0xf8;len++;//248[13-260]//13+248=261bytes???
364
			*(buf+len)	= HIWORD(m_nNo);len++;
365
			*(buf+len)	= LOWORD(m_nNo);len++;
366
			switch (PisCmd->Func)
367
			{
368
			case PIS_FUNC_URGCTL:
369
			switch (PisCmd->Val)
370
			{
371
			case PIS_VAL_CTL_NORM:
372
			*(buf+len+0)	= 0x00;
373
			*(buf+len+1)	= 0x00;//????????
374
			*(buf+len+2)	= 1;//????1-100
375
			*(buf+len+3)	= 1;//??ȡ??????ģʽ
376
			break;
377
			case PIS_VAL_CTL_URGEN:
378
			*(buf+len+0)	= 0x00;
379
			*(buf+len+1)	= 0x01;//????????
380
			*(buf+len+2)	= 100;//????1-100
381
			*(buf+len+3)	= 0;//??????ģʽ
382
			break;
383
			case PIS_VAL_CTL_CANCEL:
384
			*(buf+len+0)	= 0x00;
385
			*(buf+len+1)	= 0x00;//????ȡ??
386
			*(buf+len+2)	= 10;//????1-100
387
			*(buf+len+3)	= 1;//??ȡ??????ģʽ
388
			break;
389
			break;
390
			default:
391
			break;
392
			}
393
			break;
394
			case PIS_FUNC_ONOFF:
395
			//				*(buf+len+0)	= 0x00;
396
			//				*(buf+len+1)	= 0x00;//????ȡ??
397

    
398
			switch (PisCmd->Val)
399
			{
400
			case PIS_VAL_ON:
401
			*(buf+len+2)	= 50;//????1-100
402
			*(buf+len+3)	= 0x00;//??????ģʽ
403
			break;
404
			case PIS_VAL_OFF:
405
			*(buf+len+2)	= 0;//????1-100
406
			*(buf+len+3)	= 0x01;//??ȡ??????ģʽ
407
			break;
408
			default:
409
			*(buf+len+2)	= 0;//????1-100
410
			*(buf+len+3)	= 0x01;//??ȡ??????ģʽ
411
			break;
412
			}
413
			break;
414
			case PIS_FUNC_VOLUME:
415
			*(buf+len+0)	= 0x00;
416
			*(buf+len+1)	= 0x02;//????????
417
			if(PisCmd->Val>100)
418
			*(buf+len+2)	= 100;//????1-100
419
			else
420
			*(buf+len+2)	= PisCmd->Val;//????1-100
421
			*(buf+len+3)	= 1;//??ȡ??????ģʽ
422
			break;
423
			case PIS_FUNC_LEDTEST://Modified by WYF20180605 ??????
424

    
425
			//				*(buf+len+len)	= 0x03;len++;//(h)LED??Ļ????
426
			//				*(buf+len+0)	= 0x00;
427
			//				*(buf+len+1)	= 0x03;//3-?????ģʽ
428
			//				*(buf+len+2)	= PisCmd->Val / 0x10;//0-2???̻?
429
			//				*(buf+len+3)	= PisCmd->Val % 0x10;//0-9?????ٶ?
430

    
431
			*(buf+len+0)	= 0x00;
432
			*(buf+len+1)	= 0x04;//4-LCD??????ģʽ
433
			*(buf+len+2)	= 0;
434
			*(buf+len+3)	= PisCmd->Val % 0x100;//1-??????2??????
435
			break;
436
			default:
437
			*(buf+len+2)	= 50;//????1-100
438
			*(buf+len+3)	= 0x00;//??????ģʽ
439
			break;
440
			}
441
			len += 4;
442
			//(e)??վ????־
443
			uchar ztmp=0;
444
			for(i=0;i<13;i++)
445
			{
446
			ztmp=PisCmd->Zone[i];
447
			if(ztmp>0)break;
448
			}
449
			for(int k = 0; k < 4; k++)//վ???????С????С??????
450
			{
451
			PRawCtrl->PutAYx(rtuno,65+k,ztmp&1);
452
			*(buf+len)=0;len++;
453
			*(buf+len)=ztmp&1;len++;
454
			ztmp  >>=	1;
455
			}
456
			char *sss="????";
457
			//(g)??Ϣ???ݻ??豸??????ֵ
458
			if (PisCmd->Func == PIS_FUNC_URGCTL || PisCmd->Func == 0)
459
			{
460
			if(PisCmd->MsgLen>117*2)	PisCmd->MsgLen=117*2;
461
			//buf[len]=49;buf[len+1]=50;buf[len+2]=51;buf[len+3]=52;
462
			//buf[len+5]=0x6b;buf[len+4]=0x70;buf[len+7]=0x7e;buf[len+6]=0x70;
463
			memcpy(buf+len, PisCmd->Msg, PisCmd->MsgLen);
464
			//len += PisCmd->MsgLen;
465
			//memset(buf+len, 0, 117*2 - PisCmd->MsgLen-8);
466
			len += 117*2;// - PisCmd->MsgLen-8;
467
			}
468
			else
469
			{
470
			memset(buf+len, 0, 117*2);
471
			len += 117*2;
472
			}
473
			//
474
			//	char prttmp[1000]={"TxCMD->"};
475
			//	for(i=0; i<len; i++)
476
			//		sprintf(prttmp+3*i+7,"%02x ",buf[i]);
477
			//	PrintLog(LOG_INFORMATION,prttmp, rtuno);
478
			//
479

    
480
			pTxBuf->Write(buf,len);
481
			pLink->SetCommStatus(CMST_NORMAL);
482
			m_nLastSendTime = GetNowSecond();
483
			PrintLog(LOG_INFORMATION,"%d???նˣ?ң??????ֱ???·?", rtuno);
484

    
485
			pCmdMem->DelACmd(rtuno);
486
			return TRUE;
487
			}
488
			*/
489

    
490

    
491

    
492
			else//?????????·????????
493
				pCmdMem->DelACmd(rtuno);
494
			break;
495
		}
496
		return FALSE;
497
	}
498
}
499
	void CModbusTcpc17pis::SetExtendedPara(uint16 no, sint32 val) {
500
		if (no == 1) {
501
			m_nStationNo = val;
502
		}
503
	}
504

    
505

    
506
	void CModbusTcpc17pis::CmdRet(uint8 result) {
507
		sint32 rtuno = pLink->GetRtuNo();
508

    
509
		S_RAWCMD rawcmd;
510
		S_CMD_RET cmdinfo;
511
		rawcmd.type = DC_K_CMDRET_PISINFO;
512

    
513
		rawcmd.len = sizeof(cmdinfo);
514

    
515
		rawcmd.src = m_CmdSrc;
516

    
517
		cmdinfo.RetCode = result;
518
		memcpy(rawcmd.data, &cmdinfo, sizeof(cmdinfo));
519
		pCmdMem->RptAResult(rtuno, rawcmd);
520
		/*	PrintLog(
521
		LOG_INFORMATION,
522
		"?յ????????Ϣ%d\n",
523
		result);	*/
524
	}
525
	void CModbusTcpc17pis::CallMeasure() {
526
		sint32 rtuno = pLink->GetRtuNo();
527
		unsigned char rtuadd = pRtu->GetRtuAddr(rtuno);
528

    
529
		BYTE buf[512];
530
		int	len = 0;
531
		m_nNo++;
532

    
533
		*(buf + len) = HIWORD(m_nNo); len++;
534
		*(buf + len) = LOWORD(m_nNo); len++;
535
		*(buf + len) = 0x00; len++;
536
		*(buf + len) = 0x00; len++;
537
		*(buf + len) = 0x00; len++;
538
		*(buf + len) = 0x06; len++;
539
		*(buf + len) = 0x01; len++;//0xff
540
		*(buf + len) = 0x04; len++;//Function code
541
		*(buf + len) = 0x00; len++;
542
		*(buf + len) = 0; len++;//0x50 + m_nStationNo;len++;
543
		*(buf + len) = 0x00; len++;
544
		*(buf + len) = 0x04; len++;//0x0A;len++;
545

    
546
		pTxBuf->Write(buf, len);
547
		pLink->SetCommStatus(CMST_RX_CNT);
548
		m_nLastSendTime = GetNowSecond();
549
		//	PrintLog(LOG_INFORMATION,"?ٻ?PIS?豸״̬rtuno:%d rtuaddr:1 len=4Word\n",rtuno);
550

    
551
		/*	int i;
552
		for(i=0; i<len; i++)
553
		printf("%x ",buf[i]);
554
		printf("\n");*/
555

    
556
		return;
557
	}
558

    
559
	//??Լ???󴴽?????
560
#ifdef __unix
561
	extern "C" CProtocol* CreateProtocol(char *defpara)
562
#else
563
	extern "C" __declspec(dllexport)  CProtocol* CreateProtocol(char *defpara)
564
#endif
565
	{
566
		CProtocol *pEpv = new CModbusTcpc17pis;
567
		return pEpv;
568
	}
(1-1/3)