////////////////////////////////////////////////////
//PROJECT CODE:ModbusSlave
//CREATE DATE:2006.2.6
//CREATED BY:软起动部:彭勃
//FUNCTION:Modbus从机通讯
//OTHERS:
////////////////////////////////////////////////////
#include "DSP2833x_Device.h" // DSP2833x Headerfile Include File
/**************************************************
宏定义
**************************************************/
#define Local 1 //slave adress
#define MaxI 32 //MaxI<=128
#define MaxQ 32 //MaxQ<=128
#define MaxAI 32 //MaxAI<=32
#define MaxHold 32 //MaxHold<=32
#define ILLEGAL_FUNCTION 0x01
#define ILLEGAL_DATA_ADDRESS 0x02
#define ILLEGAL_DATA_VALUE 0x03
#define SLAVE_DEVICE_FAILURE 0x04
#define ACKNOWLEDGE 0x05
#define SLAVE_DEVICE_BUSY 0x06
#define SLEEP_TIME 71428
#define TIME_OUT 21428571
/**************************************************
//变量定义
***************************************************/
Uint32 Cputime_Start;
Uint32 Cputime_End;
Uint16 TxFifo_Time;
Uint16 SdataA[80]; // Send data for SCI-A
Uint16 RdataA[80]; // Received data for SCI-A
Uint16 RdataA_Num;
Uint16 SdataA_Num;
Uint16 nData0Test[32];
Uint16 nData1Test[32];
Uint16 nData3Test[32];
Uint16 nData4Test[32];
/* CRC 高位字节值表 */
const Uint16 auchCRCHi[] = {
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
} ;
/* CRC低位字节值表*/
const Uint16 auchCRCLo[] = {
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
0x43, 0x83, 0x41, 0x81, 0x80, 0x40
} ;
/***************************************************
//函数定义
*****************************************************/
void ModbusSlave_init(void);
void ResolveModbus(void);
void sciaRx();
void sciaTx();
void TimeInterrupt();
void ReadCoil(void); //功能1
void ReadInputState(void); //功能2
void ReadHoldRegisters(void); //功能3
void ReadInputRegisters(void); //功能4
void ForceSingleCoil(void); //功能5
void PresetSingleRegister(void); //功能6
void ForceMultipleCoils(void); //功能15
void PresetMultipleRegisters(void); //功能16
void MaskWrite4XRegister(void); //功能22
void GetRegisterVal(Uint16 Addr,Uint16 *ReadData);
void SetRegisterVal(Uint16 Addr,Uint16 TempData);
void GetCoilVal(Uint16 Addr,Uint16 *ReadState);
void SetCoilVal(Uint16 Addr,Uint16 TempData);
void GetInputVal(Uint16 Addr,Uint16 *ReadState);
void GetInputRegisterVal(Uint16 Addr,Uint16 *ReadData);
Uint16 MarkCompute(Uint16 DataSrc,Uint16 DataAnd,Uint16 DataOr);
void SciaTx_Response(void);
void ModbusExcep(Uint16 FunctionCode,Uint16 ExceptionCode);
Uint16 AddrCheck(Uint16 AddrStart,Uint16 Num,Uint16 AddrMax);
Uint16 Crc16(Uint16 *puchMsg, Uint16 usDataLen);
/********************************************************
//函数实现
********************************************************/
void ModbusSlave_init(void)//Modbus串口通信初始化
{
Uint16 i;
for(i = 0; i<80; i++)
{
SdataA[i] = 0;
RdataA[i] = 0;
}
RdataA_Num=0;
SdataA_Num=0;
Cputime_Start=0;
Cputime_End=0;
TxFifo_Time=0;
for(i=0;i<128;i++)
nData0Test[i]=1;
for(i=0;i<128;i++)
nData1Test[i]=1;
for(i=0;i<32;i++)
nData3Test[i]=i;
for(i=0;i<32;i++)
nData4Test[i]=i;
//SCI寄存器初始化
SciaRegs.SCICCR.all =0x0087; // 2 stop bit, No loopback
// No parity,8 char bits,
// async mode, idle-line protocol
SciaRegs.SCICTL1.all =0x0047; // enable TX, RX, internal SCICLK, RX ERR, SLEEP
// disable TXWAKE
SciaRegs.SCICTL2.bit.TXINTENA =1;
SciaRegs.SCICTL2.bit.RXBKINTENA =1;
SciaRegs.SCIHBAUD = 0x0001; //9600
SciaRegs.SCILBAUD = 0x00E7;
SciaRegs.SCIFFTX.all=0xC028;
SciaRegs.SCIFFRX.all=0x0021;
SciaRegs.SCIFFCT.all=0x00;
SciaRegs.SCICTL1.all =0x0023; // Relinquish SCI from Reset
SciaRegs.SCIFFTX.bit.TXFIFOXRESET=1;
SciaRegs.SCIFFRX.bit.RXFIFORESET=1;
}
void ResolveModbus(void)//串口通信接收数据的判决
{
Uint16 CRCCheck;
CRCCheck=0;
switch(RdataA[1]) //判断功能码
{
case 1://读取线圈状态
CRCCheck = Crc16(RdataA,RdataA_Num-2);
if(CRCCheck == RdataA[RdataA_Num-1]+(RdataA[RdataA_Num-2]<<8))
{//CRC校验正确
ReadCoil();
}
else
{//CRC校验不正确
}
break;
case 2://读取输入状态
CRCCheck = Crc16(RdataA,RdataA_Num-2);
if(CRCCheck == RdataA[RdataA_Num-1]+(RdataA[RdataA_Num-2]<<8))
{//CRC校验正确
ReadInputState();
}
else
{//CRC校验不正确
}
break;
case 3://读保持存储器的内容
CRCCheck = Crc16(RdataA,RdataA_Num-2);
if(CRCCheck == RdataA[RdataA_Num-1]+(RdataA[RdataA_Num-2]<<8))
{//CRC校验正确
ReadHoldRegisters();
}
else
{//CRC校验不正确
}
break;
case 4://读输入寄存器的内容
CRCCheck = Crc16(RdataA,RdataA_Num-2);
if(CRCCheck == RdataA[RdataA_Num-1]+(RdataA[RdataA_Num-2]<<8))
{//CRC校验正确
ReadInputRegisters();
}
else
{//CRC校验不正确
}
break;
case 5://设置单个继电器
CRCCheck = Crc16(RdataA,RdataA_Num-2);
if(CRCCheck == RdataA[RdataA_Num-1]+(RdataA[RdataA_Num-2]<<8))
{//CRC校验正确
ForceSingleCoil();
}
else
{//CRC校验不正确
}
break;
case 6://设置单个保持寄存器
CRCCheck = Crc16(RdataA,RdataA_Num-2);
if(CRCCheck == RdataA[RdataA_Num-1]+(RdataA[RdataA_Num-2]<<8))
{//CRC校验正确
PresetSingleRegister();
}
else
{//CRC校验不正确
}
break;
case 8://通讯诊断
break;
case 15: //设置多个继电器
CRCCheck = Crc16(RdataA,RdataA_Num-2);
if(CRCCheck == RdataA[RdataA_Num-1]+(RdataA[RdataA_Num-2]<<8))
{//CRC校验正确
ForceMultipleCoils();
}
else
{//CRC校验不正确
}
break;
case 16://设置多个保持寄存器
CRCCheck = Crc16(RdataA,RdataA_Num-2);
if(CRCCheck == RdataA[RdataA_Num-1]+(RdataA[RdataA_Num-2]<<8))
{//CRC校验正确
PresetMultipleRegisters();
}
else
{//CRC校验不正确
}
break;
case 22://掩码设置保持寄存器
CRCCheck = Crc16(RdataA,RdataA_Num-2);
if(CRCCheck == RdataA[RdataA_Num-1]+(RdataA[RdataA_Num-2]<<8))
{//CRC校验正确
MaskWrite4XRegister();
}
else
{//CRC校验不正确
}
break;
default://从机接收到的功能码不能识别
ModbusExcep(RdataA[1],0x01);
SciaTx_Response();
break;
}
}
void sciaRx()//读取数据序列
{
EINT;
Cputime_Start=CpuTimer0Regs.TIM.all;
if(SciaRegs.SCIRXST.bit.RXWAKE==1)//接收器唤醒检测标志
{
RdataA_Num=0;
RdataA[RdataA_Num]=SciaRegs.SCIRXBUF.all;
if(RdataA[RdataA_Num]==Local)//本机地址,准备接收余下字节
{
RdataA_Num++;
SciaRegs.SCICTL1.bit.SWRESET=0;
SciaRegs.SCICTL1.bit.SLEEP =0;
SciaRegs.SCICTL1.bit.SWRESET=1;
}
else//不是本机地址,继续休眠
{
SciaRegs.SCICTL1.bit.SWRESET=0;
SciaRegs.SCICTL1.bit.SLEEP =1;
SciaRegs.SCICTL1.bit.SWRESET=1;
}
}
else//接收余下接收数据
{
RdataA[RdataA_Num++]=SciaRegs.SCIRXBUF.all; // Read data
}
SciaRegs.SCIFFRX.bit.RXFFOVRCLR=1; // Clear Overflow flag
SciaRegs.SCIFFRX.bit.RXFFINTCLR=1; // Clear Interrupt flag
PieCtrlRegs.PIEACK.all|=0x100; // Issue PIE ack
}
void sciaTx()//写入数据序列
{
Uint16 i;
EINT;
if(SdataA_Num<=16)//一次发送完毕
{
for(i=0; i< SdataA_Num; i++)
{
SciaRegs.SCITXBUF=SdataA[i]; // Send data
}
EDIS; // This is needed to disable write to EALLOW protected registers
PieCtrlRegs.PIEIER9.bit.INTx2=0; // PIE Group 9, INT2
EINT;
}
else if(16
{
if(TxFifo_Time==0)
{
TxFifo_Time++;
for(i=0; i< 16; i++)
{
SciaRegs.SCITXBUF=SdataA[i]; // Send data
}
SciaRegs.SCIFFTX.bit.SCIFFENA=SdataA_Num-16;
}
else if(TxFifo_Time==1)
{
while(SciaRegs.SCIFFTX.bit.TXFFST!=0);
TxFifo_Time=0;
for(i=16; i< SdataA_Num; i++)
{
SciaRegs.SCITXBUF=SdataA[i]; // Send data
}
EDIS; // This is needed to disable write to EALLOW protected registers
PieCtrlRegs.PIEIER9.bit.INTx2=0; // PIE Group 9, INT2
EINT;
}
}
else if(32
{
if(TxFifo_Time==0)
{
TxFifo_Time++;
for(i=0; i< 16; i++)
{
SciaRegs.SCITXBUF=SdataA[i]; // Send data
}
SciaRegs.SCIFFTX.bit.SCIFFENA=16;
}
else if(TxFifo_Time==1)
{
while(SciaRegs.SCIFFTX.bit.TXFFST!=0);
TxFifo_Time++;
for(i=16; i< 32; i++)
{
SciaRegs.SCITXBUF=SdataA[i]; // Send data
}
SciaRegs.SCIFFTX.bit.SCIFFENA=SdataA_Num-32;
}
else if(TxFifo_Time==2)
{
while(SciaRegs.SCIFFTX.bit.TXFFST!=0);
TxFifo_Time=0;
for(i=32; i< SdataA_Num; i++)
{
SciaRegs.SCITXBUF=SdataA[i]; // Send data
}
EDIS; // This is needed to disable write to EALLOW protected registers
PieCtrlRegs.PIEIER9.bit.INTx2=0; // PIE Group 9, INT2
EINT;
}
}
else if(48
{
if(TxFifo_Time==0)
{
TxFifo_Time++;
for(i=0; i< 16; i++)
{
SciaRegs.SCITXBUF=SdataA[i]; // Send data
}
SciaRegs.SCIFFTX.bit.SCIFFENA=16;
}
else if(TxFifo_Time==1)
{
while(SciaRegs.SCIFFTX.bit.TXFFST!=0);
TxFifo_Time++;
for(i=16; i< 32; i++)
{
SciaRegs.SCITXBUF=SdataA[i]; // Send data
}
SciaRegs.SCIFFTX.bit.SCIFFENA=16;
}
else if(TxFifo_Time==2)
{
while(SciaRegs.SCIFFTX.bit.TXFFST!=0);
TxFifo_Time++;
for(i=32; i< 48; i++)
{
SciaRegs.SCITXBUF=SdataA[i]; // Send data
}
SciaRegs.SCIFFTX.bit.SCIFFENA=SdataA_Num-48;
}
else if(TxFifo_Time==3)
{
while(SciaRegs.SCIFFTX.bit.TXFFST!=0);
TxFifo_Time=0;
for(i=48; i< SdataA_Num; i++)
{
SciaRegs.SCITXBUF=SdataA[i]; // Send data
}
EDIS; // This is needed to disable write to EALLOW protected registers
PieCtrlRegs.PIEIER9.bit.INTx2=0; // PIE Group 9, INT2
EINT;
}
}
else if(64
{
if(TxFifo_Time==0)
{
TxFifo_Time++;
for(i=0; i< 16; i++)
{
SciaRegs.SCITXBUF=SdataA[i]; // Send data
}
SciaRegs.SCIFFTX.bit.SCIFFENA=16;
}
else if(TxFifo_Time==1)
{
while(SciaRegs.SCIFFTX.bit.TXFFST!=0);
TxFifo_Time++;
for(i=16; i< 32; i++)
{
SciaRegs.SCITXBUF=SdataA[i]; // Send data
}
SciaRegs.SCIFFTX.bit.SCIFFENA=16;
}
else if(TxFifo_Time==2)
{
while(SciaRegs.SCIFFTX.bit.TXFFST!=0);
TxFifo_Time++;
for(i=32; i< 48; i++)
{
SciaRegs.SCITXBUF=SdataA[i]; // Send data
}
SciaRegs.SCIFFTX.bit.SCIFFENA=16;
}
else if(TxFifo_Time==3)
{
while(SciaRegs.SCIFFTX.bit.TXFFST!=0);
TxFifo_Time++;
for(i=48; i< 64; i++)
{
SciaRegs.SCITXBUF=SdataA[i]; // Send data
}
SciaRegs.SCIFFTX.bit.SCIFFENA=SdataA_Num-64;
}
else if(TxFifo_Time==4)
{
while(SciaRegs.SCIFFTX.bit.TXFFST!=0);
TxFifo_Time=0;
for(i=64; i< SdataA_Num; i++)
{
SciaRegs.SCITXBUF=SdataA[i]; // Send data
}
EDIS; // This is needed to disable write to EALLOW protected registers
PieCtrlRegs.PIEIER9.bit.INTx2=0; // PIE Group 9, INT2
EINT;
}
}
SciaRegs.SCIFFTX.bit.SCIRST=1; // Clear SCI Interrupt flag
PieCtrlRegs.PIEACK.all|=0x100; // Issue PIE ACK
}
void TimeInterrupt()//时间中断
{
CpuTimer2.InterruptCount++;
Cputime_End=CpuTimer0Regs.TIM.all;
if((Cputime_Start>0) && ((Cputime_Start-Cputime_End)>SLEEP_TIME))//10ms
{//a Modbus request has been received
Cputime_Start=0;
//软件复位SCI
SciaRegs.SCICTL1.bit.SWRESET=0;
SciaRegs.SCICTL1.bit.SLEEP=1;
SciaRegs.SCICTL1.bit.SWRESET=1;
SciaRegs.SCIFFTX.bit.SCIRST = 0;
SciaRegs.SCIFFTX.bit.SCIRST = 1; //SCI FIFO resume transmit or receive.
ResolveModbus();//读取数据判决
}
}
Uint16 Crc16(Uint16 *puchMsg, Uint16 usDataLen)
{
Uint16 uchCRCHi = 0xFF ; /* 高CRC字节初始化 */
Uint16 uchCRCLo = 0xFF ; /* 低CRC 字节初始化 */
Uint32 uIndex ; /* CRC循环中的索引 */
while (usDataLen--) /* 传输消息缓冲区 */
{
uIndex = uchCRCHi ^ *puchMsg++ ; /* 计算CRC */
uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex] ;
uchCRCLo = auchCRCLo[uIndex] ;
}
return (uchCRCHi << 8 | uchCRCLo) ;
}
//接收数据从机地址检查
Uint16 AddrCheck(Uint16 AddrStart,Uint16 Num,Uint16 AddrMax)
{
Uint16 Result=0;
//错误2:查询中的数据地址对于从机来说是非法的
if(AddrStart<0 || Num<=0 || AddrStart+Num>AddrMax)
{
ModbusExcep(RdataA[1],ILLEGAL_DATA_ADDRESS);
SciaTx_Response();
Result=1;
}
return Result;
}
//读线圈状态01H
void ReadCoil(void)
{
Uint16 AddrStart=0; //起始地址
Uint16 TempAddr=0;
Uint16 CoilCount=0; //线圈数目
Uint16 ByteCount=0; //读取数据区字节个数
Uint16 CRCCheck=0; //CRC校验码
Uint16 TempData=0;
Uint16 i=0,k=0;
AddrStart = (RdataA[2]<<8) + RdataA[3];
CoilCount = (RdataA[4]<<8) + RdataA[5];
ByteCount = CoilCount/8;
if(CoilCount%8 != 0) //不足8位的,当作一个字节
ByteCount+=1;
if(AddrCheck(AddrStart,CoilCount,MaxQ))
{//有错误
return;
}
TempAddr=AddrStart;
for(k=0;k
{
SdataA[k + 3] = 0;
for(i=0;i<8;i++)
{
GetCoilVal(TempAddr,&TempData);
SdataA[k + 3] |= TempData << i;
TempAddr++;
if(TempAddr >= AddrStart+CoilCount)
{ //读完
goto Label;
}
}
}
Label:
SdataA[0] = Local;
SdataA[1] = RdataA[1];
SdataA[2] = ByteCount;
CRCCheck = Crc16(SdataA,ByteCount+3);
SdataA[ByteCount+3] = CRCCheck >> 8;
SdataA[ByteCount+4] = CRCCheck & 0xff;
SdataA_Num = ByteCount+5;
SciaTx_Response();
}
//读输入状态02H
void ReadInputState(void)
{
Uint16 AddrStart=0; //起始地址
Uint16 TempAddr=0;
Uint16 InputCount=0; //读取数字输入量个数
Uint16 ByteCount=0; //读取数据区字节个数
Uint16 CRCCheck=0; //CRC校验码
Uint16 TempData=0;
Uint16 i=0,k=0;
AddrStart = (RdataA[2]<<8) + RdataA[3];
TempAddr=AddrStart;
InputCount = (RdataA[4]<<8) + RdataA[5];
ByteCount = InputCount / 8;
if(InputCount%8 != 0)
ByteCount+=1;
if(AddrCheck(AddrStart,InputCount,MaxI))
{//有错误
return;
}
for(k=0;k
{
SdataA[k + 3] = 0;
for(i=0;i<8;i++)
{
GetInputVal(TempAddr,&TempData);
SdataA[k + 3] |= TempData << i;
TempAddr++;
if(TempAddr >= AddrStart+InputCount)
{ //读完
goto Label;
}
}
}
Label:
SdataA[0] = Local;
SdataA[1] = RdataA[1];
SdataA[2] = ByteCount;
CRCCheck = Crc16(SdataA,ByteCount+3);
SdataA[ByteCount+3] = CRCCheck >> 8;
SdataA[ByteCount+4] = CRCCheck & 0xff;
SdataA_Num = ByteCount+5;
SciaTx_Response();
}
//读保持寄存器03H
void ReadHoldRegisters(void)
{
Uint16 AddrStart=0; //4X寄存器起始地址
Uint16 TempAddr=0;
Uint16 CRCCheck=0;
Uint16 RegistersCount=0; //要读寄存器的个数
Uint16 ByteCount=0; //字节计?
Uint16 TempData=0;
Uint16 i=0;
AddrStart = (RdataA[2]<<8) + RdataA[3];
TempAddr=AddrStart;
RegistersCount = (RdataA[4]<<8) + RdataA[5];
ByteCount = RegistersCount * 2;
if(AddrCheck(AddrStart,RegistersCount,MaxHold))
{//有错误
return;
}
for(i=0;i
{
GetRegisterVal(TempAddr,&TempData);
SdataA[i+3] = TempData >> 8;
SdataA[i+4] = TempData & 0xff;
}
SdataA[0] = Local;
SdataA[1] = RdataA[1];
SdataA[2] = ByteCount;
CRCCheck = Crc16(SdataA,ByteCount+3);
SdataA[ByteCount+3] = CRCCheck >> 8;
SdataA[ByteCount+4] = CRCCheck & 0xff;
SdataA_Num = ByteCount + 5;
SciaTx_Response();
}//void readRegisters(void)
//读输入寄存器04H
void ReadInputRegisters(void)
{
Uint16 AddrStart=0; //寄存器起始地址
Uint16 TempAddr=0;
Uint16 CRCCheck=0;
Uint16 RegistersCount=0; //要读寄存器的个数
Uint16 ByteCount=0; //字节计数
Uint16 TempData = 0;
Uint16 i=0;
AddrStart = (RdataA[2]<<8) + RdataA[3];
TempAddr=AddrStart;
RegistersCount = (RdataA[4]<<8) + RdataA[5];
ByteCount = RegistersCount * 2;
if(AddrCheck(AddrStart,RegistersCount,MaxAI))
{//有错误
return;
}
for(i=0;i
{
GetInputRegisterVal(TempAddr,&TempData);
SdataA[i+3] = TempData >> 8;
SdataA[i+4] = TempData & 0xff;
}
SdataA[0] = Local;
SdataA[1] = RdataA[1];
SdataA[2] = ByteCount;
CRCCheck = Crc16(SdataA,ByteCount+3);
SdataA[ByteCount+3] = CRCCheck >> 8;
SdataA[ByteCount+4] = CRCCheck & 0xff;
SdataA_Num = ByteCount + 5;
SciaTx_Response();
}
//强制单个线圈
void ForceSingleCoil(void)
{
Uint16 AddrStart=0;
Uint16 TempAddr=0;
Uint16 TempData=0;
Uint16 SetData=0;
Uint16 i=0;
AddrStart = (RdataA[2]<<8) + RdataA[3];
TempAddr = AddrStart;
SetData = (RdataA[4]<<8) + RdataA[5];
if(AddrCheck(AddrStart,1,MaxQ))
{//有错误
return;
}
/////////////////////////////
//这里不同的上位机可能不同,要测试一下该功能?
/////////////////////////////
if(SetData == 0xFF00)
{ //设为ON
TempData = 1;
}
else if(SetData == 0x0000)
{ //设为OFF
TempData = 0;
}
SetCoilVal(TempAddr,TempData);
for(i=0;i
{
SdataA[i] = RdataA[i];
}
SdataA_Num = RdataA_Num;
SciaTx_Response();
}
//设置单个保持寄存器
void PresetSingleRegister(void)
{
Uint16 AddrStart=0;
Uint16 TempAddr=0;
Uint16 SetData=0;
Uint16 i=0;
AddrStart = (RdataA[2]<<8) + RdataA[3];
TempAddr = AddrStart;
SetData = (RdataA[4]<<8) + RdataA[5];
if(AddrCheck(AddrStart,1,MaxHold))
{//有错误
return;
}
SetRegisterVal(TempAddr,SetData);
for(i=0;i
{
SdataA[i] = RdataA[i];
}
SdataA_Num = RdataA_Num;
SciaTx_Response();
}
//设置多个线圈
void ForceMultipleCoils(void)
{
Uint16 AddrStart=0;
Uint16 TempAddr=0;
Uint16 ByteCount=0;
Uint16 SetCount=0;
Uint16 CRCCheck=0;
unsigned TempState=0;
Uint16 i=0,k=0;
AddrStart = (RdataA[2]<<8) + RdataA[3];
TempAddr = AddrStart;
SetCount = (RdataA[4]<<8) + RdataA[5];
ByteCount = RdataA[6];
if(AddrCheck(AddrStart,SetCount,MaxQ))
{//有错误
return;
}
for(k=0;k
{
for(i=0;i<8;i++)
{
TempState = (RdataA[k+7]>>i) & 0x01;
SetCoilVal(TempAddr,TempState);
TempAddr++;
if(TempAddr >= AddrStart+SetCount)
{ //写完
goto Lable1;
}
}
}
Lable1:
SdataA[0] = Local;
SdataA[1] = RdataA[1];
SdataA[2] = AddrStart >> 8;
SdataA[3] = AddrStart & 0xff;
SdataA[4] = SetCount >> 8;
SdataA[5] = SetCount & 0xff;
CRCCheck = Crc16(SdataA,6);
SdataA[6] = CRCCheck >> 8;
SdataA[7] = CRCCheck & 0xff;
SdataA_Num = 8;
SciaTx_Response();
}
//设置多个保持寄存器
void PresetMultipleRegisters(void)
{
Uint16 AddrStart=0;
Uint16 TempAddr=0;
//Uint16 ByteCount=0;//没有使用,屏蔽
Uint16 SetCount=0;
Uint16 CRCCheck=0;
Uint16 TempData=0;
Uint16 i=0;
AddrStart = (RdataA[2]<<8) + RdataA[3];
TempAddr = AddrStart;
SetCount = (RdataA[4]<<8) + RdataA[5];
//ByteCount = RdataA[6];//没有使用,屏蔽
if(AddrCheck(AddrStart,SetCount,MaxHold))
{//有错误
return;
}
for(i=0;i
{
TempData = (RdataA[i*2+7]<<8) + RdataA[i*2+8];
SetRegisterVal(TempAddr,TempData);
}
SdataA[0] = Local;
SdataA[1] = RdataA[1];
SdataA[2] = AddrStart >> 8;
SdataA[3] = AddrStart & 0xff;
SdataA[4] = SetCount >> 8;
SdataA[5] = SetCount & 0xff;
CRCCheck = Crc16(SdataA,6);
SdataA[6] = CRCCheck >> 8;
SdataA[7] = CRCCheck & 0xff;
SdataA_Num = 8;
SciaTx_Response();
}//void presetMultipleRegisters(void)
//掩码设置保持寄存器
void MaskWrite4XRegister(void)
{
Uint16 AddrStart=0;
Uint16 OrMark=0;
Uint16 AndMark=0;
Uint16 i=0;
Uint16 TempData=0;
AddrStart = (RdataA[2]<<8) + RdataA[3];
AndMark = (RdataA[4]<<8) + RdataA[5];
OrMark = (RdataA[6]<<8) + RdataA[7];
if(AddrCheck(AddrStart,1,MaxHold))
{//有错误
return;
}
switch(AddrStart & 0x00ff)
{
case 0:
TempData=nData4Test[0];//将寄存器的值赋给TempData
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[0]=TempData;//将新的TempData值赋给寄存器
break;
case 1:
TempData=nData4Test[1];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[1]=TempData;
break;
case 2:
TempData=nData4Test[2];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[2]=TempData;
break;
case 3:
TempData=nData4Test[3];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[3]=TempData;
break;
case 4:
TempData=nData4Test[4];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[4]=TempData;
break;
case 5:
TempData=nData4Test[5];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[5]=TempData;
break;
case 6:
TempData=nData4Test[6];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[6]=TempData;
break;
case 7:
TempData=nData4Test[7];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[7]=TempData;
break;
case 8:
TempData=nData4Test[8];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[8]=TempData;
break;
case 9:
TempData=nData4Test[9];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[9]=TempData;
break;
case 10:
TempData=nData4Test[10];//将寄存器的值赋给TempData
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[10]=TempData;//将新的TempData值赋给寄存器
break;
case 11:
TempData=nData4Test[11];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[11]=TempData;
break;
case 12:
TempData=nData4Test[12];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[12]=TempData;
break;
case 13:
TempData=nData4Test[13];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[13]=TempData;
break;
case 14:
TempData=nData4Test[14];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[14]=TempData;
break;
case 15:
TempData=nData4Test[15];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[15]=TempData;
break;
case 16:
TempData=nData4Test[16];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[16]=TempData;
break;
case 17:
TempData=nData4Test[17];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[17]=TempData;
break;
case 18:
TempData=nData4Test[18];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[18]=TempData;
break;
case 19:
TempData=nData4Test[19];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[19]=TempData;
break;
case 20:
TempData=nData4Test[20];//将寄存器的值赋给TempData
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[20]=TempData;//将新的TempData值赋给寄存器
break;
case 21:
TempData=nData4Test[21];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[21]=TempData;
break;
case 22:
TempData=nData4Test[22];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[22]=TempData;
break;
case 23:
TempData=nData4Test[23];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[23]=TempData;
break;
case 24:
TempData=nData4Test[24];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[24]=TempData;
break;
case 25:
TempData=nData4Test[25];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[25]=TempData;
break;
case 26:
TempData=nData4Test[26];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[26]=TempData;
break;
case 27:
TempData=nData4Test[27];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[27]=TempData;
break;
case 28:
TempData=nData4Test[28];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[28]=TempData;
break;
case 29:
TempData=nData4Test[29];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[29]=TempData;
break;
case 30:
TempData=nData4Test[30];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[30]=TempData;
break;
case 31:
TempData=nData4Test[31];
TempData=MarkCompute(TempData,AndMark,OrMark);
nData4Test[31]=TempData;
break;
}
for(i=0;i
{
SdataA[i] = RdataA[i];
}
SdataA_Num = RdataA_Num;
SciaTx_Response();
}
#pragma CODE_SECTION(GetCoilVal, "MODBUS_CODE")
//取线圈状态
void GetCoilVal(Uint16 Addr,Uint16 *ReadState)
{
switch(Addr & 0xff)
{
case 0:
*ReadState=nData0Test[0];
break;
case 1:
*ReadState=nData0Test[1];
break;
case 2:
*ReadState=nData0Test[2];
break;
case 3:
*ReadState=nData0Test[3];
break;
case 4:
*ReadState=nData0Test[4];
break;
case 5:
*ReadState=nData0Test[5];
break;
case 6:
*ReadState=nData0Test[6];
break;
case 7:
*ReadState=nData0Test[7];
break;
case 8:
*ReadState=nData0Test[8];
break;
case 9:
*ReadState=nData0Test[9];
break;
case 10:
*ReadState=nData0Test[10];
break;
case 11:
*ReadState=nData0Test[11];
break;
case 12:
*ReadState=nData0Test[12];
break;
case 13:
*ReadState=nData0Test[13];
break;
case 14:
*ReadState=nData0Test[14];
break;
case 15:
*ReadState=nData0Test[15];
break;
case 16:
*ReadState=nData0Test[16];
break;
case 17:
*ReadState=nData0Test[17];
break;
case 18:
*ReadState=nData0Test[18];
break;
case 19:
*ReadState=nData0Test[19];
break;
case 20:
*ReadState=nData0Test[20];
break;
case 21:
*ReadState=nData0Test[21];
break;
case 22:
*ReadState=nData0Test[22];
break;
case 23:
*ReadState=nData0Test[23];
break;
case 24:
*ReadState=nData0Test[24];
break;
case 25:
*ReadState=nData0Test[25];
break;
case 26:
*ReadState=nData0Test[26];
break;
case 27:
*ReadState=nData0Test[27];
break;
case 28:
*ReadState=nData0Test[28];
break;
case 29:
*ReadState=nData0Test[29];
break;
case 30:
*ReadState=nData0Test[30];
break;
case 31:
*ReadState=nData0Test[31];
break;
/* case 32:
*ReadState=nData0Test[2];
break;
case 33:
*ReadState=nData0Test[33];
break;
case 34:
*ReadState=nData0Test[34];
break;
case 35:
*ReadState=nData0Test[35];
break;
case 36:
*ReadState=nData0Test[36];
break;
case 37:
*ReadState=nData0Test[37];
break;
case 38:
*ReadState=nData0Test[38];
break;
case 39:
*ReadState=nData0Test[39];
break;
case 40:
*ReadState=nData0Test[40];
break;
case 41:
*ReadState=nData0Test[41];
break;
case 42:
*ReadState=nData0Test[42];
break;
case 43:
*ReadState=nData0Test[43];
break;
case 44:
*ReadState=nData0Test[44];
break;
case 45:
*ReadState=nData0Test[45];
break;
case 46:
*ReadState=nData0Test[46];
break;
case 47:
*ReadState=nData0Test[47];
break;
case 48:
*ReadState=nData0Test[48];
break;
case 49:
*ReadState=nData0Test[49];
break;
case 50:
*ReadState=nData0Test[50];
break;
case 51:
*ReadState=nData0Test[51];
break;
case 52:
*ReadState=nData0Test[52];
break;
case 53:
*ReadState=nData0Test[53];
break;
case 54:
*ReadState=nData0Test[54];
break;
case 55:
*ReadState=nData0Test[55];
break;
case 56:
*ReadState=nData0Test[56];
break;
case 57:
*ReadState=nData0Test[57];
break;
case 58:
*ReadState=nData0Test[58];
break;
case 59:
*ReadState=nData0Test[59];
break;
case 60:
*ReadState=nData0Test[60];
break;
case 61:
*ReadState=nData0Test[61];
break;
case 62:
*ReadState=nData0Test[62];
break;
case 63:
*ReadState=nData0Test[63];
break;
case 64:
*ReadState=nData0Test[64];
break;
case 65:
*ReadState=nData0Test[65];
break;
case 66:
*ReadState=nData0Test[66];
break;
case 67:
*ReadState=nData0Test[67];
break;
case 68:
*ReadState=nData0Test[68];
break;
case 69:
*ReadState=nData0Test[69];
break;
case 70:
*ReadState=nData0Test[70];
break;
case 71:
*ReadState=nData0Test[71];
break;
case 72:
*ReadState=nData0Test[72];
break;
case 73:
*ReadState=nData0Test[73];
break;
case 74:
*ReadState=nData0Test[74];
break;
case 75:
*ReadState=nData0Test[75];
break;
case 76:
*ReadState=nData0Test[76];
break;
case 77:
*ReadState=nData0Test[77];
break;
case 78:
*ReadState=nData0Test[78];
break;
case 79:
*ReadState=nData0Test[79];
break;
case 80:
*ReadState=nData0Test[80];
break;
case 81:
*ReadState=nData0Test[81];
break;
case 82:
*ReadState=nData0Test[82];
break;
case 83:
*ReadState=nData0Test[83];
break;
case 84:
*ReadState=nData0Test[84];
break;
case 85:
*ReadState=nData0Test[85];
break;
case 86:
*ReadState=nData0Test[86];
break;
case 87:
*ReadState=nData0Test[87];
break;
case 88:
*ReadState=nData0Test[88];
break;
case 89:
*ReadState=nData0Test[89];
break;
case 90:
*ReadState=nData0Test[90];
break;
case 91:
*ReadState=nData0Test[91];
break;
case 92:
*ReadState=nData0Test[92];
break;
case 93:
*ReadState=nData0Test[93];
break;
case 94:
*ReadState=nData0Test[94];
break;
case 95:
*ReadState=nData0Test[95];
break;
case 96:
*ReadState=nData0Test[96];
break;
case 97:
*ReadState=nData0Test[97];
break;
case 98:
*ReadState=nData0Test[98];
break;
case 99:
*ReadState=nData0Test[99];
break;
case 100:
*ReadState=nData0Test[100];
break;
case 101:
*ReadState=nData0Test[101];
break;
case 102:
*ReadState=nData0Test[102];
break;
case 103:
*ReadState=nData0Test[103];
break;
case 104:
*ReadState=nData0Test[104];
break;
case 105:
*ReadState=nData0Test[105];
break;
case 106:
*ReadState=nData0Test[106];
break;
case 107:
*ReadState=nData0Test[107];
break;
case 108:
*ReadState=nData0Test[108];
break;
case 109:
*ReadState=nData0Test[109];
break;
case 110:
*ReadState=nData0Test[110];
break;
case 111:
*ReadState=nData0Test[111];
break;
case 112:
*ReadState=nData0Test[112];
break;
case 113:
*ReadState=nData0Test[113];
break;
case 114:
*ReadState=nData0Test[114];
break;
case 115:
*ReadState=nData0Test[115];
break;
case 116:
*ReadState=nData0Test[116];
break;
case 117:
*ReadState=nData0Test[117];
break;
case 118:
*ReadState=nData0Test[118];
break;
case 119:
*ReadState=nData0Test[119];
break;
case 120:
*ReadState=nData0Test[120];
break;
case 121:
*ReadState=nData0Test[121];
break;
case 122:
*ReadState=nData0Test[122];
break;
case 123:
*ReadState=nData0Test[123];
break;
case 124:
*ReadState=nData0Test[124];
break;
case 125:
*ReadState=nData0Test[125];
break;
case 126:
*ReadState=nData0Test[126];
break;
case 127:
*ReadState=nData0Test[127];
break;*/
}
}
#pragma CODE_SECTION(GetInputVal, "MODBUS_CODE")
//取开关输入量状态
void GetInputVal(Uint16 Addr,Uint16 *ReadState)
{
switch(Addr & 0xff)
{
case 0:
*ReadState=nData1Test[0];
nData1Test[0]=!nData1Test[0];
break;
case 1:
*ReadState=nData1Test[1];
nData1Test[1]=!nData1Test[1];
break;
case 2:
*ReadState=nData1Test[2];
nData1Test[2]=!nData1Test[2];
break;
case 3:
*ReadState=nData1Test[3];
nData1Test[3]=!nData1Test[3];
break;
case 4:
*ReadState=nData1Test[4];
nData1Test[4]=!nData1Test[4];
break;
case 5:
*ReadState=nData1Test[5];
nData1Test[5]=!nData1Test[5];
break;
case 6:
*ReadState=nData1Test[6];
nData1Test[6]=!nData1Test[6];
break;
case 7:
*ReadState=nData1Test[7];
nData1Test[7]=!nData1Test[7];
break;
case 8:
*ReadState=nData1Test[8];
nData1Test[8]=!nData1Test[8];
break;
case 9:
*ReadState=nData1Test[9];
nData1Test[9]=!nData1Test[9];
break;
case 10:
*ReadState=nData1Test[10];
nData1Test[10]=!nData1Test[10];
break;
case 11:
*ReadState=nData1Test[11];
nData1Test[11]=!nData1Test[11];
break;
case 12:
*ReadState=nData1Test[12];
nData1Test[12]=!nData1Test[12];
break;
case 13:
*ReadState=nData1Test[13];
nData1Test[13]=!nData1Test[13];
break;
case 14:
*ReadState=nData1Test[14];
nData1Test[14]=!nData1Test[14];
break;
case 15:
*ReadState=nData1Test[15];
nData1Test[15]=!nData1Test[15];
break;
case 16:
*ReadState=nData1Test[16];
nData1Test[16]=!nData1Test[16];
break;
case 17:
*ReadState=nData1Test[17];
nData1Test[17]=!nData1Test[17];
break;
case 18:
*ReadState=nData1Test[18];
nData1Test[18]=!nData1Test[18];
break;
case 19:
*ReadState=nData1Test[19];
nData1Test[19]=!nData1Test[19];
break;
case 20:
*ReadState=nData1Test[20];
nData1Test[20]=!nData1Test[20];
break;
case 21:
*ReadState=nData1Test[21];
nData1Test[21]=!nData1Test[21];
break;
case 22:
*ReadState=nData1Test[22];
nData1Test[22]=!nData1Test[22];
break;
case 23:
*ReadState=nData1Test[23];
nData1Test[23]=!nData1Test[23];
break;
case 24:
*ReadState=nData1Test[24];
nData1Test[24]=!nData1Test[24];
break;
case 25:
*ReadState=nData1Test[25];
nData1Test[25]=!nData1Test[25];
break;
case 26:
*ReadState=nData1Test[26];
nData1Test[26]=!nData1Test[26];
break;
case 27:
*ReadState=nData1Test[27];
nData1Test[27]=!nData1Test[27];
break;
case 28:
*ReadState=nData1Test[28];
nData1Test[28]=!nData1Test[28];
break;
case 29:
*ReadState=nData1Test[29];
nData1Test[29]=!nData1Test[29];
break;
case 30:
*ReadState=nData1Test[30];
nData1Test[30]=!nData1Test[30];
break;
case 31:
*ReadState=nData1Test[31];
nData1Test[31]=nData1Test[31];
break;
/* case 32:
*ReadState=nData1Test[32];
nData1Test[32]=nData1Test[32];
break;
case 33:
*ReadState=nData1Test[33];
nData1Test[33]=nData1Test[33];
break;
case 34:
*ReadState=nData1Test[34];
nData1Test[34]=nData1Test[34];
break;
case 35:
*ReadState=nData1Test[35];
nData1Test[35]=nData1Test[35];
break;
case 36:
*ReadState=nData1Test[36];
nData1Test[36]=nData1Test[36];
break;
case 37:
*ReadState=nData1Test[37];
nData1Test[37]=nData1Test[37];
break;
case 38:
*ReadState=nData1Test[38];
nData1Test[38]=nData1Test[38];
break;
case 39:
*ReadState=nData1Test[39];
nData1Test[39]=nData1Test[39];
break;
case 40:
*ReadState=nData1Test[40];
nData1Test[40]=nData1Test[40];
break;
case 41:
*ReadState=nData1Test[41];
nData1Test[41]=nData1Test[41];
break;
case 42:
*ReadState=nData1Test[42];
nData1Test[42]=nData1Test[42];
break;
case 43:
*ReadState=nData1Test[43];
nData1Test[43]=nData1Test[43];
break;
case 44:
*ReadState=nData1Test[44];
nData1Test[44]=nData1Test[44];
break;
case 45:
*ReadState=nData1Test[45];
nData1Test[45]=nData1Test[45];
break;
case 46:
*ReadState=nData1Test[46];
nData1Test[46]=nData1Test[46];
break;
case 47:
*ReadState=nData1Test[47];
nData1Test[47]=nData1Test[47];
break;
case 48:
*ReadState=nData1Test[48];
nData1Test[48]=nData1Test[48];
break;
case 49:
*ReadState=nData1Test[49];
nData1Test[49]=nData1Test[49];
break;
case 50:
*ReadState=nData1Test[50];
nData1Test[50]=nData1Test[50];
break;
case 51:
*ReadState=nData1Test[51];
nData1Test[51]=nData1Test[51];
break;
case 52:
*ReadState=nData1Test[52];
nData1Test[52]=nData1Test[52];
break;
case 53:
*ReadState=nData1Test[53];
nData1Test[53]=nData1Test[53];
break;
case 54:
*ReadState=nData1Test[54];
nData1Test[54]=nData1Test[54];
break;
case 55:
*ReadState=nData1Test[55];
nData1Test[55]=nData1Test[55];
break;
case 56:
*ReadState=nData1Test[56];
nData1Test[56]=nData1Test[56];
break;
case 57:
*ReadState=nData1Test[57];
nData1Test[57]=nData1Test[57];
break;
case 58:
*ReadState=nData1Test[58];
nData1Test[58]=nData1Test[58];
break;
case 59:
*ReadState=nData1Test[59];
nData1Test[59]=nData1Test[59];
break;
case 60:
*ReadState=nData1Test[60];
nData1Test[60]=nData1Test[60];
break;
case 61:
*ReadState=nData1Test[61];
nData1Test[61]=nData1Test[61];
break;
case 62:
*ReadState=nData1Test[62];
nData1Test[62]=nData1Test[62];
break;
case 63:
*ReadState=nData1Test[63];
nData1Test[63]=nData1Test[63];
break;
case 64:
*ReadState=nData1Test[64];
nData1Test[64]=nData1Test[64];
break;
case 65:
*ReadState=nData1Test[65];
nData1Test[65]=nData1Test[65];
break;
case 66:
*ReadState=nData1Test[66];
nData1Test[66]=nData1Test[66];
break;
case 67:
*ReadState=nData1Test[67];
nData1Test[67]=nData1Test[67];
break;
case 68:
*ReadState=nData1Test[68];
nData1Test[68]=nData1Test[68];
break;
case 69:
*ReadState=nData1Test[69];
nData1Test[69]=nData1Test[69];
break;
case 70:
*ReadState=nData1Test[70];
nData1Test[70]=nData1Test[70];
break;
case 71:
*ReadState=nData1Test[71];
nData1Test[71]=nData1Test[71];
break;
case 72:
*ReadState=nData1Test[72];
nData1Test[72]=nData1Test[72];
break;
case 73:
*ReadState=nData1Test[73];
nData1Test[73]=nData1Test[73];
break;
case 74:
*ReadState=nData1Test[74];
nData1Test[74]=nData1Test[74];
break;
case 75:
*ReadState=nData1Test[75];
nData1Test[75]=nData1Test[75];
break;
case 76:
*ReadState=nData1Test[76];
nData1Test[76]=nData1Test[76];
break;
case 77:
*ReadState=nData1Test[77];
nData1Test[77]=nData1Test[77];
break;
case 78:
*ReadState=nData1Test[78];
nData1Test[78]=nData1Test[78];
break;
case 79:
*ReadState=nData1Test[79];
nData1Test[79]=nData1Test[79];
break;
case 80:
*ReadState=nData1Test[80];
nData1Test[80]=nData1Test[80];
break;
case 81:
*ReadState=nData1Test[81];
nData1Test[81]=nData1Test[81];
break;
case 82:
*ReadState=nData1Test[82];
nData1Test[82]=nData1Test[82];
break;
case 83:
*ReadState=nData1Test[83];
nData1Test[83]=nData1Test[83];
break;
case 84:
*ReadState=nData1Test[84];
nData1Test[84]=nData1Test[84];
break;
case 85:
*ReadState=nData1Test[85];
nData1Test[85]=nData1Test[85];
break;
case 86:
*ReadState=nData1Test[86];
nData1Test[86]=nData1Test[86];
break;
case 87:
*ReadState=nData1Test[87];
nData1Test[87]=nData1Test[87];
break;
case 88:
*ReadState=nData1Test[88];
nData1Test[88]=nData1Test[88];
break;
case 89:
*ReadState=nData1Test[89];
nData1Test[89]=nData1Test[89];
break;
case 90:
*ReadState=nData1Test[90];
nData1Test[90]=nData1Test[90];
break;
case 91:
*ReadState=nData1Test[91];
nData1Test[91]=nData1Test[91];
break;
case 92:
*ReadState=nData1Test[92];
nData1Test[92]=nData1Test[92];
break;
case 93:
*ReadState=nData1Test[93];
nData1Test[93]=nData1Test[93];
break;
case 94:
*ReadState=nData1Test[94];
nData1Test[94]=nData1Test[94];
break;
case 95:
*ReadState=nData1Test[95];
nData1Test[95]=nData1Test[95];
break;
case 96:
*ReadState=nData1Test[96];
nData1Test[96]=nData1Test[96];
break;
case 97:
*ReadState=nData1Test[97];
nData1Test[97]=nData1Test[97];
break;
case 98:
*ReadState=nData1Test[98];
nData1Test[98]=nData1Test[98];
break;
case 99:
*ReadState=nData1Test[99];
nData1Test[99]=nData1Test[99];
break;
case 100:
*ReadState=nData1Test[100];
nData1Test[100]=nData1Test[100];
break;
case 101:
*ReadState=nData1Test[101];
nData1Test[101]=nData1Test[101];
break;
case 102:
*ReadState=nData1Test[102];
nData1Test[102]=nData1Test[102];
break;
case 103:
*ReadState=nData1Test[103];
nData1Test[103]=nData1Test[103];
break;
case 104:
*ReadState=nData1Test[104];
nData1Test[104]=nData1Test[104];
break;
case 105:
*ReadState=nData1Test[105];
nData1Test[105]=nData1Test[105];
break;
case 106:
*ReadState=nData1Test[106];
nData1Test[106]=nData1Test[106];
break;
case 107:
*ReadState=nData1Test[107];
nData1Test[107]=nData1Test[107];
break;
case 108:
*ReadState=nData1Test[108];
nData1Test[108]=nData1Test[108];
break;
case 109:
*ReadState=nData1Test[109];
nData1Test[109]=nData1Test[109];
break;
case 110:
*ReadState=nData1Test[110];
nData1Test[110]=nData1Test[110];
break;
case 111:
*ReadState=nData1Test[111];
nData1Test[111]=nData1Test[111];
break;
case 112:
*ReadState=nData1Test[112];
nData1Test[112]=nData1Test[112];
break;
case 113:
*ReadState=nData1Test[113];
nData1Test[113]=nData1Test[113];
break;
case 114:
*ReadState=nData1Test[114];
nData1Test[114]=nData1Test[114];
break;
case 115:
*ReadState=nData1Test[115];
nData1Test[115]=nData1Test[115];
break;
case 116:
*ReadState=nData1Test[116];
nData1Test[116]=nData1Test[116];
break;
case 117:
*ReadState=nData1Test[117];
nData1Test[117]=nData1Test[117];
break;
case 118:
*ReadState=nData1Test[118];
nData1Test[118]=nData1Test[118];
break;
case 119:
*ReadState=nData1Test[119];
nData1Test[119]=nData1Test[119];
break;
case 120:
*ReadState=nData1Test[120];
nData1Test[120]=nData1Test[120];
break;
case 121:
*ReadState=nData1Test[121];
nData1Test[121]=nData1Test[121];
break;
case 122:
*ReadState=nData1Test[122];
nData1Test[122]=nData1Test[122];
break;
case 123:
*ReadState=nData1Test[123];
nData1Test[123]=nData1Test[123];
break;
case 124:
*ReadState=nData1Test[124];
nData1Test[124]=nData1Test[124];
break;
case 125:
*ReadState=nData1Test[125];
nData1Test[125]=nData1Test[125];
break;
case 126:
*ReadState=nData1Test[126];
nData1Test[126]=nData1Test[126];
break;
case 127:
*ReadState=nData1Test[127];
nData1Test[127]=nData1Test[127];
break;*/
}
}
#pragma CODE_SECTION(SetCoilVal, "MODBUS_CODE")
//设定线圈状态
void SetCoilVal(Uint16 Addr,Uint16 SetState)
{
switch(Addr & 0xff)
{
case 0:
nData0Test[0]=SetState;
break;
case 1:
nData0Test[1]=SetState;
break;
case 2:
nData0Test[2]=SetState;
break;
case 3:
nData0Test[3]=SetState;
break;
case 4:
nData0Test[4]=SetState;
break;
case 5:
nData0Test[5]=SetState;