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

📄 dc.cpp

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

#define READWORD 0x03 // modbus command don't modify

void CDC::initial(INT8U portno)
{
	int i;
	myportno = portno;
	start_add = port_set[myportno].start_addr;
	end_add = port_set[myportno].end_addr;

	for (i = start_add; i <= end_add; i++) {
		LINK_flag[i] = 0;
		fail_count[i] = 0;
	}
	station_add = start_add;
}

void CDC::deal()
{
	master_receive();
	if (!port_free(myportno)) return;

	if (port_timeout[myportno] >= 20) { //100 = 0.5s
		if (rece_end == 1) {
			fail_count[send_add] = 0;
			LINK_flag[send_add] = 1;
		}
		else if (rece_end == 2) {
			if (fail_count[send_add] > 3)
				LINK_flag[send_add] = 0;
			else fail_count[send_add]++;
			clear_com_rece_buf(myportno);
		}
		master_send();
		port_timeout[myportno] = 0;
	}
}

void CDC::master_receive()
{
	INT16U rece_len;
	rece_len = get_com_rece_num(myportno);

	if (rece_len < 3) return; // too little
	get_com_rece_nbytes(recbuf, 3, myportno);

	INT16U framelen = recbuf[2] + 5;
	if (recbuf[0] == send_add && recbuf[1] == currentCMD) {
		if (rece_len >= framelen) {
			read_com_rece_nbytes(recbuf, framelen, myportno);
			INT16U temp = crc16(recbuf, framelen - 2);
			INT16U temp1 = ((recbuf[framelen - 2] << 8) | recbuf[framelen - 1]);

			if (temp == temp1) {
				draw_rece_message(myportno, recbuf, framelen, POLL_RECE_COLOR);
				rece_end = 1;
				process(recbuf);
			}
		}
	}
	else del_com_rece_one(myportno);
}

void CDC::process(INT8U *buf)
{
	processYx(1);
	processYx(2);
	processYx(3);
	int i;
	INT16U temp;
 /*	int i;
	INT16U temp, yx_no;
	sclock sclk;
	soe_record_t temp_soe;
	yxbw_record_t temp_yx_bw;

	for (i = 0; i < _TotalYx; i++) {
		if ((myportno == (yx_define[i].portno - 1)) &&
			(yx_define[i].devno == buf[0]) && (yx_define[i].info < 16)) {
			temp = ((buf[4] << 8) & 0xff00) + buf[3];
			temp = (temp >> yx_define[i].info) & 0x01;
			yx_no = i;
			if (get_yx_dot(yx_no) ^ temp) {
				yx_update_db(i, temp);

				temp_soe.status_switch_no = yx_no & 0xfff;
				if (temp)
					temp_soe.status_switch_no |= 0x8000;	// 0x80:0->1  0x00:1->0
				else temp_soe.status_switch_no &= 0x7fff;

				GetClock(&sclk);
				temp_soe.day = sclk.day;
				temp_soe.hour = sclk.hour;
				temp_soe.minute = sclk.minute;
				temp_soe.second = sclk.second;
				temp_soe.msecond = sclk.msecond;

				insert_soe(&temp_soe);						// add to soe buffer

				temp_yx_bw.func_code = yx_no / 32;
				temp_yx_bw.yx_code[0] = dbdata.Yx[yx_no / 32 * 2];
				temp_yx_bw.yx_code[1] = dbdata.Yx[yx_no / 32 * 2 + 1];
				insert_yx_bw(&temp_yx_bw);							// add to yx bw buffer
			}
		}
	}
	 */
	for (i = 0; i < _TotalYc; i++) {
		if ((myportno == (yc_define[i].portno - 1)) &&
			(yc_define[i].devno == buf[0]) && (yc_define[i].info < 20)) {
			temp =buf[yc_define[i].info * 2 + 3];
			temp = ((temp << 8) & 0xff00) + buf[yc_define[i].info * 2 + 4];
			yc_update_db(i, temp / 10);
		}
	}
}


INT16U CDC::crc16(INT8U *msg, INT8U len)
{
	INT8U uindex;
	INT8U uchhi=0xff;
	INT8U uchlo=0xff;
	while (len--) {
		uindex = uchhi ^ *msg++;
		uchhi = uchlo ^ auchhi[uindex];
		uchlo = auchlo[uindex];
	}
	return (uchhi << 8 | uchlo);
}

void CDC::master_send()
{
	INT16U temp;
	if (++station_add > end_add) station_add = start_add;

	askbuf[0] = station_add;      //modbusaddr;
	askbuf[1] = READWORD;

	askbuf[2] = 0x00; // data address
	askbuf[3] = 0x61; // data address

	askbuf[4] = 0x00; // high byte
	askbuf[5] = 0x12; // read word number

	temp = crc16(askbuf, 6);
	askbuf[6] = (temp >> 8) & 0xff;
	askbuf[7] = temp & 0xff;
	currentCMD = READWORD;

	rece_end = 2;
	send_add = station_add;
	com_transfer(myportno, 8, askbuf);
	draw_send_message(myportno, askbuf, 8, POLL_SEND_COLOR);
}
void CDC::processYx(int flag)
{
	int i;
	INT16U temp, yx_no;
	sclock sclk;
	soe_record_t temp_soe;
	yxbw_record_t temp_yx_bw;

	for (i = 0; i < _TotalYx; i++) {
		if ((myportno == (yx_define[i].portno - 1)) &&
			(yx_define[i].devno == recbuf[0]) && (yx_define[i].info < 16)&&
			(yx_define[i].channel==flag)) {
			temp = ((recbuf[2*flag+2] << 8) & 0xff00) +recbuf[2*flag+1];
			temp = (temp >> yx_define[i].info) & 0x01;
			yx_no = i;
			if (get_yx_dot(yx_no) ^ temp) {
				yx_update_db(i, temp);

				temp_soe.status_switch_no = yx_no & 0xfff;
				if (temp)
					temp_soe.status_switch_no |= 0x8000;	// 0x80:0->1  0x00:1->0
				else temp_soe.status_switch_no &= 0x7fff;

				GetClock(&sclk);
				temp_soe.day = sclk.day;
				temp_soe.hour = sclk.hour;
				temp_soe.minute = sclk.minute;
				temp_soe.second = sclk.second;
				temp_soe.msecond = sclk.msecond;

				insert_soe(&temp_soe);						// add to soe buffer

				temp_yx_bw.func_code = yx_no / 32;
				temp_yx_bw.yx_code[0] = dbdata.Yx[yx_no / 32 * 2];
				temp_yx_bw.yx_code[1] = dbdata.Yx[yx_no / 32 * 2 + 1];
				insert_yx_bw(&temp_yx_bw);							// add to yx bw buffer
			}
		}
	}

}

⌨️ 快捷键说明

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