?? mb_pfunction.c
字號:
else
Response->ExceptionCode = 0x02;
}
else
Response->ExceptionCode = 0x03;
}
#endif
#if READ_DIS_INPUT_EN >0
/****************************************************************************************/
// 函數名稱:FReadDiscreteInputs
// 輸入參數:Response
// 輸出參數:無
// 功能描述:處理讀離散輸入指令02
/***************************************************************************************/
void FReadDiscreteInputs(PDU_RESPONSE *Response)
{
uint8 * PDUPtr;
uint16 StAddr ,Quantity;
// 從請求PDU中提取相關參數
PDUPtr = Response->PDUDataPtr;
StAddr = PDUPtr[1]<<8|PDUPtr[2];
Quantity = PDUPtr[3]<<8|PDUPtr[4];
Response->PDUByteLength = 2 + Quantity/8+((Quantity%8)?1:0) ; // 正常返回的數據長度
if((Quantity>=0x001)&&(Quantity<=0x07d0))
{
if((StAddr<END_DISC_INPUT_ADDR)&&((StAddr+Quantity)<=END_DISC_INPUT_ADDR))
{
if(ReadDiscreteInputs(PDUPtr+2,StAddr,Quantity))
{
*(PDUPtr+1) = Quantity/8+(Quantity%8)?1:0; // 字節數
Response->ExceptionCode = 0x00;
}
else
Response->ExceptionCode = 0x04;
}
else
Response->ExceptionCode = 0x02;
}
else
Response->ExceptionCode = 0x03;
}
#endif
#if READ_HOLD_REG_EN >0
/****************************************************************************************/
// 函數名稱:FReadHoldingReg
// 輸入參數:Response
// 輸出參數:無
// 功能描述:處理讀保持寄存器指令03
/****************************************************************************************/
void FReadHoldingReg(PDU_RESPONSE *Response)
{
uint8 * PDUPtr;
uint16 StAddr ,Quantity;
// 從請求PDU中提取相關參數
PDUPtr = Response->PDUDataPtr;
StAddr = PDUPtr[1]<<8|PDUPtr[2];
Quantity = PDUPtr[3]<<8|PDUPtr[4];
Response->PDUByteLength = 2 + Quantity*2 ; // 正常返回的數據長度
if((Quantity>=0x001)&&(Quantity<=0x007d))
{
if((StAddr<END_HOLDING_REG_ADDR)&&((StAddr+Quantity)<=END_HOLDING_REG_ADDR))
{
if(ReadHoldingReg(PDUPtr+2,StAddr,Quantity))
{
*(PDUPtr+1) = Quantity*2;
Response->ExceptionCode = 0x00;
}
else
Response->ExceptionCode = 0x04;
}
else
Response->ExceptionCode = 0x02;
}
else
Response->ExceptionCode = 0x03;
}
#endif
#if READ_INPUT_REG_EN >0
/****************************************************************************************/
// 函數名稱:FReadInputReg
// 輸入參數:Response
// 輸出參數:無
// 功能描述:處理讀輸入寄存器指令04
/****************************************************************************************/
void FReadInputReg(PDU_RESPONSE *Response)
{
uint8 * PDUPtr;
uint16 StAddr ,Quantity;
// 從請求PDU中提取相關參數
PDUPtr = Response->PDUDataPtr;
StAddr = PDUPtr[1]<<8|PDUPtr[2];
Quantity = PDUPtr[3]<<8|PDUPtr[4];
Response->PDUByteLength = 2 + Quantity*2 ; // 正常返回的數據長度
if((Quantity>=0x001)&&(Quantity<=0x007d))
{
if((StAddr<END_INPUT_REG_ADDR) && ((StAddr+Quantity)<=END_INPUT_REG_ADDR))
{
if(ReadInputReg(PDUPtr+2,StAddr,Quantity))
{
*(PDUPtr+1) = Quantity*2;
Response->ExceptionCode = 0x00;
}
else
Response->ExceptionCode = 0x04;
}
else
Response->ExceptionCode = 0x02;
}
else
Response->ExceptionCode = 0x03;
}
#endif
#if WRITE_SING_COIL_EN >0
/****************************************************************************************/
// 函數名稱:FWriteSingleCoil
// 輸入參數:Response
// 輸出參數:無
// 功能描述:處理寫單線圈指令05
/****************************************************************************************/
void FWriteSingleCoil(PDU_RESPONSE *Response)
{
uint8 * PDUPtr;
uint16 OutputAddr ,OutputValue;
// 從請求PDU中提取相關參數
PDUPtr = Response->PDUDataPtr;
OutputAddr = PDUPtr[1]<<8|PDUPtr[2];
OutputValue = PDUPtr[3]<<8|PDUPtr[4];
Response->PDUByteLength = 5 ; // 正常返回的數據長度
if((OutputValue==0x0000)||(OutputValue==0xff00))
{
if( OutputAddr < END_COILS_ADDR )
{
if(WriteSingleCoil(OutputAddr,OutputValue))
{
*(PDUPtr+1) = OutputAddr>>8;
*(PDUPtr+2) = OutputAddr;
*(PDUPtr+3) = OutputValue>>8;
*(PDUPtr+4) = OutputValue;
Response->ExceptionCode = 0x00;
}
else
Response->ExceptionCode = 0x04;
}
else
Response->ExceptionCode = 0x02;
}
else
Response->ExceptionCode = 0x03;
}
#endif
#if WRITE_SING_REG_EN >0
/****************************************************************************************/
// 函數名稱:FWriteSingleReg
// 輸入參數:Response
// 輸出參數:無
// 功能描述:處理寫單線圈指令06
/****************************************************************************************/
void FWriteSingleReg(PDU_RESPONSE *Response)
{
uint8 * PDUPtr;
uint16 OutputAddr ,OutputValue;
// 從請求PDU中提取相關參數
PDUPtr = Response->PDUDataPtr;
OutputAddr = PDUPtr[1]<<8|PDUPtr[2];
OutputValue = PDUPtr[3]<<8|PDUPtr[4];
Response->PDUByteLength = 2 + 5 ; // 正常返回的數據長度
if(OutputValue<=0xffff)
{
if(OutputAddr<END_HOLDING_REG_ADDR)
{
if(WriteHoldingReg(&PDUPtr[3],OutputAddr,1))
{
*(PDUPtr+1) = OutputAddr>>8;
*(PDUPtr+2) = OutputAddr;
*(PDUPtr+3) = OutputValue>>8;
*(PDUPtr+4) = OutputValue;
Response->ExceptionCode = 0x00;
}
else
Response->ExceptionCode = 0x04;
}
else
Response->ExceptionCode = 0x02;
}
else
Response->ExceptionCode = 0x03;
}
#endif
#if WRITE_MULT_COIL_EN >0
/****************************************************************************************/
// 函數名稱:FWriteMultipleCoils
// 輸入參數:Response
// 輸出參數:無
// 功能描述:處理寫多線圈指令15
/****************************************************************************************/
void FWriteMultipleCoils(PDU_RESPONSE *Response)
{
uint8 * PDUPtr;
uint16 StAddr ,Quantity;
uint8 ByteCount;
// 從請求PDU中提取相關參數
PDUPtr = Response->PDUDataPtr;
StAddr = PDUPtr[1]<<8|PDUPtr[2];
Quantity = PDUPtr[3]<<8|PDUPtr[4];
ByteCount = PDUPtr[5];
Response->PDUByteLength = 5 ; // 正常返回的數據長度
if((Quantity>=0x001)&&(Quantity<=0x07d0)&&(ByteCount==(Quantity/8+((Quantity%8)?1:0))))
{
if((StAddr<END_COILS_ADDR)&&((StAddr+Quantity)<=(END_COILS_ADDR+1)))
{
if(WriteMultipleCoils(PDUPtr+6,StAddr,Quantity))
{
*(PDUPtr+1) = StAddr>>8;
*(PDUPtr+2) = StAddr;
*(PDUPtr+3) = Quantity>>8;
*(PDUPtr+4) = Quantity;
Response->ExceptionCode = 0x00;
}
else
Response->ExceptionCode = 0x04;
}
else
Response->ExceptionCode = 0x02;
}
else
Response->ExceptionCode = 0x03;
}
#endif
#if WRITE_MULT_REG_EN >0
/****************************************************************************************/
// 函數名稱:FWriteMultipleReg
// 輸入參數:Response
// 輸出參數:無
// 功能描述:處理寫多寄存器指令16
/****************************************************************************************/
void FWriteMultipleReg(PDU_RESPONSE *Response)
{
uint8 * PDUPtr;
uint16 StAddr ,Quantity;
uint8 ByteCount;
// 從請求PDU中提取相關參數
PDUPtr = Response->PDUDataPtr;
StAddr = PDUPtr[1]<<8|PDUPtr[2];
Quantity = PDUPtr[3]<<8|PDUPtr[4];
ByteCount = PDUPtr[5];
Response->PDUByteLength = 5 ; // 正常返回的數據長度
if((Quantity>=0x001)&&(Quantity<=0x007d)&&(ByteCount==(Quantity*2)))
{
if((StAddr<END_HOLDING_REG_ADDR) && ((StAddr+Quantity)<=(END_HOLDING_REG_ADDR+1)))
{
if(WriteHoldingReg(PDUPtr+6,StAddr,Quantity))
{
*(PDUPtr+1) = StAddr>>8;
*(PDUPtr+2) = StAddr;
*(PDUPtr+3) = Quantity>>8;
*(PDUPtr+4) = Quantity;
Response->ExceptionCode = 0x00;
}
else
Response->ExceptionCode = 0x04;
}
else
Response->ExceptionCode = 0x02;
}
else
Response->ExceptionCode = 0x03;
}
#endif
#if MASK_WRITE_REG_EN >0
/****************************************************************************************/
// 函數名稱:FMaskWriteReg
// 輸入參數:Response
// 輸出參數:無
// 功能描述:處理屏蔽寄存器指令22
/****************************************************************************************/
void FMaskWriteReg(PDU_RESPONSE *Response)
{
uint16 RefeAddr,And_Mask,Or_Mask,RegValue;
uint8 *PDUPtr;
uint8 ValueInByte[2];
PDUPtr = Response->PDUDataPtr;
RefeAddr = (PDUPtr[1]<<8)|(PDUPtr[2]);
And_Mask = (PDUPtr[3]<<8)|(PDUPtr[4]);
Or_Mask = (PDUPtr[5]<<8)|(PDUPtr[6]);
Response->PDUByteLength = 7;
if(RefeAddr<END_HOLDING_REG_ADDR)
{
if(ReadHoldingReg(ValueInByte,RefeAddr,1))
{
RegValue = (ValueInByte[0]<<8)|(ValueInByte[1]);
RegValue = (RegValue & And_Mask)|(Or_Mask & (~And_Mask));
ValueInByte[0] = RegValue >> 8;
ValueInByte[1] = RegValue ;
if(WriteHoldingReg(ValueInByte,RefeAddr,1))
{
*(PDUPtr+1) = RefeAddr>>8;
*(PDUPtr+2) = RefeAddr;
*(PDUPtr+3) = And_Mask>>8;
*(PDUPtr+4) = And_Mask;
*(PDUPtr+5) = Or_Mask>>8;
*(PDUPtr+6) = Or_Mask;
Response->ExceptionCode = 0x00;
}
else
Response->ExceptionCode = 0x04;
}
else
Response->ExceptionCode = 0x03;
}
else
Response->ExceptionCode = 0x02;
}
#endif
#if READ_WRITE_REG_EN > 0
/****************************************************************************************/
// 函數名稱:FRWMultipleReg
// 輸入參數:Response
// 輸出參數:無
// 功能描述:處理讀寫多寄存器指令23
/****************************************************************************************/
void FRWMultipleReg(PDU_RESPONSE *Response)
{
uint16 ReadStaAddr, ReadQuantity;
uint16 WriteStaAddr,WriteQuantity;
uint8 WriteBytes;
uint8 *WriteValuePtr;
uint8 *PDUPtr;
PDUPtr = Response->PDUDataPtr;
ReadStaAddr = (PDUPtr[1]<<8)|(PDUPtr[2]);
ReadQuantity = (PDUPtr[3]<<8)|(PDUPtr[4]);
WriteStaAddr = (PDUPtr[5]<<8)|(PDUPtr[6]);
WriteQuantity = (PDUPtr[7]<<8)|(PDUPtr[8]);
WriteBytes = PDUPtr[9];
WriteValuePtr = &PDUPtr[10];
Response->PDUByteLength = ReadQuantity*2 + 2;
if((ReadQuantity>=0x001)&&(ReadQuantity<=0x007d)&& // 讀寄存器的數量是否在有效范圍內
(WriteQuantity>=0x001)&&(WriteQuantity<=0x0079)&& // 寫寄存器的數量是否在有效范圍內
(WriteBytes==(WriteQuantity*2))) // 接收到的字節數是否與寫入的寄存器數量一至
{
if((ReadStaAddr<END_HOLDING_REG_ADDR)&&((ReadStaAddr+ReadQuantity)<=END_HOLDING_REG_ADDR)&&
(WriteStaAddr<END_HOLDING_REG_ADDR)&&((WriteStaAddr+WriteQuantity)<=END_HOLDING_REG_ADDR))
{
if(WriteHoldingReg(WriteValuePtr,WriteStaAddr,WriteQuantity)) // 寫寄存器
{
if(ReadHoldingReg(PDUPtr+2,ReadStaAddr,ReadQuantity)) // 讀寄存器
{
*(PDUPtr+1) = ReadQuantity*2;
Response->ExceptionCode = 0x00;
}
else
Response->ExceptionCode = 0x04;
}
else
Response->ExceptionCode = 0x04;
}
else
Response->ExceptionCode = 0x02;
}
else
Response->ExceptionCode = 0x03;
}
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -