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

📄 mod1.cpp

📁 一个完整的RTU程序源码,用DOS平台,支持16串口,在天津港用的很多,8个规约103,modbus,cdt,1801,u4f
💻 CPP
📖 第 1 页 / 共 2 页
字号:

#include "modbus.h"
//#include "extern.h"


#define READ_DI  0x02
#define READWORD 0x03
#define READ_AI  0x04
#define SET_COIL 0x05



void CMod1::send_yxyc()
{
	INT16U temp;

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

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

	askbuf[4] = 0x00; // high byte
	askbuf[5] = 0x10; // 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 CMod1::initial(INT8U portno)
{
	int i;
	myportno = portno;
	start_add = port_set[myportno].start_addr;
	end_add = port_set[myportno].end_addr;
	send_cycle = 1;

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





INT16U CMod1::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 CMod1::processREADWORD(INT8U *buf)
{
	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 & 0xfff;
			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;
				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);

				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);
			}
		}
	}

	for (i = 0; i < _TotalYc; i++) {
		if ((myportno == (yc_define[i].portno - 1)) &&
			(yc_define[i].devno == buf[0]) && (yc_define[i].info < 10)) {
			temp = buf[yc_define[i].info * 2 + 3];//yong yu ding wei
			temp = ((temp << 8) & 0xff00) + buf[yc_define[i].info * 2 + 4];
			yc_update_db(i, temp / 10);//10kv;/1000
		}
	}
}
void CMod1::processREAD_DI(INT8U *buf)
{
	int i;

	INT32U temp;
	INT16U yx_no,temp1,temp2;

	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 < 14)) {

			temp1 = (((INT16U)buf[3] << 8) & 0xff00) + buf[2];
		 /*	temp2 = (((INT16U)buf[6] << 8) & 0xff00) + buf[5];
			temp = (((INT32U)temp2 << 16) & 0xffff0000) + temp1;*/
			temp1 = (temp1 >> yx_define[i].info) & 0x01;

			yx_no =  i & 0xfff;


			if (get_yx_dot(yx_no) ^ temp1) {
				yx_update_db(i, temp1);

				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);
			}
		}
	}


}

void CMod1::processREAD_AI(INT8U *buf)
{
	int i;
	INT32U temp;
	INT16U temp1,temp2;
	yxbw_record_t temp_yx_bw;
		if (buf[2] == 0x28)
 {
		for (i = 0; i < _TotalYc; i++) {
		if ((myportno == (yc_define[i].portno - 1)) &&
			(yc_define[i].devno == buf[0]) && (yc_define[i].info < 12)) {

						temp1 = buf[yc_define[i].info * 4 + 3];
			temp1 = ((temp1 << 8) & 0xff00) + buf[yc_define[i].info * 4 + 4];
			temp2 = buf[yc_define[i].info * 4 + 5];
			temp2 = ((temp2 << 8) & 0xff00) + buf[yc_define[i].info * 4 + 6];
			temp = ((temp1 << 16) & 0xffff0000) + temp2;
			yc_update_db(i, temp / 1000);
		}
	}

 }
		else if (buf[2] == 0x14)
 {
				for (i = 0; i < _TotalYm; i++)
{
/*		if ((myportno == (ym_define[i][0] - 1)) &&
			(ym_define[i][1] == buf[0]) && (ym_define[i][3] < 7))
		{
			temp1 = buf[ym_define[i][3] * 4 + 3];
			temp1 = ((temp1 << 8) & 0xff00) + buf[ym_define[i][3] * 4 + 4];
			temp2 = buf[ym_define[i][3] * 4 + 5];
			temp2 = ((temp2 << 8) & 0xff00) + buf[ym_define[i][3] * 4 + 6];
			 temp = ((temp1 << 16) & 0xffff0000) + temp2;
			ym_update_db(PORT_YC_SOURCE, i, temp / 1000);
		} */
}
				for (i = 0; i < _TotalYc; i++)
		{
		if ((myportno == (yc_define[i].portno - 1)) &&
			(yc_define[i].devno == buf[0]) &&
			(yc_define[i].info < 15))
			//(10 <= yc_define[i][4] < 15))
				{
													temp1 = buf[yc_define[i].info * 2 + 3];
			temp1 = ((temp1 << 8) & 0xff00) + buf[yc_define[i].info * 2 + 4];
		/*	temp2 = buf[yc_define[i][4] * 4 + 5];
			temp2 = ((temp2 << 8) & 0xff00) + buf[yc_define[i][4] * 4 + 6];
			 temp = ((temp1 << 16) & 0xffff0000) + temp2; */
		//	yc_update_db(i, temp / 1000);
					//   	yc_update_db(i, temp1);
								}
	}
 }
	else  if (buf[2] == 0x20){
		for(i = 0; i<_TotalYx; i++){
			if((myportno == (yx_define[i].portno - 1))&&
				(yx_define[i].devno == buf[0])&&(yx_define[i].info>19)&&
				(yx_define[i].info<26))
				;

		}
	}

}

void CMod1::processSET_COIL(INT8U *buf)
{
	if (myportno == port_yk_info.yk_des && buf[0] == port_yk_info.dev_no){
		if (
				buf[1] == 0x05 && buf[2] == 0x00 &&	buf[4] == 0xff && buf[5] == 0xff &&
			( ((buf[3] == 0x10) && (port_yk_info.yk_cmd == YKCCLOSE))
			||((buf[3] == 0x0f) && (port_yk_info.yk_cmd == YKCTRIP)) )
			){
			port_yk_info.Y_CHECK = TRUE;
			port_yk_info.check_result = YKROK;
		}
		else {
			port_yk_info.Y_CHECK = TRUE;
			port_yk_info.check_result = FALSE;
		}
	}
}

void CMod1::change_addr()
{
	station_add = station_add+2;
	if (station_add > end_add)
	{
		station_add = start_add;
		send_cycle++;
	}

}



void CMod1::send_yx()
{
	INT16U temp;


	askbuf[0] = station_add;
	askbuf[1] = READ_DI;
	askbuf[2] = 0x00;
	askbuf[3] = 0x00;

	askbuf[4] = 0x00;
	askbuf[5] = 0x0e; //14

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

	rece_end = 2;

	com_transfer(myportno, 8, askbuf);
	draw_send_message(myportno, askbuf, 8, POLL_SEND_COLOR);
}



void CMod1::send_yc()
{
	INT16U temp;

	askbuf[0] = station_add;
	askbuf[1] = READ_AI;
	askbuf[2] = 0x00;
	askbuf[3] = 0x00;

	askbuf[4] = 0x00;
	askbuf[5] = 0x12;//0x0a

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

	rece_end = 2;

	com_transfer(myportno, 8, askbuf);
	draw_send_message(myportno, askbuf, 8, POLL_SEND_COLOR);
}




void CMod1::send_ymyc()
{
	INT16U temp;

	askbuf[0] = station_add;
	askbuf[1] = READ_AI;
	askbuf[2] = 0x00;
	askbuf[3] = 0x16;

	askbuf[4] = 0x00;
	askbuf[5] = 0x0e;

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

	rece_end = 2;
	com_transfer(myportno, 8, askbuf);
	draw_send_message(myportno, askbuf, 8, POLL_SEND_COLOR);
}

void CMod1::send_leds()
{
	INT16U temp;
	askbuf[0] = station_add;
	askbuf[1] = READ_AI;
	askbuf[2] = 0x00;
	askbuf[3] = 0x28;

	askbuf[4] = 0x00;
	askbuf[5] = 0x10e;

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

	rece_end = 2;
	com_transfer(myportno, 8, askbuf);
	draw_send_message(myportno, askbuf, 8, POLL_SEND_COLOR);

}

void CMod1::send_yk_preset()
{
	INT16U temp;

	askbuf[0] = station_add;
	askbuf[1] = SET_COIL;
	askbuf[2] = 0x00;

	if (port_yk_info.yk_cmd == YKCTRIP) {
		askbuf[3] = 0x0f;
	}
	else if (port_yk_info.yk_cmd == YKCCLOSE){
		askbuf[3] = 0x10;
	}
				else {
				askbuf[3] = 20;
			}



		askbuf[4] = 0xff;
	askbuf[5] = 0x00;

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

	rece_end = 2;
	com_transfer(myportno, 8, askbuf);
	draw_send_message(myportno, askbuf, 8, POLL_SEND_COLOR);
}

void CMod1::send_yk_exec()

⌨️ 快捷键说明

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