DSP28335 高速modbus代码实现

程序特点

  • 不使用while循环
  • 速度尽可能快速
  • 除去程序运行时间,没有多余等待时间
  • 优化CRC校验方式

头文件modbus.h

#ifndef MODBUS_H
#define MODBUS_H

#define ModbusSlaveAddress 1

typedef enum {
    MB_FUNC_READ_COIL = 1,
    MB_FUNC_READ_INPUT,
    MB_FUNC_READ_HOLDINGREGISTERS,
    MB_FUNC_READ_INPUTREGISTERS,
    MB_FUNC_FORCE_COIL,
    MB_FUNC_WRITE_HOLDINGREGISTER,
    MB_FUNC_FORCE_NCOILS = 15,
    MB_FUNC_WRITE_NREGISTERS = 16
} ModbusFunctionCode;

typedef enum {
    MB_ERROR_ILLEGALFUNC = 1,
    MB_ERROR_ILLEGALADDR,
    MB_ERROR_ILLEGALDATA,
    MB_ERROR_SLVFAILURE,
    MB_ERROR_ACKNOWLEDGE,
    MB_ERROR_SLVBUSY,
    MB_ERROR_NEGACKNOWLEDGE,
    MB_ERROR_MEMPARITY,
    MB_ERROR_GATEWAYPATHUN
} ModbusError;

// State machine constants
typedef enum {
    MB_CREATE,
    MB_START,
    MB_TIMER_T35_WAIT,
    MB_IDLE,
    MB_RECEIVE,
    MB_PROCESS,
    MB_TRANSMIT,
    MB_DESTROY
} ModbusState;



typedef struct ModbusData ModbusData;
struct ModbusData {
    Uint16 slaveAddress;
    Uint16 functionCode;
    Uint32 contentIdx;
    Uint32 numofregisters;
    Uint16 content[20];
    Uint16 size;
    Uint16 crc;
};

void KD_Modbus_Init(void);

typedef struct ModbusCoilsMap ModbusCoilsMap;
struct ModbusCoilsMap{
    Uint16   dummy1;
    Uint16   dummy2;
    Uint16   dummy3;
    Uint16   dummy4;
    Uint16   dummy5;
    Uint16   dummy6;
    Uint16   dummy7;
    Uint16   dummy8;
    Uint16   dummy9;
    Uint16   dummy10;
    Uint16   dummy11;
    Uint16   dummy12;
    Uint16   dummy13;
    Uint16   dummy14;
    Uint16   dummy15;
    Uint16   dummy16;
    Uint16   dummy17;
    Uint16   dummy18;
    Uint16   dummy19;
    Uint16   dummy20;
    Uint16   dummy21;
    Uint16   dummy22;
    Uint16   dummy23;
    Uint16   dummy24;
    Uint16   dummy25;
    Uint16   dummy26;
    Uint16   dummy27;
    Uint16   dummy28;
    Uint16   dummy29;
    Uint16   dummy30;
    Uint16   dummy31;
    Uint16   dummy32;
    Uint16   dummy33;
    Uint16   dummy34;
    Uint16   dummy35;
    Uint16   dummy36;
    Uint16   dummy37;
    Uint16   dummy38;
    Uint16   dummy39;
    Uint16   dummy40;
};

typedef struct ModbusInputsMap ModbusInputsMap;
struct ModbusInputsMap{
    Uint16   dummy1;
    Uint16   dummy2;
    Uint16   dummy3;
    Uint16   dummy4;
    Uint16   dummy5;
    Uint16   dummy6;
    Uint16   dummy7;
    Uint16   dummy8;
    Uint16   dummy9;
    Uint16   dummy10;
    Uint16   dummy11;
    Uint16   dummy12;
    Uint16   dummy13;
    Uint16   dummy14;
    Uint16   dummy15;
    Uint16   dummy16;
    Uint16   dummy17;
    Uint16   dummy18;
    Uint16   dummy19;
    Uint16   dummy20;
    Uint16   dummy21;
    Uint16   dummy22;
    Uint16   dummy23;
    Uint16   dummy24;
    Uint16   dummy25;
    Uint16   dummy26;
    Uint16   dummy27;
    Uint16   dummy28;
    Uint16   dummy29;
    Uint16   dummy30;
    Uint16   dummy31;
    Uint16   dummy32;
    Uint16   dummy33;
    Uint16   dummy34;
    Uint16   dummy35;
    Uint16   dummy36;
    Uint16   dummy37;
    Uint16   dummy38;
    Uint16   dummy39;
    Uint16   dummy40;
};

typedef struct ModbusHoldingRegistersMap ModbusHoldingRegistersMap;
struct ModbusHoldingRegistersMap {
    Uint16  dummy1;
    Uint16  dummy2;
    Uint16  dummy3;
    Uint16  dummy4;
    Uint16  dummy5;
    Uint16  dummy6;
    Uint16  dummy7;
    Uint16  dummy8;
    Uint16  dummy9;
    Uint16  dummy10;
    Uint16  dummy11;
    Uint16  dummy12;
    Uint16  dummy13;
    Uint16  dummy14;
    Uint16  dummy15;
    Uint16  dummy16;
    Uint16  dummy17;
    Uint16  dummy18;
    Uint16  dummy19;
    Uint16  dummy20;
    Uint16  dummy21;
    Uint16  dummy22;
    Uint16  dummy23;
    Uint16  dummy24;
    Uint16  dummy25;
    Uint16  dummy26;
    Uint16  dummy27;
    Uint16  dummy28;
    Uint16  dummy29;
    Uint16  dummy30;
    Uint16  dummy31;
    Uint16  dummy32;
    Uint16  dummy33;
    Uint16  dummy34;
    Uint16  dummy35;
    Uint16  dummy36;
    Uint16  dummy37;
    Uint16  dummy38;
    Uint16  dummy39;
    Uint16  dummy40;
};

typedef struct ModbusInputRegistersMap ModbusInputRegistersMap;
struct ModbusInputRegistersMap {
    Uint16  dummy1;
    Uint16  dummy2;
    Uint16  dummy3;
    Uint16  dummy4;
    Uint16  dummy5;
    Uint16  dummy6;
    Uint16  dummy7;
    Uint16  dummy8;
    Uint16  dummy9;
    Uint16  dummy10;
    Uint16  dummy11;
    Uint16  dummy12;
    Uint16  dummy13;
    Uint16  dummy14;
    Uint16  dummy15;
    Uint16  dummy16;
    Uint16  dummy17;
    Uint16  dummy18;
    Uint16  dummy19;
    Uint16  dummy20;
    Uint16  dummy21;
    Uint16  dummy22;
    Uint16  dummy23;
    Uint16  dummy24;
    Uint16  dummy25;
    Uint16  dummy26;
    Uint16  dummy27;
    Uint16  dummy28;
    Uint16  dummy29;
    Uint16  dummy30;
    Uint16  dummy31;
    Uint16  dummy32;
    Uint16  dummy33;
    Uint16  dummy34;
    Uint16  dummy35;
    Uint16  dummy36;
    Uint16  dummy37;
    Uint16  dummy38;
    Uint16  dummy39;
    Uint16  dummy40;
};

typedef struct ModbusSlaveStruc{
    ModbusState state;
    Uint16 rdata[30];
    Uint16 sdata[100];
    Uint16 recvlen;
    Uint16 sendlen;
    ModbusCoilsMap coils,*pcoils;
    ModbusInputsMap inputs,*pinputs;
    ModbusHoldingRegistersMap holdingRegisters,*pholdingRegisters;
    ModbusInputRegistersMap inputRegisters,*pinputRegisters;
}MBSLSTRU;

#endif

源文件modbus.c

/*********************************************************
 * 处理一个数据需要8个us,所以尽可能一次多访问几个数据,可以一次把40个寄存器读完
 * 发送命令计算校验码时,前后都不能有多余字符,不然校验会出错
 * 波特率460800,暂时不修改
 * ***************************************************************/
#include 
#include 
#include 
#include   //用于清零数组

MBSLSTRU public_struc_modbus;

void receieve_handler(MBSLSTRU *pModbusSlave);
//void Scic_xmit(int a);
Uint16 CRC16 (Uint16 *nData, Uint16 wLength);

#define bufferdepth 0x10
//CycleQueue ScicRecvMsgQueue;
//struct ScicRecvMsgQueueStruc ScicRecvMsgBox[ScicRecvMsgLen], *pScicRecvMsgQueueStruc;


__interrupt void scicTxFifoIsr(void)
{
    Uint16 i, Temp;

    if((public_struc_modbus.sendlen&0xff)>bufferdepth)
    {
        Temp = bufferdepth;
        ScicRegs.SCIFFTX.bit.TXFFIL = 0;

        for(i=0; i< Temp; i++)
        {
            ScicRegs.SCITXBUF = public_struc_modbus.sdata[i + bufferdepth*(public_struc_modbus.sendlen>>12)];
        }
        public_struc_modbus.sendlen = public_struc_modbus.sendlen - bufferdepth + 0x1000;
    }
    else
    {
        Temp = (public_struc_modbus.sendlen&0xff);
        if((public_struc_modbus.sendlen&0xff)>0)
        {
            ScicRegs.SCIFFTX.bit.TXFFIL = 0;

            for(i=0; i< Temp; i++)
            {
                ScicRegs.SCITXBUF = public_struc_modbus.sdata[i + bufferdepth*(public_struc_modbus.sendlen>>12)];
            }
            public_struc_modbus.sendlen = 0;
        }
        else
        {
            if(public_struc_modbus.sendlen==0)
            {
                ScicRegs.SCIFFTX.bit.TXFFIENA = 0;
            }
        }
    }

    ScicRegs.SCIFFTX.bit.TXFFINTCLR=1;  // Clear Interrupt flag
    PieCtrlRegs.PIEACK.bit.ACK8=1;
}


__interrupt void scicRxFifoIsr(void)
{
    Uint16 i;
    if(ScicRegs.SCIFFRX.bit.RXFFIL != 1)
    {
        if(public_struc_modbus.rdata[1]==0)
        {
            for(i=1;i<8;i++)
            {
                public_struc_modbus.rdata[i]=ScicRegs.SCIRXBUF.all;  // Read data
            }
        }
        if((public_struc_modbus.rdata[1]==15)||(public_struc_modbus.rdata[1]==16))
        {
            if(public_struc_modbus.recvlen == 0)
            {
                public_struc_modbus.recvlen = public_struc_modbus.rdata[6] + 9 + 0x1000;
                ScicRegs.SCIFFRX.bit.RXFFIL = 8;
                i = public_struc_modbus.recvlen&0xff - ((public_struc_modbus.recvlen>>12)*8);
                if(i>8)
                {ScicRegs.SCIFFRX.bit.RXFFIL = 8;}
                else
                {ScicRegs.SCIFFRX.bit.RXFFIL = i;}
            }
            else
            {

                if((public_struc_modbus.recvlen>>12)<((public_struc_modbus.recvlen&0xFF)/8))
                {
                    for(i=0;i<8;i++)
                    {
                        public_struc_modbus.rdata[i+(public_struc_modbus.recvlen>>12)*8]=ScicRegs.SCIRXBUF.all;  // Read data
                    }
                    public_struc_modbus.recvlen = public_struc_modbus.recvlen + 0x1000;
                    i = public_struc_modbus.recvlen&0xff - ((public_struc_modbus.recvlen>>12)*8);
                    if(i>8)
                    {ScicRegs.SCIFFRX.bit.RXFFIL = 8;}
                    else
                    {ScicRegs.SCIFFRX.bit.RXFFIL = i;}
                }
                else
                {
                    for(i=0;i<((public_struc_modbus.recvlen&0xFF)%8);i++)
                    {
                        public_struc_modbus.rdata[i+(public_struc_modbus.recvlen>>12)*8]=ScicRegs.SCIRXBUF.all;  // Read data
                    }
                    if((public_struc_modbus.recvlen&0xFF) <= (i+(public_struc_modbus.recvlen>>12)*8))
                    {
                        receieve_handler(&public_struc_modbus);
                        public_struc_modbus.rdata[1] = 0;
                        ScicRegs.SCIFFRX.bit.RXFFIL = 1;
                        public_struc_modbus.recvlen = 0;
                    }
                }
            }
        }
        else
        {
            public_struc_modbus.recvlen = 8;

            if((public_struc_modbus.rdata[1]>=1)&&(public_struc_modbus.rdata[1]<=6))
            {
                receieve_handler(&public_struc_modbus);
                public_struc_modbus.rdata[1] = 0;
                public_struc_modbus.recvlen = 0;
                ScicRegs.SCIFFRX.bit.RXFFIL = 1;
            }
            else
            {
                    receieve_handler(&public_struc_modbus);
                    public_struc_modbus.rdata[1] = 0;
                    ScicRegs.SCIFFRX.bit.RXFFIL = 1;
            }
        }
    }
    else
    {
        public_struc_modbus.rdata[0]=ScicRegs.SCIRXBUF.all;
        if(public_struc_modbus.rdata[0] == ModbusSlaveAddress)
        {ScicRegs.SCIFFRX.bit.RXFFIL = 7;}
        else
        {ScicRegs.SCIFFRX.bit.RXFFIL = 1;}
    }


    ScicRegs.SCIFFRX.bit.RXFFOVRCLR=1;  // Clear Overflow flag
    ScicRegs.SCIFFRX.bit.RXFFINTCLR=1;  // Clear Interrupt flag

    PieCtrlRegs.PIEACK.bit.ACK8=1;
}

void KD_Modbus_Init(void)
{
    InitScicGpio();

    InitSci();

    EALLOW;
    PieVectTable.SCIRXINTC = &scicRxFifoIsr;
    PieVectTable.SCITXINTC = &scicTxFifoIsr;
    EDIS;

    PieCtrlRegs.PIECTRL.bit.ENPIE = 1;
    PieCtrlRegs.PIEIER8.bit.INTx5=1;     // PIE Group 9, int1
    PieCtrlRegs.PIEIER8.bit.INTx6=1;     // PIE Group 9, INT2
    IER|=M_INT8; // Enable CPU INT

    public_struc_modbus.recvlen = 0;
    public_struc_modbus.sendlen = 0;

    Uint16 i, *pcoils_sub, *pinputs_sub, *pholdingRegisters_sub, *pinputRegisters_sub;
    public_struc_modbus.pcoils = &public_struc_modbus.coils;
    public_struc_modbus.pinputs = &public_struc_modbus.inputs;
    public_struc_modbus.pholdingRegisters = &public_struc_modbus.holdingRegisters;
    public_struc_modbus.pinputRegisters = &public_struc_modbus.inputRegisters;

    pcoils_sub =(Uint16 *) public_struc_modbus.pcoils;
    pinputs_sub =(Uint16 *) public_struc_modbus.pinputs;
    pholdingRegisters_sub =(Uint16 *) public_struc_modbus.pholdingRegisters;
    pinputRegisters_sub =(Uint16 *) public_struc_modbus.pinputRegisters;

    for(i=0;i<sizeof(public_struc_modbus.coils);i++)
    {(i%2==1)?(*(pcoils_sub+i)=1):(*(pcoils_sub+i)=0);}
    for(i=0;i<sizeof(public_struc_modbus.inputs);i++)
    {(i%2==0)?(*(pinputs_sub+i)=1):(*(pinputs_sub+i)=0);}
    for(i=0;i<sizeof(public_struc_modbus.holdingRegisters);i++)
    {*(pholdingRegisters_sub+i)=i+1;}
    for(i=0;i<sizeof(public_struc_modbus.inputRegisters);i++)
    {*(pinputRegisters_sub+i)=i+1;}

    memset(public_struc_modbus.rdata,0,sizeof(public_struc_modbus.rdata));
}

void receieve_handler(MBSLSTRU *pModbusSlave)
{
    Uint16 i, lentemp = public_struc_modbus.recvlen & 0xff;
    ModbusData pTempData;
    Uint16 *pcoils_sub, *pinputs_sub, *pholdingRegisters_sub, *pinputRegisters_sub;
//    struct ScicRecvMsgQueueStruc KD_ScicRecvMsg;

    pcoils_sub =(Uint16 *) pModbusSlave->pcoils;
    pinputs_sub =(Uint16 *) pModbusSlave->pinputs;
    pholdingRegisters_sub =(Uint16 *) pModbusSlave->pholdingRegisters;
    pinputRegisters_sub =(Uint16 *) pModbusSlave->pinputRegisters;

    memset(pModbusSlave->sdata,0,sizeof(pModbusSlave->sdata));

    pTempData.crc = ((pModbusSlave->rdata[lentemp-2])<<8)+pModbusSlave->rdata[lentemp-1];

    pModbusSlave->sdata[0] = pModbusSlave->rdata[0];

    if(pModbusSlave->rdata[0]==ModbusSlaveAddress)
    {//从地址检查
        pModbusSlave->sdata[1] = pModbusSlave->rdata[1];

        if(CRC16(pModbusSlave->rdata, lentemp-2) == pTempData.crc)
        {//校验CRC
            pTempData.contentIdx = (pModbusSlave->rdata[2]<<8)+pModbusSlave->rdata[3];
            pTempData.numofregisters = (pModbusSlave->rdata[4]<<8)+pModbusSlave->rdata[5];//这里不表示寄存器数目,表示线圈的状态

            if((pTempData.contentIdx+pTempData.numofregisters)>40)
            {         //数值超限判定
                pModbusSlave->sdata[1]|= 0x80;
                pModbusSlave->sdata[2]|= 0x03;
                i=CRC16(pModbusSlave->sdata, 3);
                pModbusSlave->sdata[3]= (i>>8)&0xff;
                pModbusSlave->sdata[4]= i&0xff;
                pModbusSlave->sendlen=5;
                ScicRegs.SCIFFTX.bit.TXFFIL = pModbusSlave->sendlen;
                ScicRegs.SCIFFTX.bit.TXFFIENA = 1;
            }
            else
            {
                switch(pModbusSlave->rdata[1])
                {
                    case MB_FUNC_READ_COIL:
                        pModbusSlave->sdata[2] = (pTempData.numofregisters+7)/8;   //向上取整
                        for(i=0;i<pTempData.numofregisters;i++)
                        {
                            pModbusSlave->sdata[3+(i/8)] |= (*(pcoils_sub+i+pTempData.contentIdx) << (i%8));
                        }
                        i=CRC16(pModbusSlave->sdata, pModbusSlave->sdata[2]+3);
                        pModbusSlave->sdata[2+pModbusSlave->sdata[2]+1]=(i>>8)&0xff;
                        pModbusSlave->sdata[2+pModbusSlave->sdata[2]+2]=i&0xff;
                        pModbusSlave->sendlen= pModbusSlave->sendlen + 5 + pModbusSlave->sdata[2];
                        if(pModbusSlave->sendlen > bufferdepth)
                        {ScicRegs.SCIFFTX.bit.TXFFIL = bufferdepth;}
                        else
                        {ScicRegs.SCIFFTX.bit.TXFFIL = pModbusSlave->sendlen;}
                        ScicRegs.SCIFFTX.bit.TXFFIENA = 1;
                        break;
                    case MB_FUNC_READ_INPUT:
                        pModbusSlave->sdata[2] = (pTempData.numofregisters+7)/8;   //向上取整
                        for(i=0;i<pTempData.numofregisters;i++)
                        {
                            pModbusSlave->sdata[3+(i/8)]|=*(pinputs_sub+i+pTempData.contentIdx) << (i%8);
                        }
                        i=CRC16(pModbusSlave->sdata, pModbusSlave->sdata[2]+3);
                        pModbusSlave->sdata[2+pModbusSlave->sdata[2]+1]=(i>>8)&0xff;
                        pModbusSlave->sdata[2+pModbusSlave->sdata[2]+2]=i&0xff;
                        pModbusSlave->sendlen= pModbusSlave->sendlen + 5 + pModbusSlave->sdata[2];
                        if(pModbusSlave->sendlen > bufferdepth)
                        {ScicRegs.SCIFFTX.bit.TXFFIL = bufferdepth;}
                        else
                        {ScicRegs.SCIFFTX.bit.TXFFIL = pModbusSlave->sendlen;}
                        ScicRegs.SCIFFTX.bit.TXFFIENA = 1;
                        break;
                    case MB_FUNC_READ_HOLDINGREGISTERS:
                        pModbusSlave->sdata[2] = pTempData.numofregisters*2;   //向上取整
                        for(i=0;i<(pTempData.numofregisters);i++)
                        {
                            pModbusSlave->sdata[3+i*2+1]=*(pholdingRegisters_sub+i+pTempData.contentIdx) ;
                            pModbusSlave->sdata[3+i*2+1] = (pModbusSlave->sdata[3+i*2+1]) & 0xff;
                            pModbusSlave->sdata[3+i*2]=*(pholdingRegisters_sub+i+pTempData.contentIdx) ;
                            pModbusSlave->sdata[3+i*2]=(pModbusSlave->sdata[3+i*2])>>8;
                        }
                        i=CRC16(pModbusSlave->sdata, pModbusSlave->sdata[2]+3);
                        pModbusSlave->sdata[2+pModbusSlave->sdata[2]+1]=(i>>8)&0xff;
                        pModbusSlave->sdata[2+pModbusSlave->sdata[2]+2]=i&0xff;
                        pModbusSlave->sendlen= pModbusSlave->sendlen + 5 + pModbusSlave->sdata[2];
                        if(pModbusSlave->sendlen > bufferdepth)
                        {ScicRegs.SCIFFTX.bit.TXFFIL = bufferdepth;}
                        else
                        {ScicRegs.SCIFFTX.bit.TXFFIL = pModbusSlave->sendlen;}
                        ScicRegs.SCIFFTX.bit.TXFFIENA = 1;
                        break;
                    case MB_FUNC_READ_INPUTREGISTERS:
                        pModbusSlave->sdata[2] = pTempData.numofregisters*2;   //向上取整
                        for(i=0;i<(pTempData.numofregisters);i++)
                        {
                            pModbusSlave->sdata[3+i*2+1]=*(pinputRegisters_sub+i+pTempData.contentIdx) ;
                            pModbusSlave->sdata[3+i*2+1] = (pModbusSlave->sdata[3+i*2+1]) & 0xff;
                            pModbusSlave->sdata[3+i*2]=*(pinputRegisters_sub+i+pTempData.contentIdx) ;
                            pModbusSlave->sdata[3+i*2]=(pModbusSlave->sdata[3+i*2])>>8;
                        }
                        i=CRC16(pModbusSlave->sdata, pModbusSlave->sdata[2]+3);
                        pModbusSlave->sdata[2+pModbusSlave->sdata[2]+1]=(i>>8)&0xff;
                        pModbusSlave->sdata[2+pModbusSlave->sdata[2]+2]=i&0xff;
                        pModbusSlave->sendlen= pModbusSlave->sendlen + 5 + pModbusSlave->sdata[2];
                        if(pModbusSlave->sendlen > bufferdepth)
                        {ScicRegs.SCIFFTX.bit.TXFFIL = bufferdepth;}
                        else
                        {ScicRegs.SCIFFTX.bit.TXFFIL = pModbusSlave->sendlen;}
                        ScicRegs.SCIFFTX.bit.TXFFIENA = 1;
                        break;
                    case MB_FUNC_FORCE_COIL:
                        for(i=2;i<8;i++)
                        {
                            pModbusSlave->sdata[i] = pModbusSlave->rdata[i];
                        }
                        if(pTempData.numofregisters==0xFF00)
                        {
                            *(pcoils_sub+pTempData.contentIdx)=1;
                            pModbusSlave->sendlen=8;
                        }
                        else
                        {
                            if(pTempData.numofregisters==0x0000)
                            {
                                *(pcoils_sub+pTempData.contentIdx)=0;
                                pModbusSlave->sendlen=8;
                            }
                            else//命令错误
                            {
                                pModbusSlave->sdata[1]= 0x85;
                                pModbusSlave->sdata[2]= 0x03;
                                i=CRC16(pModbusSlave->sdata, 3);
                                pModbusSlave->sdata[3]= (i>>8)&0xff;
                                pModbusSlave->sdata[4]= i&0xff;
                                pModbusSlave->sendlen=5;
                            }
                        }

                        ScicRegs.SCIFFTX.bit.TXFFIL = pModbusSlave->sendlen;
                        ScicRegs.SCIFFTX.bit.TXFFIENA = 1;
                        break;
                    case MB_FUNC_WRITE_HOLDINGREGISTER:
                        for(i=2;i<8;i++)
                        {
                            pModbusSlave->sdata[i] = pModbusSlave->rdata[i];
                        }

                        *(pholdingRegisters_sub+pTempData.contentIdx)=pTempData.numofregisters;
                        pModbusSlave->sendlen=8;

                        ScicRegs.SCIFFTX.bit.TXFFIL = pModbusSlave->sendlen;
                        ScicRegs.SCIFFTX.bit.TXFFIENA = 1;
                        break;
                    case MB_FUNC_FORCE_NCOILS:
                        for(i=0;i<pTempData.numofregisters;i++)
                        {
                            *(pcoils_sub+i+pTempData.contentIdx) = ((pModbusSlave->rdata[i/8+7])>>(i%8))&0x01;
                        }
                        for(i=2;i<7;i++)
                        {
                            pModbusSlave->sdata[i] = pModbusSlave->rdata[i];
                        }

                        i=CRC16(pModbusSlave->sdata, 7);
                        pModbusSlave->sdata[7]=(i>>8)&0xff;
                        pModbusSlave->sdata[8]=i&0xff;
                        pModbusSlave->sendlen=9;
                        ScicRegs.SCIFFTX.bit.TXFFIL = pModbusSlave->sendlen;
                        ScicRegs.SCIFFTX.bit.TXFFIENA = 1;
                        break;
                    case MB_FUNC_WRITE_NREGISTERS:
                        for(i=0;i<pTempData.numofregisters;i++)
                        {
                            *(pholdingRegisters_sub+i+pTempData.contentIdx) =(Uint16)(pModbusSlave->rdata[i*2+7]<<8) + (pModbusSlave->rdata[i*2+8]&0xFF);
                        }
                        for(i=2;i<6;i++)
                        {
                            pModbusSlave->sdata[i] = pModbusSlave->rdata[i];
                        }

                        i=CRC16(pModbusSlave->sdata, 6);
                        pModbusSlave->sdata[6]=(i>>8)&0xff;
                        pModbusSlave->sdata[7]=i&0xff;
                        pModbusSlave->sendlen=8;
                        ScicRegs.SCIFFTX.bit.TXFFIL = pModbusSlave->sendlen;
                        ScicRegs.SCIFFTX.bit.TXFFIENA = 1;
                        break;
                    default:
                        pModbusSlave->sdata[2]= 0x03;
                        i=CRC16(pModbusSlave->sdata, 3);
                        pModbusSlave->sdata[3]= (i>>8)&0xff;
                        pModbusSlave->sdata[4]= i&0xff;
                        pModbusSlave->sendlen=5;
                        ScicRegs.SCIFFTX.bit.TXFFIL = pModbusSlave->sendlen;
                        ScicRegs.SCIFFTX.bit.TXFFIENA = 1;
                        break;
                }
            }
        }
        else//校验码错误
        {
            pModbusSlave->sdata[1]|= 0x80;
            pModbusSlave->sdata[2]|= 0x03;
            i=CRC16(pModbusSlave->sdata, 3);
            pModbusSlave->sdata[3]= (i>>8)&0xff;
            pModbusSlave->sdata[4]= i&0xff;
            pModbusSlave->sendlen=5;
            ScicRegs.SCIFFTX.bit.TXFFIL = pModbusSlave->sendlen;
            ScicRegs.SCIFFTX.bit.TXFFIENA = 1;
        }
    }
    else//地址错误不处理
    {     }
}

//void Scic_xmit(int a)
//{
//    while(ScicRegs.SCICTL2.bit.TXRDY!=1){}
//    ScicRegs.SCITXBUF = a;
//}


Uint16 CRC16 (Uint16 *nData, Uint16 wLength)
{
    static const Uint16 wCRCTable[] = {
       0X0000, 0XC0C1, 0XC181, 0X0140, 0XC301, 0X03C0, 0X0280, 0XC241,
       0XC601, 0X06C0, 0X0780, 0XC741, 0X0500, 0XC5C1, 0XC481, 0X0440,
       0XCC01, 0X0CC0, 0X0D80, 0XCD41, 0X0F00, 0XCFC1, 0XCE81, 0X0E40,
       0X0A00, 0XCAC1, 0XCB81, 0X0B40, 0XC901, 0X09C0, 0X0880, 0XC841,
       0XD801, 0X18C0, 0X1980, 0XD941, 0X1B00, 0XDBC1, 0XDA81, 0X1A40,
       0X1E00, 0XDEC1, 0XDF81, 0X1F40, 0XDD01, 0X1DC0, 0X1C80, 0XDC41,
       0X1400, 0XD4C1, 0XD581, 0X1540, 0XD701, 0X17C0, 0X1680, 0XD641,
       0XD201, 0X12C0, 0X1380, 0XD341, 0X1100, 0XD1C1, 0XD081, 0X1040,
       0XF001, 0X30C0, 0X3180, 0XF141, 0X3300, 0XF3C1, 0XF281, 0X3240,
       0X3600, 0XF6C1, 0XF781, 0X3740, 0XF501, 0X35C0, 0X3480, 0XF441,
       0X3C00, 0XFCC1, 0XFD81, 0X3D40, 0XFF01, 0X3FC0, 0X3E80, 0XFE41,
       0XFA01, 0X3AC0, 0X3B80, 0XFB41, 0X3900, 0XF9C1, 0XF881, 0X3840,
       0X2800, 0XE8C1, 0XE981, 0X2940, 0XEB01, 0X2BC0, 0X2A80, 0XEA41,
       0XEE01, 0X2EC0, 0X2F80, 0XEF41, 0X2D00, 0XEDC1, 0XEC81, 0X2C40,
       0XE401, 0X24C0, 0X2580, 0XE541, 0X2700, 0XE7C1, 0XE681, 0X2640,
       0X2200, 0XE2C1, 0XE381, 0X2340, 0XE101, 0X21C0, 0X2080, 0XE041,
       0XA001, 0X60C0, 0X6180, 0XA141, 0X6300, 0XA3C1, 0XA281, 0X6240,
       0X6600, 0XA6C1, 0XA781, 0X6740, 0XA501, 0X65C0, 0X6480, 0XA441,
       0X6C00, 0XACC1, 0XAD81, 0X6D40, 0XAF01, 0X6FC0, 0X6E80, 0XAE41,
       0XAA01, 0X6AC0, 0X6B80, 0XAB41, 0X6900, 0XA9C1, 0XA881, 0X6840,
       0X7800, 0XB8C1, 0XB981, 0X7940, 0XBB01, 0X7BC0, 0X7A80, 0XBA41,
       0XBE01, 0X7EC0, 0X7F80, 0XBF41, 0X7D00, 0XBDC1, 0XBC81, 0X7C40,
       0XB401, 0X74C0, 0X7580, 0XB541, 0X7700, 0XB7C1, 0XB681, 0X7640,
       0X7200, 0XB2C1, 0XB381, 0X7340, 0XB101, 0X71C0, 0X7080, 0XB041,
       0X5000, 0X90C1, 0X9181, 0X5140, 0X9301, 0X53C0, 0X5280, 0X9241,
       0X9601, 0X56C0, 0X5780, 0X9741, 0X5500, 0X95C1, 0X9481, 0X5440,
       0X9C01, 0X5CC0, 0X5D80, 0X9D41, 0X5F00, 0X9FC1, 0X9E81, 0X5E40,
       0X5A00, 0X9AC1, 0X9B81, 0X5B40, 0X9901, 0X59C0, 0X5880, 0X9841,
       0X8801, 0X48C0, 0X4980, 0X8941, 0X4B00, 0X8BC1, 0X8A81, 0X4A40,
       0X4E00, 0X8EC1, 0X8F81, 0X4F40, 0X8D01, 0X4DC0, 0X4C80, 0X8C41,
       0X4400, 0X84C1, 0X8581, 0X4540, 0X8701, 0X47C0, 0X4680, 0X8641,
       0X8201, 0X42C0, 0X4380, 0X8341, 0X4100, 0X81C1, 0X8081, 0X4040 };

    Uint16 nTemp;
    Uint16 wCRCWord = 0xFFFF;

    while (wLength--)
    {
        nTemp = (*nData++ ^ wCRCWord)&0xff;
        wCRCWord >>= 8;
        wCRCWord  ^= wCRCTable[nTemp];
    }
    wCRCWord = (Uint16) ((wCRCWord<<8)+(wCRCWord>>8));
    return wCRCWord;
} // End: CRC16

你可能感兴趣的:(DSP28335 高速modbus代码实现)