1、【修改】FreeModbus主机所有请求方法,增加了请求响应成功、请求响应超时、接收数据出错及功能执行出错的返回值,目前主机请求方法为阻塞方法,直到有结果才会返回,保证了用户使用的便捷性。
Signed-off-by: armink <armink.ztl@gmail.com>
This commit is contained in:
parent
d9d5cae9bb
commit
7513daf118
|
@ -38,8 +38,8 @@
|
|||
|
||||
#define DELAY_SYS_RUN_LED DELAY_MS(500) //系统指示灯延时500ms
|
||||
#define DELAY_SYS_SLEEP_LED DELAY_MS(1000) //系统指示灯延时1s
|
||||
#define DELAY_MB_SLAVE_POLL DELAY_MS(10) //Modbus从机轮训时间
|
||||
#define DELAY_MB_MASTER_POLL DELAY_MS(10) //Modbus主机轮训时间
|
||||
#define DELAY_MB_SLAVE_POLL DELAY_MS(1) //Modbus从机轮训时间
|
||||
#define DELAY_MB_MASTER_POLL DELAY_MS(1) //Modbus主机轮训时间
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -33,6 +33,8 @@ struct rt_thread thread_ModbusMasterPoll;
|
|||
//******************************************************************
|
||||
void thread_entry_SysMonitor(void* parameter)
|
||||
{
|
||||
eMBMasterReqErrCode errorCode = MB_MRE_NO_ERR;
|
||||
uint16_t errorCount = 0;
|
||||
while (1)
|
||||
{
|
||||
cpu_usage_get(&CpuUsageMajor, &CpuUsageMinor);
|
||||
|
@ -49,15 +51,19 @@ void thread_entry_SysMonitor(void* parameter)
|
|||
usModbusUserData[0] = (USHORT)(rt_tick_get()/10);
|
||||
usModbusUserData[1] = (USHORT)(rt_tick_get()%10);
|
||||
ucModbusUserData[0] = 0x1F;
|
||||
eMBMasterReqReadDiscreteInputs(1,3,8,RT_WAITING_FOREVER);
|
||||
// eMBMasterReqWriteMultipleCoils(1,3,5,ucModbusUserData,RT_WAITING_FOREVER);
|
||||
// eMBMasterReqWriteCoil(1,8,0xFF00,RT_WAITING_FOREVER);
|
||||
// eMBMasterReqReadCoils(1,3,8,RT_WAITING_FOREVER);
|
||||
// eMBMasterReqReadInputRegister(1,3,2,RT_WAITING_FOREVER);
|
||||
// eMBMasterReqWriteHoldingRegister(1,3,usModbusUserData[0],RT_WAITING_FOREVER);
|
||||
// eMBMasterReqWriteMultipleHoldingRegister(1,3,2,usModbusUserData,RT_WAITING_FOREVER);
|
||||
// eMBMasterReqReadHoldingRegister(1,3,2,RT_WAITING_FOREVER);
|
||||
// eMBMasterReqReadWriteMultipleHoldingRegister(1,3,2,usModbusUserData,5,2,RT_WAITING_FOREVER);
|
||||
// errorCode = eMBMasterReqReadDiscreteInputs(1,3,8,RT_WAITING_FOREVER);
|
||||
// errorCode = eMBMasterReqWriteMultipleCoils(1,3,5,ucModbusUserData,RT_WAITING_FOREVER);
|
||||
errorCode = eMBMasterReqWriteCoil(1,8,0xFF00,RT_WAITING_FOREVER);
|
||||
// errorCode = eMBMasterReqReadCoils(1,3,8,RT_WAITING_FOREVER);
|
||||
// errorCode = eMBMasterReqReadInputRegister(1,3,2,RT_WAITING_FOREVER);
|
||||
// errorCode = eMBMasterReqWriteHoldingRegister(1,3,usModbusUserData[0],RT_WAITING_FOREVER);
|
||||
// errorCode = eMBMasterReqWriteMultipleHoldingRegister(1,3,2,usModbusUserData,RT_WAITING_FOREVER);
|
||||
// errorCode = eMBMasterReqReadHoldingRegister(1,3,2,RT_WAITING_FOREVER);
|
||||
// errorCode = eMBMasterReqReadWriteMultipleHoldingRegister(1,3,2,usModbusUserData,5,2,RT_WAITING_FOREVER);
|
||||
//¼Ç¼³ö´í´ÎÊý
|
||||
if (errorCode != MB_MRE_NO_ERR) {
|
||||
errorCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -103,6 +103,8 @@ eMBMasterReqReadCoils( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usNCoils ,LONG
|
|||
ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF + 1] = usNCoils;
|
||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
||||
eErrStatus = vMBMasterWaitRequestFinish( );
|
||||
|
||||
}
|
||||
return eErrStatus;
|
||||
}
|
||||
|
@ -203,6 +205,7 @@ eMBMasterReqWriteCoil( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usCoilData, LO
|
|||
ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF + 1] = usCoilData;
|
||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_SIZE );
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
||||
eErrStatus = vMBMasterWaitRequestFinish( );
|
||||
}
|
||||
return eErrStatus;
|
||||
}
|
||||
|
@ -312,6 +315,7 @@ eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr,
|
|||
}
|
||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_MUL_SIZE_MIN + ucByteCount );
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
||||
eErrStatus = vMBMasterWaitRequestFinish( );
|
||||
}
|
||||
return eErrStatus;
|
||||
}
|
||||
|
|
|
@ -88,6 +88,7 @@ eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT u
|
|||
ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF + 1] = usNDiscreteIn;
|
||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
||||
eErrStatus = vMBMasterWaitRequestFinish( );
|
||||
}
|
||||
return eErrStatus;
|
||||
}
|
||||
|
|
|
@ -115,6 +115,7 @@ eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usRe
|
|||
ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF + 1] = usRegData ;
|
||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_SIZE );
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
||||
eErrStatus = vMBMasterWaitRequestFinish( );
|
||||
}
|
||||
return eErrStatus;
|
||||
}
|
||||
|
@ -192,6 +193,7 @@ eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr,
|
|||
}
|
||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_MUL_SIZE_MIN + 2*usNRegs );
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
||||
eErrStatus = vMBMasterWaitRequestFinish( );
|
||||
}
|
||||
return eErrStatus;
|
||||
}
|
||||
|
@ -277,6 +279,7 @@ eMBMasterReqReadHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRe
|
|||
ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs;
|
||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
||||
eErrStatus = vMBMasterWaitRequestFinish( );
|
||||
}
|
||||
return eErrStatus;
|
||||
}
|
||||
|
@ -377,6 +380,7 @@ eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,
|
|||
}
|
||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READWRITE_SIZE_MIN + 2*usNWriteRegs );
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
||||
eErrStatus = vMBMasterWaitRequestFinish( );
|
||||
}
|
||||
return eErrStatus;
|
||||
}
|
||||
|
|
|
@ -88,6 +88,7 @@ eMBMasterReqReadInputRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs
|
|||
ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs;
|
||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
|
||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
||||
eErrStatus = vMBMasterWaitRequestFinish( );
|
||||
}
|
||||
return eErrStatus;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue