|
觸摸屏和單片機(jī)進(jìn)行modbus通訊,觸摸屏需要設(shè)置為主機(jī),單片機(jī)為從機(jī),modbusRTU slave 源碼需要植入單片機(jī),希望可以幫助到大家。
0.png (79.58 KB, 下載次數(shù): 194)
下載附件
2016-6-5 00:15 上傳
全部資料下載:
ModbusSlave.zip
(13.59 KB, 下載次數(shù): 249)
2016-5-20 19:07 上傳
點(diǎn)擊文件名下載附件
modbusslave文件源碼 下載積分: 黑幣 -5
- /****************************************Copyright (c)**************************************************
- ** 廣州致遠(yuǎn)電子有限公司
- **
- **--------------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"
- //**************************************************
- // 函數(shù)名稱(chēng):MB_GetDiscrete
- // 輸入?yún)?shù):Address,線(xiàn)圈地址
- // 輸出參數(shù):返回線(xiàn)圈值(0\1)
- // 功能描述:獲取離散值函數(shù),訪(fǎng)函數(shù)由用戶(hù)編寫(xiě)
- //**************************************************
- uint8_t MB_GetDiscrete(uint16_t Address)
- {
- if(Address/2)
- return 1;
- else
- return 0;
- }
- //**************************************************
- // 函數(shù)名稱(chēng):MB_GetCoils
- // 輸入?yún)?shù):Address,線(xiàn)圈地址
- // 輸出參數(shù):返回線(xiàn)圈值(0\1)
- // 功能描述:獲取線(xiàn)圈值函數(shù),訪(fǎng)函數(shù)由用戶(hù)編寫(xiě)
- //**************************************************
- uint8_t MB_GetCoils(uint16_t Address)
- {
- if(Address/2)
- return 1;
- else
- return 0;
- }
- //**************************************************
- // 函數(shù)名稱(chēng):MB_SetCoil
- // 輸入?yún)?shù):Address,線(xiàn)圈地址
- // CoilValue,線(xiàn)圈值(0\1)
- // 輸出參數(shù):返回寄存器值
- // 功能描述:設(shè)置線(xiàn)圈值函數(shù),訪(fǎng)函數(shù)由用戶(hù)編寫(xiě)
- //**************************************************
- uint8_t MB_SetCoil(uint16_t Address,uint8_t CoilValue)
- {
- return CoilValue;
- }
- //**************************************************
- // 函數(shù)名稱(chēng):MB_GetInputRegValue
- // 輸入?yún)?shù):Address,寄存器地址
- // 輸出參數(shù):返回寄存器值
- // 功能描述:讀輸入寄存器值函數(shù),訪(fǎng)函數(shù)由用戶(hù)編寫(xiě)
- //**************************************************
- uint16_t MB_GetInputRegValue(uint16_t Address)
- {
- return Address;
- }
- //**************************************************
- // 函數(shù)名稱(chēng):MB_GetRegValue
- // 輸入?yún)?shù):Address,寄存器地址
- // 輸出參數(shù):返回寄存器值
- // 功能描述:讀保持寄存器值函數(shù),訪(fǎng)函數(shù)由用戶(hù)編寫(xiě)
- //**************************************************
- uint16_t MB_GetRegValue(uint16_t Address)
- {
- return Address;
- }
- //**************************************************
- // 函數(shù)名稱(chēng):MB_SetRegValue
- // 輸入?yún)?shù):Address,寄存器地址
- // Value,寫(xiě)入的值
- // 輸出參數(shù):無(wú)
- // 功能描述:寫(xiě)保持寄存器值函數(shù),訪(fǎng)函數(shù)由用戶(hù)編寫(xiě)
- //**************************************************
- void MB_SetRegValue(uint16_t Address,uint16_t Value)
- {
- }
- /****************************************************************************************/
- // 函數(shù)名稱(chēng):FReadCoils
- // 輸入?yún)?shù):Response
- // 輸出參數(shù):無(wú)
- // 功能描述:處理讀單線(xiàn)圈指令01
- /***************************************************************************************/
- void FReadCoils(PDU_RESPONSE *Response);
- /****************************************************************************************/
- // 函數(shù)名稱(chēng):FReadDiscreteInputs
- // 輸入?yún)?shù):Response
- // 輸出參數(shù):無(wú)
- // 功能描述:處理讀離散輸入指令02
- /***************************************************************************************/
- void FReadDiscreteInputs(PDU_RESPONSE *Response);
- /****************************************************************************************/
- // 函數(shù)名稱(chēng):FReadHoldingReg
- // 輸入?yún)?shù):Response
- // 輸出參數(shù):無(wú)
- // 功能描述:處理讀保持寄存器指令03
- /****************************************************************************************/
- void FReadHoldingReg(PDU_RESPONSE *Response);
- /****************************************************************************************/
- // 函數(shù)名稱(chēng):FReadInputReg
- // 輸入?yún)?shù):Response
- // 輸出參數(shù):無(wú)
- // 功能描述:處理讀輸入寄存器指令04
- /****************************************************************************************/
- void FReadInputReg(PDU_RESPONSE *Response);
- /****************************************************************************************/
- // 函數(shù)名稱(chēng):FWriteSingleCoil
- // 輸入?yún)?shù):Response
- // 輸出參數(shù):無(wú)
- // 功能描述:處理寫(xiě)單線(xiàn)圈指令05
- /****************************************************************************************/
- void FWriteSingleCoil(PDU_RESPONSE *Response);
- /****************************************************************************************/
- // 函數(shù)名稱(chēng):FWriteSingleReg
- // 輸入?yún)?shù):Response
- // 輸出參數(shù):無(wú)
- // 功能描述:處理寫(xiě)單線(xiàn)圈指令06
- /****************************************************************************************/
- void FWriteSingleReg(PDU_RESPONSE *Response);
- /****************************************************************************************/
- // 函數(shù)名稱(chēng):FWriteMultipleCoils
- // 輸入?yún)?shù):Response
- // 輸出參數(shù):無(wú)
- // 功能描述:處理寫(xiě)多線(xiàn)圈指令15
- /****************************************************************************************/
- void FWriteMultipleCoils(PDU_RESPONSE *Response);
- /****************************************************************************************/
- // 函數(shù)名稱(chēng):FWriteMultipleReg
- // 輸入?yún)?shù):Response
- // 輸出參數(shù):無(wú)
- // 功能描述:處理寫(xiě)多寄存器指令16
- /****************************************************************************************/
- void FWriteMultipleReg(PDU_RESPONSE *Response);
- /****************************************************************************************/
- // 函數(shù)名稱(chēng):FMaskWriteReg
- // 輸入?yún)?shù):Response
- // 輸出參數(shù):無(wú)
- // 功能描述:處理屏蔽寄存器指令22
- /****************************************************************************************/
- void FMaskWriteReg(PDU_RESPONSE *Response);
- /****************************************************************************************/
- // 函數(shù)名稱(chēng):FRWMultipleReg
- // 輸入?yún)?shù):Response
- // 輸出參數(shù):無(wú)
- // 功能描述:處理讀寫(xiě)多寄存器指令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}, // 讀線(xiàn)圈
- #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}, // 寫(xiě)單個(gè)線(xiàn)圈
- #endif
- #if WRITE_SING_REG_EN > 0
- {0x06,FWriteSingleReg}, // 寫(xiě)單個(gè)寄存器
- #endif
- #if WRITE_MULT_COIL_EN > 0
- {0x0f,FWriteMultipleCoils}, // 寫(xiě)多個(gè)線(xiàn)圈
- #endif
- #if WRITE_MULT_REG_EN > 0
- {0x10,FWriteMultipleReg}, // 寫(xiě)多個(gè)寄存器
- #endif
- #if MASK_WRITE_REG_EN > 0
- {0x16,FMaskWriteReg}, // 處理屏蔽寄存器指令
- #endif
- #if READ_WRITE_REG_EN > 0
- {0x17,FRWMultipleReg}, // 讀寫(xiě)多個(gè)寄存器
- #endif
- {0x00,FTest} // 測(cè)試功能代碼
- };
- #if READ_DIS_INPUT_EN
- //************************************************************************************
- // 函數(shù)名稱(chēng):ReadDiscreteInputs
- // 輸入?yún)?shù):CoilsDataPtr,存放離散輸入數(shù)據(jù)指針
- // StAddr,離散的起始地址
- // Quantity,離散的數(shù)量
- // 輸出參數(shù):正常返回TRUE,出錯(cuò)返回FALSE。
- // 功能描述:讀離散輸入操作函數(shù)
- //************************************************************************************
- 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++) // 將線(xiàn)圈值寫(xiě)入一個(gè)字節(jié)
- {
- if(i<Quantity)
- {
- *CoilsInByte |= MB_GetDiscrete(StAddr+i)<<offset;
- }
- }
- CoilsInByte++;
- }
- return TRUE;
- }
- #endif
- #if READ_COILS_EN
- //************************************************************************************
- // 函數(shù)名稱(chēng):ReadCoils
- // 輸入?yún)?shù):CoilsDataPtr,存放線(xiàn)圈數(shù)據(jù)指針
- // StAddr,線(xiàn)圈的起始地址
- // Quantity,線(xiàn)圈的數(shù)量
- // 輸出參數(shù):正常返回TRUE,出錯(cuò)返回FALSE。
- // 功能描述:讀多線(xiàn)圈操作函數(shù)
- //************************************************************************************
- 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++) // 將線(xiàn)圈值寫(xiě)入一個(gè)字節(jié)
- {
- if(i<Quantity)
- {
- *CoilsInByte |= MB_GetCoils(StAddr+i)<<offset;
- }
- }
- CoilsInByte++;
- }
- return TRUE;
- }
- #endif
- #if WRITE_SING_COIL_EN
- //************************************************************************************
- // 函數(shù)名稱(chēng):WriteSingleCoil
- // 輸入?yún)?shù):DataPtr,數(shù)據(jù)指針,
- // StAddr,寄存器起始地址
- // Quantity,寄存器數(shù)量。
- // 輸出參數(shù):正常返回TRUE,出錯(cuò)返回FALSE。
- // 功能描述:寫(xiě)單線(xiàn)圈處理函數(shù)
- //************************************************************************************
- uint8_t WriteSingleCoil(uint16_t OutputAddr,uint16_t OutputValue)
- {
- return MB_SetCoil(OutputAddr,(OutputValue==0xff00)?1:0);
- }
- #endif
- #if WRITE_MULT_COIL_EN
- //************************************************************************************
- // 函數(shù)名稱(chēng):WriteMultipleCoils
- // 輸入?yún)?shù):DataPtr,數(shù)據(jù)指針,
- // StAddr,寄存器起始地址
- // Quantity,寄存器數(shù)量。
- // 輸出參數(shù):正常返回TRUE,出錯(cuò)返回FALSE。
- // 功能描述:寫(xiě)多線(xiàn)圈處理函數(shù)
- //************************************************************************************
- 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
- //************************************************************************************
- // 函數(shù)名稱(chēng):ReadInputReg
- // 輸入?yún)?shù):DataPtr,數(shù)據(jù)指針,
- // StAddr,寄存器起始地址
- // Quantity,寄存器數(shù)量。
- // 輸出參數(shù):正常返回TRUE,出錯(cuò)返回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
- //************************************************************************************
- // 函數(shù)名稱(chēng):ReadHoldingReg
- // 輸入?yún)?shù):DataPtr,數(shù)據(jù)指針,16位數(shù)據(jù)高8位在低字節(jié),低8位在高字節(jié)
- // StAddr,寄存器起始地址
- // Quantity,寄存器數(shù)量。
- // 輸出參數(shù):正常返回TRUE,出錯(cuò)返回FALSE。
- // 功能描述:讀寄存器處理函數(shù)
- //************************************************************************************
- 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
- //************************************************************************************
- // 函數(shù)名稱(chēng):WriteHoldingReg
- // 輸入?yún)?shù):DataPtr,數(shù)據(jù)指針,16位數(shù)據(jù)高8位在低字節(jié),低8位在高字節(jié)
- // StAddr,寄存器起始地址
- // Quantity,寄存器數(shù)量。
- // 輸出參數(shù):正常返回TRUE,出錯(cuò)返回FALSE。
- // 功能描述:寫(xiě)寄存器處理函數(shù)
- //************************************************************************************
- 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
- /****************************************************************************************/
- // 函數(shù)名稱(chēng):FReadCoils
- // 輸入?yún)?shù):Response,為PDU_RESPONSE類(lèi)型的指針,PDU_RESPONSE:
- // typedef struct __PDU_RESPONSE{
- // uint8* PDUDataPtr; 請(qǐng)求數(shù)據(jù)指針,功能代碼處理函數(shù)必需將處理的數(shù)據(jù)包存在該指針的開(kāi)始位始
- // uint8 PDUByteLength;請(qǐng)求數(shù)據(jù)數(shù)長(zhǎng)度
- // uint8 ExceptionCode;僅為輸出錯(cuò)異常代碼,正常操作設(shè)為0
- // }PDU_RESPONSE
- // 輸出參數(shù):無(wú)
- // 功能描述:處理讀多線(xiàn)圈指令01
- /***************************************************************************************/
- void FReadCoils(PDU_RESPONSE *Response)
- {
- uint8_t * PDUPtr;
- uint16_t StAddr ,Quantity;
- // 從請(qǐng)求PDU中提取相關(guān)參數(shù)
- PDUPtr = Response->PDUDataPtr;
- StAddr = PDUPtr[1]<<8|PDUPtr[2];
- Quantity = PDUPtr[3]<<8|PDUPtr[4];
-
- Response->PDUByteLength = 2 + Quantity/8+((Quantity%8)?1:0) ; // 正常返回的數(shù)據(jù)長(zhǎng)度
-
- 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; // 字節(jié)數(shù)
- Response->ExceptionCode = 0x00;
- }
- else
- Response->ExceptionCode = 0x04;
- }
- else
- Response->ExceptionCode = 0x02;
- }
- else
- Response->ExceptionCode = 0x03;
- }
- #endif
- #if READ_DIS_INPUT_EN >0
- /****************************************************************************************/
- // 函數(shù)名稱(chēng):FReadDiscreteInputs
- // 輸入?yún)?shù):Response
- // 輸出參數(shù):無(wú)
- // 功能描述:處理讀離散輸入指令02
- /***************************************************************************************/
- void FReadDiscreteInputs(PDU_RESPONSE *Response)
- {
- uint8_t * PDUPtr;
- uint16_t StAddr ,Quantity;
- // 從請(qǐng)求PDU中提取相關(guān)參數(shù)
- PDUPtr = Response->PDUDataPtr;
- StAddr = PDUPtr[1]<<8|PDUPtr[2];
- Quantity = PDUPtr[3]<<8|PDUPtr[4];
-
- Response->PDUByteLength = 2 + Quantity/8+((Quantity%8)?1:0) ; // 正常返回的數(shù)據(jù)長(zhǎng)度
-
- 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; // 字節(jié)數(shù)
- Response->ExceptionCode = 0x00;
- }
- else
- Response->ExceptionCode = 0x04;
- }
- else
- Response->ExceptionCode = 0x02;
- }
- else
- Response->ExceptionCode = 0x03;
- }
- #endif
- #if READ_HOLD_REG_EN >0
- /****************************************************************************************/
- // 函數(shù)名稱(chēng):FReadHoldingReg
- // 輸入?yún)?shù):Response
- // 輸出參數(shù):無(wú)
- // 功能描述:處理讀保持寄存器指令03
- /****************************************************************************************/
- void FReadHoldingReg(PDU_RESPONSE *Response)
- {
- uint8_t * PDUPtr;
- uint16_t StAddr ,Quantity;
- // 從請(qǐng)求PDU中提取相關(guān)參數(shù)
- PDUPtr = Response->PDUDataPtr;
- StAddr = PDUPtr[1]<<8|PDUPtr[2];
- Quantity = PDUPtr[3]<<8|PDUPtr[4];
-
- Response->PDUByteLength = 2 + Quantity*2 ; // 正常返回的數(shù)據(jù)長(zhǎng)度
-
- 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
- /****************************************************************************************/
- // 函數(shù)名稱(chēng):FReadInputReg
- // 輸入?yún)?shù):Response
- // 輸出參數(shù):無(wú)
- // 功能描述:處理讀輸入寄存器指令04
- /****************************************************************************************/
- void FReadInputReg(PDU_RESPONSE *Response)
- {
- uint8_t * PDUPtr;
- uint16_t StAddr ,Quantity;
- // 從請(qǐng)求PDU中提取相關(guān)參數(shù)
- PDUPtr = Response->PDUDataPtr;
- StAddr = PDUPtr[1]<<8|PDUPtr[2];
- Quantity = PDUPtr[3]<<8|PDUPtr[4];
-
- Response->PDUByteLength = 2 + Quantity*2 ; // 正常返回的數(shù)據(jù)長(zhǎng)度
-
- 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
- /****************************************************************************************/
- // 函數(shù)名稱(chēng):FWriteSingleCoil
- // 輸入?yún)?shù):Response
- // 輸出參數(shù):無(wú)
- // 功能描述:處理寫(xiě)單線(xiàn)圈指令05
- /****************************************************************************************/
- void FWriteSingleCoil(PDU_RESPONSE *Response)
- {
- uint8_t * PDUPtr;
- uint16_t OutputAddr ,OutputValue;
- // 從請(qǐng)求PDU中提取相關(guān)參數(shù)
- PDUPtr = Response->PDUDataPtr;
- OutputAddr = PDUPtr[1]<<8|PDUPtr[2];
- OutputValue = PDUPtr[3]<<8|PDUPtr[4];
-
- Response->PDUByteLength = 5 ; // 正常返回的數(shù)據(jù)長(zhǎng)度
-
- 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
- /****************************************************************************************/
- // 函數(shù)名稱(chēng):FWriteSingleReg
- // 輸入?yún)?shù):Response
- // 輸出參數(shù):無(wú)
- // 功能描述:處理寫(xiě)單線(xiàn)圈指令06
- /****************************************************************************************/
- void FWriteSingleReg(PDU_RESPONSE *Response)
- {
- uint8 * PDUPtr;
- uint16 OutputAddr ,OutputValue;
- // 從請(qǐng)求PDU中提取相關(guān)參數(shù)
- PDUPtr = Response->PDUDataPtr;
- OutputAddr = PDUPtr[1]<<8|PDUPtr[2];
- OutputValue = PDUPtr[3]<<8|PDUPtr[4];
-
- Response->PDUByteLength = 2 + 5 ; // 正常返回的數(shù)據(jù)長(zhǎng)度
-
- 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
- /****************************************************************************************/
- // 函數(shù)名稱(chēng):FWriteMultipleCoils
- // 輸入?yún)?shù):Response
- // 輸出參數(shù):無(wú)
- // 功能描述:處理寫(xiě)多線(xiàn)圈指令15
- /****************************************************************************************/
- void FWriteMultipleCoils(PDU_RESPONSE *Response)
- {
- uint8_t * PDUPtr;
- uint16_t StAddr ,Quantity;
- uint8_t ByteCount;
- // 從請(qǐng)求PDU中提取相關(guān)參數(shù)
- PDUPtr = Response->PDUDataPtr;
- StAddr = PDUPtr[1]<<8|PDUPtr[2];
- Quantity = PDUPtr[3]<<8|PDUPtr[4];
- ByteCount = PDUPtr[5];
-
- Response->PDUByteLength = 5 ; // 正常返回的數(shù)據(jù)長(zhǎng)度
- 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
- /****************************************************************************************/
- // 函數(shù)名稱(chēng):FWriteMultipleReg
- // 輸入?yún)?shù):Response
- // 輸出參數(shù):無(wú)
- // 功能描述:處理寫(xiě)多寄存器指令16
- /****************************************************************************************/
- void FWriteMultipleReg(PDU_RESPONSE *Response)
- {
- uint8_t * PDUPtr;
- uint16_t StAddr ,Quantity;
- uint8_t ByteCount;
- // 從請(qǐng)求PDU中提取相關(guān)參數(shù)
- PDUPtr = Response->PDUDataPtr;
- StAddr = PDUPtr[1]<<8|PDUPtr[2];
- Quantity = PDUPtr[3]<<8|PDUPtr[4];
- ByteCount = PDUPtr[5];
-
- Response->PDUByteLength = 5 ; // 正常返回的數(shù)據(jù)長(zhǎng)度
- 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
- /****************************************************************************************/
- // 函數(shù)名稱(chēng):FMaskWriteReg
- // 輸入?yún)?shù):Response
- // 輸出參數(shù):無(wú)
- // 功能描述:處理屏蔽寄存器指令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
- /****************************************************************************************/
- // 函數(shù)名稱(chēng):FRWMultipleReg
- // 輸入?yún)?shù):Response
- // 輸出參數(shù):無(wú)
- // 功能描述:處理讀寫(xiě)多寄存器指令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)&& // 讀寄存器的數(shù)量是否在有效范圍內(nèi)
- (WriteQuantity>=0x001)&&(WriteQuantity<=0x0079)&& // 寫(xiě)寄存器的數(shù)量是否在有效范圍內(nèi)
- (WriteBytes==(WriteQuantity*2))) // 接收到的字節(jié)數(shù)是否與寫(xiě)入的寄存器數(shù)量一至
- {
- 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)) // 寫(xiě)寄存器
- {
- 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
復(fù)制代碼
|
評(píng)分
-
查看全部評(píng)分
|