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

📄 gst.cpp

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

void CGst::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;
	send_addr = TRUE;

	parity_mask = port_set[myportno].data - 5;
	parity_mask |= 0x28;  // none parity
}

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

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

		clear_com_rece_buf(myportno); // clear receive buffer
		master_send();

		if (send_addr) port_timeout[myportno] = 0;
		else port_timeout[myportno] = 18; // between sending address and message
																			// only a small gap may take effect
	}
}

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

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

	if (recbuf[0] == 0xaa && recbuf[1] == 0x55 && recbuf[2] ==0x55){
		read_com_rece_nbytes(recbuf, 4, myportno);
		draw_rece_message(myportno, recbuf, 4, POLL_RECE_COLOR);
		return;
	} // no action take place only ack
	else if (recbuf[0] == 0xaa && recbuf[1] == 0x15){
		if (rece_len < 24) return;
		read_com_rece_nbytes(recbuf, 24, myportno);
		draw_rece_message(myportno, recbuf, 24, POLL_RECE_COLOR);
		rece_end = 1;
		process(recbuf);
	} // some thing happened
	else del_com_rece_one(myportno);
}

void CGst::process(INT8U *buf)
{
	int i;
	INT16U switch_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))
				&& (buf[2] == INT8UtoBCD(yx_define[i].devno))
				&& (buf[3] == INT8UtoBCD(yx_define[i].type))
				&& (buf[4] == INT8UtoBCD(yx_define[i].info))
				&& (yx_define[i].channel == buf[5])) {

				yx_update_db(i, 1);

				temp_soe.status_switch_no = i & 0xfff;
				temp_soe.status_switch_no |= 0x8000;	// 0x80:0->1  0x00:1->0

				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

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

// change uart to address mode
void CGst::change_parity_mask()
{
	parity_mask &= 0xef;
	outportb(com_addr + 3 , parity_mask);
}

// change uart to data mode
void CGst::change_parity_space()
{
	parity_mask |= 0x10;
	outportb(com_addr + 3 , parity_mask);
}

void CGst::master_send()
{
	com_addr = port_set[myportno].addr;

	if (send_addr){
		askbuf[0] = 0x41;
		change_parity_mask();
		com_transfer(myportno, 1, &askbuf[0]);
		send_addr = FALSE; //
		port_timeout[myportno] = 18;
		return;
	}

	askbuf[1] = 0xaa;
	askbuf[2] = 0xaa;
	askbuf[3] = 0x54;  //check_sum
	change_parity_space();

	send_addr = TRUE;
	rece_end = 0;
	com_transfer(myportno, 3, &askbuf[1]);
	draw_send_message(myportno, askbuf, 4, POLL_SEND_COLOR);
	port_timeout[myportno] = 0;
}

INT8U CGst::checksum(INT8U *buf, INT8U num)
{
	INT8U temp = 0;
	for (int i = 0; i < num; i++)
		temp = temp + buf[i];
	return(temp);
}

INT8U CGst::INT8UtoBCD(INT8U value)
{
	INT8U temp=0;
	temp = value / 10;
	temp <<= 4;
	temp += value % 10;

	return (temp);
}


⌨️ 快捷键说明

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