⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dl645.cpp

📁 一个完整的RTU程序源码,用DOS平台,支持16串口,在天津港用的很多,8个规约103,modbus,cdt,1801,u4f
💻 CPP
字号:
#include "sc_cfg.h"
#include "common.h"
#include "class.h"
#include "extern.h"

CDL645::CDL645(INT8U portno):CMasterPollingProtocol(portno)
{
	queryno = 0;
	send_cycle = 0;
	SetQueryGap(1000);
}

void CDL645::master_receive()
{
	INT8U recbuf[DL645_RECE_BUFSIZE];
	INT16U rece_len = get_com_rece_num(myportno);

	if (rece_len < 12) return; // too little

	get_com_rece_nbytes(recbuf, 12, myportno);
	INT16U framelen = recbuf[9] + 12;

	if (recbuf[0] == 0x68 && recbuf[7] == 0x68) {
		if (rece_len >= framelen) {
			read_com_rece_nbytes(recbuf, framelen, myportno);

			if (recbuf[framelen - 1] == 0x16 &&
				checksum(recbuf, framelen - 2) == recbuf[framelen - 2]) {
				draw_rece_message(myportno, recbuf, framelen, POLL_RECE_COLOR);
				QueryStatus = ANSWER;
				process(recbuf);
			}
		}
	}
	else del_com_rece_one(myportno);
}

void CDL645::process(INT8U *buf)
{
	INT32U temp = 0;
	INT8U tmpint = 0, tmpbin = 0;
	YmDefine_t *pym = NULL;

	pym = GetYmDefine(pym_tbl[queryno]);

	if (buf[1] != ((pym->devno / 10 * 16) + (pym->devno % 10))) return;
	// receive message address is incorrect

	if (buf[8] != 0x81) return;
	// status code is incorrect;

	INT16U RegTag = buf[10] + ((buf[11] << 8) & 0xff00);
	if (RegTag != pym->info) return;
	// tag is incorrect;

	if (RegTag >= 0x9010 && RegTag < 0x9970) { // 4 bytes data
		for (int i = 3; i >= 0; i--) { // 4 bytes = INT32U
			temp *= 100;
			tmpint = buf[12 + i] - 0x33; // BCD to binary convert
			tmpbin = (tmpint & 0x0f) + ((tmpint & 0xf0) >> 4) * 10;
			temp += (INT32U)tmpbin;
		}
		ym_update_db(pym_tbl[queryno], temp);
	}
}

void CDL645::send_query()
{
	INT8U askbuf[14];
	YmDefine_t *pym = NULL;

	pym = GetYmDefine(pym_tbl[queryno]);

	askbuf[0] = 0x68;
	askbuf[1] = (pym->devno / 10 * 16) + (pym->devno % 10);
	askbuf[2] = 0x00;
	askbuf[3] = 0x00;
	askbuf[4] = 0x00;
	askbuf[5] = 0x00;
	askbuf[6] = 0x00;
	askbuf[7] = 0x68;
	askbuf[8] = 0x01;
	askbuf[9] = 0x02;
	askbuf[10] = (pym->info & 0xff) + 0x33;
	askbuf[11] = ((pym->info >> 8) & 0xff) + 0x33;
	askbuf[12] = checksum(askbuf, 12);
	askbuf[13] = 0x16;

	QueryStatus = QUERY;
	com_transfer(myportno, 14, askbuf);
	draw_send_message(myportno, askbuf, 14, POLL_SEND_COLOR);

/*  askbuf[0]=0x68;
	askbuf[1]=0x99;
	askbuf[2]=0x99;
	askbuf[3]=0x99;
	askbuf[4]=0x99;
	askbuf[5]=0x99;
	askbuf[6]=0x99;
	askbuf[7]=0x68;
	askbuf[8]=0x0a;
	askbuf[9]=0x06;
	askbuf[10]=0x44;
	askbuf[11]=0x33;
	askbuf[12]=0x33;
	askbuf[13]=0x33;
	askbuf[14]=0x33;
	askbuf[15]=0x33;
	askbuf[16]=checksum(askbuf,16);
	askbuf[17]=0x16;
	current_num_nosend=18;*/
}

void CDL645::send_time() // boardcasting time
{
	INT16U temp;
	sclock sclk;
	INT8U askbuf[18];

	askbuf[0] = 0x68;
	askbuf[1] = 0x99;
	askbuf[2] = 0x99;
	askbuf[3] = 0x99;
	askbuf[4] = 0x99;
	askbuf[5] = 0x99;
	askbuf[6] = 0x99;
	askbuf[7] = 0x68;
	askbuf[8] = 0x08;
	askbuf[9] = 0x06;
	GetClock(&sclk);
	askbuf[10] = (((sclk.second / 10) << 4) + sclk.second % 10) & 0xff + 0x33;
	askbuf[11] = (((sclk.minute / 10) << 4) + sclk.minute % 10) & 0xff + 0x33;
	askbuf[12] = (((sclk.hour / 10) << 4) + sclk.hour % 10) & 0xff + 0x33;
	askbuf[13] = (((sclk.day / 10) << 4) + sclk.day % 10) & 0xff + 0x33;
	askbuf[14] = (((sclk.month / 10) << 4) + sclk.month % 10) & 0xff + 0x33;
	temp = sclk.year % 100;
	askbuf[15] = (((temp / 10) << 4) + temp % 10) & 0xff + 0x33;
	askbuf[16] = checksum(askbuf, 16);
	askbuf[17] = 0x16;

	QueryStatus = BOARDCAST;
	com_transfer(myportno, 18, askbuf);
	draw_send_message(myportno, askbuf, 18, POLL_SEND_COLOR);
}

void CDL645::master_send()
{
	if (send_cycle++ > 60000) {
		send_time();
		send_cycle = 0;
	}
	else {
		if (ym_num < 1) return;
		if (++queryno >= ym_num) queryno = 0;
		send_query();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -