📄 command.c
字号:
/*Master*/ uAutoPollPause=0; // Release AutoPoll.
/*Master*/ break;
/*Master*/
/*Master*/ case MC_FIND_SLAVES:
/*Master*/ //==============================================================================
/*Master*/ // Start searching for all slaves.
/*Master*/ //
/*Master*/ // uUartCommand[1] = starting slave address - high
/*Master*/ // uUartCommand[2] = starting slave address - low
/*Master*/ // uUartCommand[3] = max slave address - high
/*Master*/ // uUartCommand[4] = max slave address - low
/*Master*/ //
/*Master*/ // return:
/*Master*/ // No return value is sent until a slave is found, or all addresses are
/*Master*/ // tried without any success.
/*Master*/ //==============================================================================
/*Master*/ ulFindSlaveAddr = (u32)uUartCommand[1]*65536L + (u32)uUartCommand[2];
/*Master*/ ulFindSlaveAddrMax = (u32)uUartCommand[3]*65536L + (u32)uUartCommand[4];
/*Master*/ uPlcState = PLC_FIND_ALL_SLAVES; // Get ready for response
/*Master*/ uFindSlaves = 1; // Start searching.
/*Master*/ break;
/*Master*/
/*Master*/ case MC_ABORT_FIND:
/*Master*/ //==============================================================================
/*Master*/ // Abort searching for all slaves.
/*Master*/ //
/*Master*/ // return:
/*Master*/ // Command acknowledge.
/*Master*/ //==============================================================================
/*Master*/ WriteUARTValue(UART_TARGET_HOST, SUCCESS); // Command acknowledge.
/*Master*/ uFindSlaves = 0; // Stop searching.
/*Master*/ uPlcState = PLC_IGNORE; // back to normal.
/*Master*/
/*Master*/ // Abort Find closest slave if it's running as well.
/*Master*/ // Initialize uClosestSlaveAddr to an invalid address.
/*Master*/ if (uSlaveFound == 0)
/*Master*/ {
/*Master*/ uSlaveFound = 1;
/*Master*/ ulClosestSlaveAddr = 0;
/*Master*/ }
/*Master*/
/*Master*/ break;
/*Master*/
/*Master*/ case MC_RESET_MSP:
/*Master*/ //==============================================================================
/*Master*/ // Reset the MSP.
/*Master*/ //
/*Master*/ // NOTE: There is a fairly long time lag before the MSP comes back!
/*Master*/ // The host code must wait, or poll for completion.
/*Master*/ //
/*Master*/ // return:
/*Master*/ // Command acknowledge.
/*Master*/ //==============================================================================
/*Master*/ WriteUARTValue(UART_TARGET_HOST, SUCCESS); // Command acknowledge.
/*Master*/ ResetEMeter();
/*Master*/ ulAutoPollCounter = 0L;
/*Master*/ uAutoPollPause=0; // Release AutoPoll.
/*Master*/ break;
/*Master*/
/*Master*/ case MC_EMETER_CMD:
/*Master*/ //==============================================================================
/*Master*/ // Command: Emeter command
/*Master*/ //
/*Master*/ // This is a command to the emeter which does NOT expect a reply.
/*Master*/ // ex:
/*Master*/ // 'W' Write string to LCD
/*Master*/ // 'D' Display mode
/*Master*/ // +/- Adjust calibration values
/*Master*/ // 'S' Set time/date
/*Master*/ //
/*Master*/ // uUartCommand[1-7] = Variable length string to send to emeter
/*Master*/ // This string must include the command letter prefix and a training \r
/*Master*/ // if required for the command. Any unused trailing command parms should
/*Master*/ // be set to zero. When the command is sent, one additional parm of 0
/*Master*/ // will be tacked on.
/*Master*/ //
/*Master*/ // return:
/*Master*/ // NOTHING. Command acknowledge handled when emeter communication is done by
/*Master*/ // setting uAckAfter485 flag.
/*Master*/ //
/*Master*/ // NOTE: Most changes to this function should have corresponding changes made
/*Master*/ // in the SC_EMETER_CMD case below.
/*Master*/ //==============================================================================
/*Master*/ emeterStringIndex = 0;
/*Master*/ for(i=1; i<=7; i++)
/*Master*/ {
/*Master*/ emeterMsgString[emeterStringIndex++] = (uUartCommand[i]&0xFF00)>>8;
/*Master*/ emeterMsgString[emeterStringIndex++] = (uUartCommand[i]&0x00FF);
/*Master*/ }
/*Master*/ emeterMsgString[emeterStringIndex] = 0; // Add a null in case the received string didn't have one.
/*Master*/ WriteUARTString(emeterMsgString); // Send command to emeter.
/*Master*/
/*Master*/ uAckAfter485 = 1; // Notify host after 485 communication is complete.
/*Master*/ break;
/*Master*/
/*Master*/ case MC_EMETER_REQ:
/*Master*/ //==============================================================================
/*Master*/ // Command: Emeter request
/*Master*/ //
/*Master*/ // This is a command to the emeter which DOES expect a reply.
/*Master*/ //
/*Master*/ // uUartCommand[1-7] = Variable length string to send to emeter
/*Master*/ // This string must include the command letter prefix and a training \r
/*Master*/ // if required for the command. Any unused trailing command parms should
/*Master*/ // be set to zero. When the command is sent, one additional parm of 0
/*Master*/ // will be tacked on.
/*Master*/ //
/*Master*/ // return:
/*Master*/ // NOTHING. Command response to host will be sent with emeter data.
/*Master*/ //
/*Master*/ // NOTE: Most changes to this function should have corresponding changes made
/*Master*/ // in the SC_EMETER_CMD case below.
/*Master*/ //==============================================================================
/*Master*/ emeterStringIndex = 0;
/*Master*/ for(i=1; i<=7; i++)
/*Master*/ {
/*Master*/ emeterMsgString[emeterStringIndex++] = (uUartCommand[i]&0xFF00)>>8;
/*Master*/ emeterMsgString[emeterStringIndex++] = (uUartCommand[i]&0x00FF);
/*Master*/ }
/*Master*/ emeterMsgString[emeterStringIndex] = 0; // Add a null in case the received string didn't have one.
/*Master*/ WriteUARTString(emeterMsgString);
/*Master*/
/*Master*/ uUartState = UART_RECEIVE_RESPONSE;
/*Master*/ memset(upUartResponseBuffer, 0, 61*sizeof(upUartResponseBuffer[0]));
/*Master*/ upUartResponseBuffer[0] = SUCCESS;
/*Master*/ uUartResponseIndex = 2; // offset in bytes to start reading into receive buffer.
/*Master*/ ulUartCounter = 0L; // Reset timeout counter
/*Master*/ break;
/*Master*/
/*Master*/ case MC_GET_SLAVE_PARM:
/*Master*/ //==============================================================================
/*Master*/ // Command: Get slave parm.
/*Master*/ // uUartCommand[1] = Slave address - high (0 for closest).
/*Master*/ // uUartCommand[2] = Slave address - low (0 for closest).
/*Master*/ // uUartCommand[3] = index into address table of requested value.
/*Master*/ //
/*Master*/ // txUserDataArray[0] = dest addr - high
/*Master*/ // txUserDataArray[1] = dest addr - low
/*Master*/ // txUserDataArray[2] = source addr - high
/*Master*/ // txUserDataArray[3] = source addr - low
/*Master*/ // txUserDataArray[4] = command number
/*Master*/ // txUserDataArray[5] = index into configuration table.
/*Master*/ //
/*Master*/ // return:
/*Master*/ // uPlcState is set to generate the return code for the response.
/*Master*/ //==============================================================================
/*Master*/ ulAddr = (u32)uUartCommand[1]*65536L + (u32)uUartCommand[2];
/*Master*/ memset(txUserDataArray, 0, DATA_BUFFER_LEN*sizeof(txUserDataArray[0]));
/*Master*/
/*Master*/ if(ulAddr == 0L) // Command[1] specifies slave address
/*Master*/ { // 0L --> use "uClosestSlave"
/*Master*/ *(u32*)&txUserDataArray[0] = ulClosestSlaveAddr;
/*Master*/ }
/*Master*/ else // else used passed arguement for slave addr
/*Master*/ {
/*Master*/ *(u32*)&txUserDataArray[0] = ulAddr;
/*Master*/ }
/*Master*/
/*Master*/ *(u32*)&txUserDataArray[2] = ulMyAddr;
/*Master*/ txUserDataArray[4] = SC_GET_PARM; // Command number = Get parm.
/*Master*/ txUserDataArray[5] = uUartCommand[3]; // parm index that we're requesting.
/*Master*/ uHostResponseStart = 4; // Slaves response data will follow two address parms.
/*Master*/ if(uUartCommand[3] <= 30) // Requesting 16-bit parm
/*Master*/ {
/*Master*/ uHostResponseLength = 2; // Acknowlege and single value.
/*Master*/ }
/*Master*/ else // Requesting 32-bit parm
/*Master*/ {
/*Master*/ uHostResponseLength = 3; // Acknowlege and double value.
/*Master*/ }
/*Master*/ uPlcState = PLC_RECEIVE_RESPONSE; // Get ready for response
/*Master*/ ulPlcResponseTimeoutCounter = 0; // Reset timeout counter.
/*Master*/ uTransmitPacketReady = 1; // Send completed command to the SLAVE.
/*Master*/ break;
/*Master*/
/*Master*/ case MC_SET_SLAVE_PARM:
/*Master*/ //==============================================================================
/*Master*/ // Command: Get slave parm.
/*Master*/ // uUartCommand[1] = Slave address - high (0 for closest).
/*Master*/ // uUartCommand[2] = Slave address - low (0 for closest).
/*Master*/ // uUartCommand[3] = index into address table of requested value.
/*Master*/ // uUartCommand[4] = value (high word for 32-bit parms)
/*Master*/ // uUartCommand[5] = (low word for 32-bit parms)
/*Master*/ //
/*Master*/ // txUserDataArray[0] = dest addr - high
/*Master*/ // txUserDataArray[1] = dest addr - low
/*Master*/ // txUserDataArray[2] = source addr - high
/*Master*/ // txUserDataArray[3] = source addr - low
/*Master*/ // txUserDataArray[4] = command number
/*Master*/ // txUserDataArray[5] = index into configuration table.
/*Master*/ // txUserDataArray[6] = value (high word).
/*Master*/ // txUserDataArray[7] = value (low word).
/*Master*/ //
/*Master*/ // return:
/*Master*/ // uPlcState is set to generate the return code for the response.
/*Master*/ //==============================================================================
/*Master*/ ulAddr = (u32)uUartCommand[1]*65536L + (u32)uUartCommand[2];
/*Master*/ memset(txUserDataArray, 0, DATA_BUFFER_LEN*sizeof(txUserDataArray[0]));
/*Master*/
/*Master*/ if(ulAddr == 0L) // Command[1] specifies slave address
/*Master*/ { // 0L --> use "uClosestSlave"
/*Master*/ *(u32*)&txUserDataArray[0] = ulClosestSlaveAddr;
/*Master*/ }
/*Master*/ else // else used passed arguement for slave addr
/*Master*/ {
/*Master*/ *(u32*)&txUserDataArray[0] = ulAddr;
/*Master*/ }
/*Master*/
/*Master*/ *(u32*)&txUserDataArray[2] = ulMyAddr;
/*Master*/ txUserDataArray[4] = SC_SET_PARM; // Command number = Set parm.
/*Master*/ txUserDataArray[5] = uUartCommand[3]; // parm index that we're setting.
/*Master*/ txUserDataArray[6] = uUartCommand[4]; // value (high word)
/*Master*/ txUserDataArray[7] = uUartCommand[5]; // value (low word).
/*Master*/ uHostResponseStart = 4; // Slaves response data will follow two address parms.
/*Master*/ uHostResponseLength = 1; // Acknowledge.
/*Master*/ uPlcState = PLC_RECEIVE_RESPONSE; // Get ready for response
/*Master*/ ulPlcResponseTimeoutCounter = 0; // Reset timeout counter.
/*Master*/ uTransmitPacketReady = 1; // Send completed command to the SLAVE.
/*Master*/ break;
/*Master*/
/*Master*/ case MC_GET_SLAVE_STATUS:
/*Master*/ //==============================================================================
/*Master*/ // Command: Get slave status.
/*Master*/ // uUartCommand[1] = Slave address - high (0 for closest).
/*Master*/ // uUartCommand[2] = Slave address - low (0 for closest).
/*Master*/ //
/*Master*/ // txUserDataArray[0] = dest addr - high
/*Master*/ // txUserDataArray[1] = dest addr - low
/*Master*/ // txUserDataArray[2] = source addr - high
/*Master*/ // txUserDataArray[3] = source addr - low
/*Master*/ // txUserDataArray[4] = command number
/*Master*/ //
/*Master*/ // return:
/*Master*/ // uPlcState is set to generate the return code for the response.
/*Master*/ //==============================================================================
/*Master*/ ulAddr = (u32)uUartCommand[1]*65536L + (u32)uUartCommand[2];
/*Master*/ memset(txUserDataArray, 0, DATA_BUFFER_LEN*sizeof(txUserDataArray[0]));
/*Master*/
/*Master*/ if(ulAddr == 0L) // Command[1] specifies slave address
/*Master*/ { // 0L --> use "uClosestSlave"
/*Master*/ *(u32*)&txUserDataArray[0] = ulClosestSlaveAddr;
/*Master*/ }
/*Master*/ else // else used passed arguement for slave addr
/*Master*/ {
/*Master*/ *(u32*)&txUserDataArray[0] = ulAddr;
/*Master*/ }
/*Master*/
/*Master*/ *(u32*)&txUserDataArray[2] = ulMyAddr;
/*Master*/ txUserDataArray[4] = SC_GET_STATUS; // Command number = Get status
/*Master*/ uHostResponseStart = 4; // Slaves response data will follow two address parms.
/*Master*/ uHostResponseLength = 1+50; // Acknowledge and 50 words of status data.
/*Master*/ uPlcState = PLC_RECEIVE_RESPONSE; // Get ready for response
/*Master*/ ulPlcResponseTimeoutCounter = 0L; // Reset timeout counter.
/*Master*/ uTransmitPacketReady = 1; // Send completed command to the SLAVE.
/*Master*/ break;
/*Master*/
/*Master*/
/*Master*/ case MC_READ_SLAVE_BLOCK:
/*Master*/ //==============================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -