int prase_modbus_protocol(char * pbuf,unsigned int len)
{
	int ret = 0; //返回应答的数据长度
	modbus_tcp_head * phead = (modbus_tcp_head *)pbuf;

	if(DUMP_DATA_INFO)dumpdata(pbuf,len);

	//判断是否modbus/tcp协议
	if(len < sizeof(modbus_tcp_head)) {
		DEBUGMSG(THISERROR,("modbus/tcp data len error!\r\n"));
		return 0;
	}
	//获取数据长度
	len = phead->lengthh;len <<= 8;
	len |= phead->lengthl;
	switch(phead->function_code) {
		case 0x01:
		case 0x02:
		{
			//modbus_type_fc1_cmd * pcmd = (modbus_type_fc1_cmd *)GET_MODBUS_DATA(phead);
			DEBUGMSG(THISINFO,("modbus 0x01:\r\n"));
			if(len < 4) {
				DEBUGMSG(THISERROR,("len < 4 error\r\n"));
				break;
			} else {
				if(phead->function_code == 0x01) {
				    len = ReadCoilStatus(GET_MODBUS_DATA(phead));
				} else if(phead->function_code == 0x02) {
					len = ReadInputDiscretes(GET_MODBUS_DATA(phead));
				}
				if(len > 0) {
					DEBUGMSG(THISINFO,("ReadCoilStatus Ok ret len(%d)\r\n",len));
					phead->lengthl = len & 0xFF;
					phead->lengthh = len >> 8;
					return len + sizeof(modbus_tcp_head) - 2;
				} else {
					DEBUGMSG(THISERROR,("ReadCoilStatus Ko\r\n"));
				}
			}
		}
Ejemplo n.º 2
0
//* FC02 This command requests the ON/OFF status of discrete inputs///
void ReadInputStatus(void)
{
ReadCoilStatus(Input_Registers);
}