⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 command.c

📁 DSP 5409 plc应用程序,调试通过,是用在电力线通讯上的演示程序.
💻 C
📖 第 1 页 / 共 5 页
字号:
/*Master*/			// Command:  Read memory block from slave.
/*Master*/			// 	uUartCommand[1] = Slave address - high (0 for closest).
/*Master*/			// 	uUartCommand[2] = Slave address - low  (0 for closest).
/*Master*/			// 	uUartCommand[3] = starting address
/*Master*/			// 	uUartCommand[4] = word count (<=60)
/*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] = starting address
/*Master*/			//  txUserDataArray[6] = block length (words).  <= 60
/*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_READ_BLOCK;		// Command number = Get status
/*Master*/			txUserDataArray[5] = uUartCommand[3]; 	// Starting Address.
/*Master*/			txUserDataArray[6] = uUartCommand[4]; 	// Block Length
/*Master*/			uHostResponseStart = 4;					// Slaves response data will follow two address parms.
/*Master*/			uHostResponseLength = 1+uUartCommand[4]; // Acknowledge plus requested 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*/ 		case MC_ERASE_SLAVE_FLASH:
/*Master*/ 			//==============================================================================
/*Master*/ 			// Command:  Erase Slave Flash
/*Master*/ 			//           Only able to erase log area on slave. (not program spaces)
/*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*/			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_ERASE_FLASH;	// Command number
/*Master*/			uHostResponseStart = 4;					// Slaves response data will follow two address parms.
/*Master*/			uHostResponseLength = 1; 				// Command acknowledge.
/*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*/ 		case MC_SLAVE_READ_FLASH:
/*Master*/ 			//==============================================================================
/*Master*/ 			// Command:  Read Slave Flash
/*Master*/ 			// 	uUartCommand[1] = Slave address - high (0 for closest).
/*Master*/ 			// 	uUartCommand[2] = Slave address - low  (0 for closest).
/*Master*/ 			// 	uUartCommand[3] = Starting address - high.
/*Master*/ 			// 	uUartCommand[4] = Starting address - low.
/*Master*/ 			// 	uUartCommand[5] = Count (words) to read. <= 60.
/*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] = starting address - high
/*Master*/ 			//  txUserDataArray[6] = starting address - low
/*Master*/ 			//  txUserDataArray[7] = block length (words).  <= 60
/*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_READ_FLASH;		// Command number = Get status
/*Master*/ 			txUserDataArray[5] = uUartCommand[3]; 	// Starting Address - high.
/*Master*/ 			txUserDataArray[6] = uUartCommand[4]; 	// Starting Address - low.
/*Master*/ 			txUserDataArray[7] = uUartCommand[5]; 	// Block Length
/*Master*/ 			uHostResponseStart = 4;					// Slaves response data will follow two address parms.
/*Master*/ 			uHostResponseLength = 1+uUartCommand[4]; // Acknowledge plus requested 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*/ 		case MC_SLAVE_RESET_MSP:
/*Master*/ 			//==============================================================================
/*Master*/ 			// Command:  Reset Slave MSP430
/*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*/			//	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*/			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_RESET_MSP;		// Command number
/*Master*/			uHostResponseStart = 4;					// Slaves response data will follow two address parms.
/*Master*/			uHostResponseLength = 1; 				// Command acknowledge.
/*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*/		case MC_SLAVE_EMETER_CMD:
/*Master*/			//==============================================================================
/*Master*/			// Command:  Send a command to the slave emeter
/*Master*/			//
/*Master*/			//  This is a command to the slave emeter which doesn't expect a reply.  The
/*Master*/			//	slave will still respond when it's 485 communication is finished, but only
/*Master*/			//	the one word status is valid in it's response, and only that one word is
/*Master*/			//	sent back to the host.
/*Master*/			//
/*Master*/			// Unless the address is broadcast - then this command responds to the host
/*Master*/			// and the slave will not respond to the Master.
/*Master*/			//
/*Master*/			// 	uUartCommand[1] = Slave address - high (0 for closest).
/*Master*/			// 	uUartCommand[2] = Slave address - low  (0 for closest).
/*Master*/			//	uUartCommand[3-7] = Command string.
/*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-9] = Command string.
/*Master*/			//
/*Master*/			// return:
/*Master*/			// 		uPlcState is set to receive 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_EMETER_CMD;		// Command number = Issue slave emeter command.
/*Master*/			txUserDataArray[5] = uUartCommand[3];
/*Master*/			txUserDataArray[6] = uUartCommand[4];
/*Master*/			txUserDataArray[7] = uUartCommand[5];
/*Master*/			txUserDataArray[8] = uUartCommand[6];
/*Master*/			txUserDataArray[9] = uUartCommand[7];
/*Master*/
/*Master*/			// Test for broadcast case - if so, the Master sends the acknowledge.
/*Master*/			if( ulAddr == 1L)		// broadcast.
/*Master*/			{
/*Master*/				WriteUARTValue(UART_TARGET_HOST, SUCCESS);	// Command acknowledge.
/*Master*/				uPlcState = PLC_IGNORE;						// back to normal.
/*Master*/				ulAutoPollCounter = 0L;
/*Master*/				uAutoPollPause=0;							// Release AutoPoll.
/*Master*/			}
/*Master*/			else
/*Master*/			{
/*Master*/				uHostResponseStart = 4;				// Slaves response data will follow two address parms.
/*Master*/				uHostResponseLength = 1;			// Return only the status word.
/*Master*/				uPlcState = PLC_RECEIVE_RESPONSE;	// Get ready for response
/*Master*/				ulPlcResponseTimeoutCounter = 0L;	// Reset timeout counter.
/*Master*/			}
/*Master*/			uTransmitPacketReady = 1;			// Send completed command to the SLAVE.
/*Master*/
/*Master*/			break;
/*Master*/
/*Master*/		case MC_SLAVE_EMETER_REQ:
/*Master*/			//==============================================================================
/*Master*/			// Command:  Send a request to the slave emeter
/*Master*/			//
/*Master*/			//  This is a command to the slave which expect a meaningful reply which will
/*Master*/			//	be relayed to the host (the entire PLC packet).
/*Master*/			//
/*Master*/			// 	uUartCommand[1] = Slave address - high (0 for closest).
/*Master*/			// 	uUartCommand[2] = Slave address - low  (0 for closest).
/*Master*/			//	uUartCommand[3-7] = Command string.
/*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-9] = Command string.
/*Master*/			//
/*Master*/			// return:
/*Master*/			// 		uPlcState is set to receive 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_EMETER_REQ;		// Command number = Issue slave emeter request.
/*Master*/			txUserDataArray[5] = uUartCommand[3];
/*Master*/			txUserDataArray[6] = uUartCommand[4];
/*Master*/			txUserDataArray[7] = uUartCommand[5];
/*Master*/			txUserDataArray[8] = uUartCommand[6];
/*Master*/			txUserDataArray[9] = uUartCommand[7];
/*Master*/			uHostResponseStart = 4;				// Slaves response data will follow two address parms.
/*Master*/			uHostResponseLength = 61;			// Return all emeter data.
/*Master*/			ulPlcResponseTimeoutCounter = 0L;			// Reset timeout counter.
/*Master*/			uPlcState = PLC_RECEIVE_RESPONSE;	// Get ready for response
/*Master*/			uTransmitPacketReady = 1;			// Send completed command to the SLAVE.
/*Master*/			break;
/*Master*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -