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

📄 modbus.cpp

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

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

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

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

void CModbus::send_yk_esc()
{
	INT16U temp;

	askbuf[0] = port_yk_info.dev_no;
	askbuf[1] = SET_COIL;
	askbuf[2] = 0x00;
	askbuf[3] = 0x0e;
	askbuf[4] = 0xff;
	askbuf[5] = 0x00;

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

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

void CModbus::send_ds()
{
	int remark;
	sclock sclk;
	askbuf[0] = 0x00;
	askbuf[1] = 0x15;
	askbuf[2] = 0x0f;
	askbuf[3] = 0x06;
	askbuf[4] = 0x00;
	askbuf[5] = 0x07;
	askbuf[6] = 0x00;
	askbuf[7] = 0x00;
	askbuf[8] = 0x00;
	askbuf[9] = 0x04;

	GetClock(&sclk);

	remark = 1000*sclk.second + sclk.msecond;
	askbuf[10] = sclk.year%100&0xff;
	askbuf[11] = sclk.month&0xff;
	askbuf[12] = sclk.day&0xff;
	askbuf[13] = sclk.hour&0xff;
	askbuf[14] = sclk.minute&0xff;
	askbuf[15] = (remark>>8)&0xff;
	askbuf[16] = remark&0xff;

	int	temp = crc16(askbuf, 17);

	askbuf[17] = (temp >> 8) & 0xff;
	askbuf[18] = temp & 0xff;

 //	currentCMD = 0x15;
		com_transfer(myportno, 19, askbuf);
	draw_send_message(myportno, askbuf, 19, POLL_SEND_COLOR);
}

void CModbus::send_soe_length()
{
	INT16U temp;

	askbuf[0] = station_add;
	askbuf[1] = 0x14;
	askbuf[2] = 0x07;
	askbuf[3] = 0x06;
	askbuf[4] = 0x00;
	askbuf[5] = 0x02;
	askbuf[6] = 0x00;
	askbuf[7] = 0x00;
	askbuf[8] = 0x00;
	askbuf[9] = 0x01;

	temp = crc16(askbuf, 10);
	askbuf[10] = (temp >> 8) & 0xff;
	askbuf[11] = temp & 0xff;
	currentCMD = 0x14;

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

void CModbus::processSOE_LENGTH(INT8U *buf)
{
	soelength = ((buf[5] << 8)&0xff00) + buf[6];
	if(soelength >=17)
			soelength = 17;
}

void CModbus::send_soe_clear()
{
	INT16U temp;

	askbuf[0] = station_add;
	askbuf[1] = 0x15;
	askbuf[2] = 0x09;
	askbuf[3] = 0x06;
	askbuf[4] = 0x00;
		askbuf[5] = 0x02;
		askbuf[6] = 0x00;
	askbuf[7] = 0x02;
	askbuf[8] = 0x00;
	askbuf[9] = 0x01;
	askbuf[10] = 0xff;
	askbuf[11] = 0x00;

	temp = crc16(askbuf, 12);
	askbuf[12] = (temp >> 8) & 0xff;
	askbuf[13] = temp & 0xff;
	currentCMD = 0x15;

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

void CModbus::send_soe()//qurey_soe!
{
	INT16U temp;
		soelength = 7*(soelength);
	askbuf[0] = station_add;
	askbuf[1] = 0x14;
	askbuf[2] = 0x07;
	askbuf[3] = 0x06;
	askbuf[4] = 0x00;
		askbuf[5] = 0x02;
		askbuf[6] = 0x00;
	askbuf[7] = 0x03;
	askbuf[8] = INT8U((soelength >> 8)&0x00ff);
	askbuf[9] = INT8U(soelength & 0x00ff);
	temp = crc16(askbuf, 10);
	askbuf[10] = (temp >> 8) & 0xff;
	askbuf[11] = temp & 0xff;
	currentCMD = 0x14;

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

void CModbus::processSOE(INT8U *buf)
{
 //	if(send_add == 0x0d)
	//	send_soe_clear();
		int i, j;
	soe_record_t temp_soe;
	sclock sclk;
	INT16U temp;

	INT16U no = soelength;
		for (i = 0; i < no; i++) {
		for (j = 0; j < _TotalYx; j++) {
			if( ((myportno == (yx_define[j].portno - 1)) && (buf[5] == yx_define[j].devno)
				&& (yx_define[j].channel == buf[i * 14 + 6])&&(yx_define[j].type == buf[i*14+8]))) {

				temp_soe.status_switch_no = j & 0xfff;
				if (buf[i * 14 + 5] == 0x06)
					temp_soe.status_switch_no |= 0x8000;
				else if(
					(buf[i * 14 + 5] == 0x07)||
					( ((buf[i * 14 + 4] == 11) || (buf[i * 14 + 4] == 11) ||(buf[i * 14 + 4] == 11))&&
										 (buf[i * 7 + 5] == 0x05) )
					 )
				temp_soe.status_switch_no &= 0x7fff;
				else return;

				GetClock(&sclk);
				temp = ((buf[i * 7 + 13] << 8) & 0xff00) + buf[i * 7 + 14];

			/*	temp_soe.time.date = (((sclk.year % 100) << 9) & 0xfe00) +
								((sclk.month << 5) & 0x01e0) + (sclk.day & 0x001f);
				temp_soe.time.time = ((sclk.weekday << 12) & 0xf000) +
								((buf[i * 7 + 10] << 7) & 0x0f80) + (buf[i * 7 + 11] & 0x007f);
				temp_soe.time.msec = ((buf[i * 7 + 12] << 10) & 0xfc00) +
								(temp & 0x03ff);*/

			temp_soe.day = sclk.day;
			temp_soe.hour = buf[i * 14 + 11];
			temp_soe.minute = buf[i * 14 + 12];
			temp_soe.second = temp / 1000;
			temp_soe.msecond = temp % 1000;
				insert_soe(&temp_soe);
			}
		}
	}

}



void CModbus::master_send()
{
	int i;

	if (!is_port_yk_doing(myportno)) yk_flag = FALSE;

	if (yk_flag) return;

	if (port_yk_info.Y_PRESET) {
		if (port_yk_info.yk_des == myportno) {
			port_yk_info.Y_PRESET = FALSE;
			yk_flag = TRUE;
			send_yk_preset();
			send_add = port_yk_info.dev_no;
			rece_end = 2;
			return;
		}
	}

	if (port_yk_info.Y_EXEC) {
		if (port_yk_info.yk_des == myportno) {
			port_yk_info.Y_EXEC = FALSE;
			yk_flag = FALSE;
			send_yk_exec();
			send_add = port_yk_info.dev_no;
			rece_end = 2;
			return;
		}
	}

	if (port_yk_info.Y_ESC) {
		if (port_yk_info.yk_des == myportno) {
			port_yk_info.Y_ESC = FALSE;
			yk_flag = FALSE;//bu zai zuo
			send_yk_esc();
			send_add = port_yk_info.dev_no;
			rece_end = 2;
			return;
		}
	}


	if (send_cycle >= 4000) {

	send_cycle = 1;
	}



 if (send_cycle % 20 == 0) {
		send_ds();//guangbo
		send_cycle++;
		rece_end = 0;
		return;
}

switch (send_state[station_add]) {
	case 0: case 3://case 6:
		send_yx();
			++send_state[station_add];
			send_add = station_add;
			change_addr();
			break;

	case 2:
			 send_yc();
			 ++send_state[station_add];
			 send_add = station_add;
			 change_addr();
			 break;
	case 1:case 4://case 7:
			 send_leds();
			 ++send_state[station_add];
			 send_add = station_add;
			 change_addr();
			 break;

	 case 5:
			send_soe_length();
			++send_state[station_add] ;
			send_add = station_add;
			break;

	case 6:
			 if(soelength>0)
			 send_soe();
			 ++send_state[station_add];
		 //	 else( send_state[station_add] = 0;

			 send_add = station_add;
			 break;
	case 7:
			 if(soelength>0)
			 send_soe_clear();
			 send_state[station_add] = 0;
			 send_add = station_add;
			 change_addr();


	}

}




void CModbus::process(INT8U *buf)
{
	switch (buf[1]) {
			case 0x02:
			processREAD_DI(buf);
			break;
		case 0x03:
			processREADWORD(buf);
			break;
		case 0x04:
			processREAD_AI(buf);
			break;
		case 0x05:
			processSET_COIL(buf);
				break;
		case 0x14:
			if (buf[2] == 0x04)
			{
			processSOE_LENGTH(buf);
			break;
			}
		 else	if(soelength==0)
			{
			break;
			}
		 else
		 {
		 //	send_soe_clear();
			processSOE(buf);
			}
				break;
		case 0x15:
				break;
			}
}

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

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

	INT16U framelen = recbuf[2] + 5;
	if (recbuf[0] == send_add&&recbuf[1]==0x05){
			read_com_rece_nbytes(recbuf, 8 , myportno);
			process(recbuf);
			}
	else if (((recbuf[0] == send_add)||(recbuf[0] == 0x00)) && recbuf[1] == currentCMD) {
			if (rece_len >= framelen) {
		 //	draw_rece_message(myportno,recbuf,framelen,POLL_RECE_COLOR);
			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;
	 //	 if((recbuf[1]==0x14)&&(recbuf[2]!=0x04))
	 //	 send_soe_clear();
		 process(recbuf);
			}
		}
	}
	else del_com_rece_one(myportno);
	}


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

	if (port_timeout[myportno] >=4) {
		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;
	}
}

⌨️ 快捷键说明

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