觸摸屏和單片機進行modbus通訊,觸摸屏需要設置為主機,單片機為從機,modbusRTU slave 源碼需要植入單片機,希望可以幫助到大家。
全部資料下載:
ModbusSlave.zip
(13.59 KB, 下載次數: 249)
2016-5-20 19:07 上傳
點擊文件名下載附件
modbusslave文件源碼
- /****************************************Copyright (c)**************************************************
- ** 廣州致遠電子有限公司
- **
- **--------------File Info-------------------------------------------------------------------------------
- ** File name: MB_PFunction.c
- ** Last modified Date: 2005-01-11
- ** Last Version: 1.0
- ** Descriptions:
- **
- **------------------------------------------------------------------------------------------------------
- ** Created by: ZhouLishan
- ** Created date: 2005-01-11
- ** Version: 1.0
- ** Descriptions:
- **
- **------------------------------------------------------------------------------------------------------
- ** Modified by:
- ** Modified date:
- ** Version:
- ** Descriptions:
- **
- ********************************************************************************************************/
- #include "config.h"
- #include "modbus.h"
- //**************************************************
- // 函數名稱:MB_GetDiscrete
- // 輸入參數:Address,線圈地址
- // 輸出參數:返回線圈值(0\1)
- // 功能描述:獲取離散值函數,訪函數由用戶編寫
- //**************************************************
- uint8_t MB_GetDiscrete(uint16_t Address)
- {
- if(Address/2)
- return 1;
- else
- return 0;
- }
- //**************************************************
- // 函數名稱:MB_GetCoils
- // 輸入參數:Address,線圈地址
- // 輸出參數:返回線圈值(0\1)
- // 功能描述:獲取線圈值函數,訪函數由用戶編寫
- //**************************************************
- uint8_t MB_GetCoils(uint16_t Address)
- {
- if(Address/2)
- return 1;
- else
- return 0;
- }
- //**************************************************
- // 函數名稱:MB_SetCoil
- // 輸入參數:Address,線圈地址
- // CoilValue,線圈值(0\1)
- // 輸出參數:返回寄存器值
- // 功能描述:設置線圈值函數,訪函數由用戶編寫
- //**************************************************
- uint8_t MB_SetCoil(uint16_t Address,uint8_t CoilValue)
- {
- return CoilValue;
- }
- //**************************************************
- // 函數名稱:MB_GetInputRegValue
- // 輸入參數:Address,寄存器地址
- // 輸出參數:返回寄存器值
- // 功能描述:讀輸入寄存器值函數,訪函數由用戶編寫
- //**************************************************
- uint16_t MB_GetInputRegValue(uint16_t Address)
- {
- return Address;
- }
- //**************************************************
- // 函數名稱:MB_GetRegValue
- // 輸入參數:Address,寄存器地址
- // 輸出參數:返回寄存器值
- // 功能描述:讀保持寄存器值函數,訪函數由用戶編寫
- //**************************************************
- uint16_t MB_GetRegValue(uint16_t Address)
- {
- return Address;
- }
- //**************************************************
- // 函數名稱:MB_SetRegValue
- // 輸入參數:Address,寄存器地址
- // Value,寫入的值
- // 輸出參數:無
- // 功能描述:寫保持寄存器值函數,訪函數由用戶編寫
- //**************************************************
- void MB_SetRegValue(uint16_t Address,uint16_t Value)
- {
- }
- /****************************************************************************************/
- // 函數名稱:FReadCoils
- // 輸入參數:Response
- // 輸出參數:無
- // 功能描述:處理讀單線圈指令01
- /***************************************************************************************/
- void FReadCoils(PDU_RESPONSE *Response);
- /****************************************************************************************/
- // 函數名稱:FReadDiscreteInputs
- // 輸入參數:Response
- // 輸出參數:無
- // 功能描述:處理讀離散輸入指令02
- /***************************************************************************************/
- void FReadDiscreteInputs(PDU_RESPONSE *Response);
- /****************************************************************************************/
- // 函數名稱:FReadHoldingReg
- // 輸入參數:Response
- // 輸出參數:無
- // 功能描述:處理讀保持寄存器指令03
- /****************************************************************************************/
- void FReadHoldingReg(PDU_RESPONSE *Response);
- /****************************************************************************************/
- // 函數名稱:FReadInputReg
- // 輸入參數:Response
- // 輸出參數:無
- // 功能描述:處理讀輸入寄存器指令04
- /****************************************************************************************/
- void FReadInputReg(PDU_RESPONSE *Response);
- /****************************************************************************************/
- // 函數名稱:FWriteSingleCoil
- // 輸入參數:Response
- // 輸出參數:無
- // 功能描述:處理寫單線圈指令05
- /****************************************************************************************/
- void FWriteSingleCoil(PDU_RESPONSE *Response);
- /****************************************************************************************/
- // 函數名稱:FWriteSingleReg
- // 輸入參數:Response
- // 輸出參數:無
- // 功能描述:處理寫單線圈指令06
- /****************************************************************************************/
- void FWriteSingleReg(PDU_RESPONSE *Response);
- /****************************************************************************************/
- // 函數名稱:FWriteMultipleCoils
- // 輸入參數:Response
- // 輸出參數:無
- // 功能描述:處理寫多線圈指令15
- /****************************************************************************************/
- void FWriteMultipleCoils(PDU_RESPONSE *Response);
- /****************************************************************************************/
- // 函數名稱:FWriteMultipleReg
- // 輸入參數:Response
- // 輸出參數:無
- // 功能描述:處理寫多寄存器指令16
- /****************************************************************************************/
- void FWriteMultipleReg(PDU_RESPONSE *Response);
- /****************************************************************************************/
- // 函數名稱:FMaskWriteReg
- // 輸入參數:Response
- // 輸出參數:無
- // 功能描述:處理屏蔽寄存器指令22
- /****************************************************************************************/
- void FMaskWriteReg(PDU_RESPONSE *Response);
- /****************************************************************************************/
- // 函數名稱:FRWMultipleReg
- // 輸入參數:Response
- // 輸出參數:無
- // 功能描述:處理讀寫多寄存器指令23
- /****************************************************************************************/
- void FRWMultipleReg(PDU_RESPONSE *Response);
- void FTest(PDU_RESPONSE *Response){}
- AT_CODE FUNCTION_ARRAY FCodeHandleArray[MAX_FUNCTION]={
- #if READ_COILS_EN > 0
- {0x01,FReadCoils}, // 讀線圈
- #endif
- #if READ_DIS_INPUT_EN > 0
- {0x02,FReadDiscreteInputs}, // 讀離散量輸入
- #endif
- #if READ_HOLD_REG_EN > 0
- {0x03,FReadHoldingReg}, // 讀保持寄存器
- #endif
- #if READ_INPUT_REG_EN > 0
- {0x04,FReadInputReg}, // 讀輸入寄存器
- #endif
- #if WRITE_SING_COIL_EN > 0
- {0x05,FWriteSingleCoil}, // 寫單個線圈
- #endif
- #if WRITE_SING_REG_EN > 0
- {0x06,FWriteSingleReg}, // 寫單個寄存器
- #endif
- #if WRITE_MULT_COIL_EN > 0
- {0x0f,FWriteMultipleCoils}, // 寫多個線圈
- #endif
- #if WRITE_MULT_REG_EN > 0
- {0x10,FWriteMultipleReg}, // 寫多個寄存器
- #endif
- #if MASK_WRITE_REG_EN > 0
- {0x16,FMaskWriteReg}, // 處理屏蔽寄存器指令
- #endif
- #if READ_WRITE_REG_EN > 0
- {0x17,FRWMultipleReg}, // 讀寫多個寄存器
- #endif
- {0x00,FTest} // 測試功能代碼
- };
- #if READ_DIS_INPUT_EN
- //************************************************************************************
- // 函數名稱:ReadDiscreteInputs
- // 輸入參數:CoilsDataPtr,存放離散輸入數據指針
- // StAddr,離散的起始地址
- // Quantity,離散的數量
- // 輸出參數:正常返回TRUE,出錯返回FALSE。
- // 功能描述:讀離散輸入操作函數
- //************************************************************************************
- uint8_t ReadDiscreteInputs(uint8_t *CoilsDataPtr,uint16_t StAddr ,uint16_t Quantity)
- {
- uint8_t *CoilsInByte;
- uint8_t offset;
- uint16_t i=0 ;
- CoilsInByte = CoilsDataPtr;
- while(i<Quantity)
- {
- *CoilsInByte = 0;
- for(offset=0;offset<8;offset++,i++) // 將線圈值寫入一個字節
- {
- if(i<Quantity)
- {
- *CoilsInByte |= MB_GetDiscrete(StAddr+i)<<offset;
- }
- }
- CoilsInByte++;
- }
- return TRUE;
- }
- #endif
- #if READ_COILS_EN
- //************************************************************************************
- // 函數名稱:ReadCoils
- // 輸入參數:CoilsDataPtr,存放線圈數據指針
- // StAddr,線圈的起始地址
- // Quantity,線圈的數量
- // 輸出參數:正常返回TRUE,出錯返回FALSE。
- // 功能描述:讀多線圈操作函數
- //************************************************************************************
- uint8_t ReadCoils(uint8_t *CoilsDataPtr,uint16_t StAddr ,uint16_t Quantity)
- {
- uint8_t *CoilsInByte;
- uint8_t offset;
- uint16_t i=0 ;
- CoilsInByte = CoilsDataPtr;
- while(i<Quantity)
- {
- *CoilsInByte = 0;
- for(offset=0;offset<8;offset++,i++) // 將線圈值寫入一個字節
- {
- if(i<Quantity)
- {
- *CoilsInByte |= MB_GetCoils(StAddr+i)<<offset;
- }
- }
- CoilsInByte++;
- }
- return TRUE;
- }
- #endif
- #if WRITE_SING_COIL_EN
- //************************************************************************************
- // 函數名稱:WriteSingleCoil
- // 輸入參數:DataPtr,數據指針,
- // StAddr,寄存器起始地址
- // Quantity,寄存器數量。
- // 輸出參數:正常返回TRUE,出錯返回FALSE。
- // 功能描述:寫單線圈處理函數
- //************************************************************************************
- uint8_t WriteSingleCoil(uint16_t OutputAddr,uint16_t OutputValue)
- {
- return MB_SetCoil(OutputAddr,(OutputValue==0xff00)?1:0);
- }
- #endif
- #if WRITE_MULT_COIL_EN
- //************************************************************************************
- // 函數名稱:WriteMultipleCoils
- // 輸入參數:DataPtr,數據指針,
- // StAddr,寄存器起始地址
- // Quantity,寄存器數量。
- // 輸出參數:正常返回TRUE,出錯返回FALSE。
- // 功能描述:寫多線圈處理函數
- //************************************************************************************
- uint8 WriteMultipleCoils(uint8 *DataPtr,uint16 StAddr ,uint16 Quantity)
- {
- uint16 i;
- uint8 CoilInByte;
- for(i=0;i<Quantity;i++)
- {
- if((i%8)==0)
- CoilInByte = *(DataPtr+(i/8));
- if( FALSE == MB_SetCoil(StAddr+i,(CoilInByte>>(i%8))&0x01))
- return FALSE;
- }
- return TRUE;
- }
- #endif
- #if READ_INPUT_REG_EN
- //************************************************************************************
- // 函數名稱:ReadInputReg
- // 輸入參數:DataPtr,數據指針,
- // StAddr,寄存器起始地址
- // Quantity,寄存器數量。
- // 輸出參數:正常返回TRUE,出錯返回FALSE。
- // 功能描述:讀輸入寄存器
- //************************************************************************************
- uint8_t ReadInputReg(uint8_t *DataPtr,uint16_t StAddr ,uint16_t Quantity)
- {
- uint8_t i;
- uint16_t Value;
- for(i=0;i<Quantity;i++)
- {
- Value = MB_GetInputRegValue(StAddr+i);
- *(DataPtr+i*2) = (uint8)(Value>>8); // 先傳高位
- *(DataPtr+i*2+1) = (uint8)Value; // 后傳低位
- }
- return TRUE;
- }
- #endif
- #if READ_HOLD_REG_EN+READ_WRITE_REG_EN+MASK_WRITE_REG_EN
- //************************************************************************************
- // 函數名稱:ReadHoldingReg
- // 輸入參數:DataPtr,數據指針,16位數據高8位在低字節,低8位在高字節
- // StAddr,寄存器起始地址
- // Quantity,寄存器數量。
- // 輸出參數:正常返回TRUE,出錯返回FALSE。
- // 功能描述:讀寄存器處理函數
- //************************************************************************************
- uint8_t ReadHoldingReg(uint8_t *DataPtr,uint16_t StAddr ,uint16_t Quantity)
- {
- uint8_t i;
- uint16_t Value;
- for(i=0;i<Quantity;i++)
- {
- Value = MB_GetRegValue(StAddr+i);
- *(DataPtr+i*2) = (uint8)(Value>>8); // 先傳高位
- *(DataPtr+i*2+1) = (uint8)Value; // 后傳低位
- }
- return TRUE;
- }
- #endif
- #if WRITE_MULT_REG_EN+READ_WRITE_REG_EN+MASK_WRITE_REG_EN+WRITE_SING_REG_EN
- //************************************************************************************
- // 函數名稱:WriteHoldingReg
- // 輸入參數:DataPtr,數據指針,16位數據高8位在低字節,低8位在高字節
- // StAddr,寄存器起始地址
- // Quantity,寄存器數量。
- // 輸出參數:正常返回TRUE,出錯返回FALSE。
- // 功能描述:寫寄存器處理函數
- //************************************************************************************
- uint8_t WriteHoldingReg(uint8_t *KeepDataPtr,uint16_t StAddr ,uint16_t Quantity)
- {
- uint8_t i;
- uint16_t Value;
- for(i=0;i<Quantity;i++)
- {
- Value = (*(KeepDataPtr+i*2)<<8)|(*(KeepDataPtr+i*2+1));
- MB_SetRegValue(StAddr+i,Value);
- }
- return TRUE;
- }
- #endif
- #if READ_COILS_EN > 0
- /****************************************************************************************/
- // 函數名稱:FReadCoils
- // 輸入參數:Response,為PDU_RESPONSE類型的指針,PDU_RESPONSE:
- // typedef struct __PDU_RESPONSE{
- // uint8* PDUDataPtr; 請求數據指針,功能代碼處理函數必需將處理的數據包存在該指針的開始位始
- // uint8 PDUByteLength;請求數據數長度
- // uint8 ExceptionCode;僅為輸出錯異常代碼,正常操作設為0
- // }PDU_RESPONSE
- // 輸出參數:無
- // 功能描述:處理讀多線圈指令01
- /***************************************************************************************/
- void FReadCoils(PDU_RESPONSE *Response)
- {
- uint8_t * PDUPtr;
- uint16_t 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_COILS_ADDR)&&((StAddr+Quantity)<=END_COILS_ADDR))
- {
- if(ReadCoils(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_DIS_INPUT_EN >0
- /****************************************************************************************/
- // 函數名稱:FReadDiscreteInputs
- // 輸入參數:Response
- // 輸出參數:無
- // 功能描述:處理讀離散輸入指令02
- /***************************************************************************************/
- void FReadDiscreteInputs(PDU_RESPONSE *Response)
- {
- uint8_t * PDUPtr;
- uint16_t 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_t * PDUPtr;
- uint16_t 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_t * PDUPtr;
- uint16_t 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_t * PDUPtr;
- uint16_t 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_t * PDUPtr;
- uint16_t StAddr ,Quantity;
- uint8_t 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_t * PDUPtr;
- uint16_t StAddr ,Quantity;
- uint8_t 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_t RefeAddr,And_Mask,Or_Mask,RegValue;
- uint8_t *PDUPtr;
- uint8_t 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_t ReadStaAddr, ReadQuantity;
- uint16_t WriteStaAddr,WriteQuantity;
- uint8_t WriteBytes;
- uint8_t *WriteValuePtr;
- uint8_t *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
復制代碼
|