STM32 KEIL编写 Modbus RTU Master slave程序源码
单片机源程序如下:
- #include "stm32f10x.h"
- #include "modbus.h"
- #include "Init_Config.h"
- // Mudbus 功能码
- #define ReadCoilSta 0x01 // 读位COIL,对应PLC的DO状态
- #define ReadHoldReg 0x03 // 读字,对应PLC的AO状态
- #define ForceSingleCoil 0x05 // 写位,只写一位COIL,对应PLC的DO输出
- #define PresetSingleReg 0x06 // 写字,对单一寄存器赋值,对应PLC的AO输出
- #define SlaveID 0x01 // 从机ID号
- unsigned int crc16(unsigned char* puchMsg,unsigned char usDataLen);
- unsigned char n=17,c[17]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
- // 变量声明
- // 保存接收与发送消息的数组
- unsigned char aRxBuff[8]; // 接收缓冲区
- unsigned char aTxBuff[17]; // 发送缓冲区
- volatile unsigned char nTxBuff = 0; // 发送计数
- unsigned char CommIndex=0; // 数据位置索引,从0开始,到CommIndexEnd结束
- unsigned char CommIndexEnd=7; // 接收长度
- unsigned char RecFinishF=0; // 是否接收完成标志
- unsigned long RecTimer=0; // 通信间隔时间
- unsigned char FreOneH=0x00; //变频器一高位
- unsigned char FreOneL=0x00;
- unsigned char FreTwoH=0x00; //变频器二高位
- unsigned char FreTwoL=0x00;
- unsigned char Run=0x00; //启动
- unsigned char Stop=0x00; //停止
- unsigned char FrePOne=0x00; //点动变频器一
- unsigned char FrePTwo=0x00; //点动变频器二
- unsigned char FrePROne=0x00; //点加变频器一
- unsigned char FrePDOne=0x00; //减
- unsigned char FrePRTwo=0x00; //点加变频器二
- unsigned char FrePDTwo=0x00; //减
- unsigned char FreR=0x00; //变频器统调加
- unsigned char FreD=0x00; //减
- /* Table Of CRC Values for high-order unsigned char */
- unsigned char 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
- };
- /* Table of CRC values for low-order unsigned char */
- unsigned char 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
- };
- /*###############################################NO
- purpose: 计算CRC16的值
- function name: crc16
- return data: unsigned int
- parameters: par1:
- name: puchMsg
- type: unsigned char*
- meaning: 将要计算CRC16值的
- 所有数据
- par2:
- name: usDataLen
- type: unsigned char
- meaning: 将要计算CRC16值的
- 数据个数
- finished time: 2007/5/16
- amended time:
- NO###############################################*/
- unsigned int crc16(unsigned char* puchMsg,unsigned char usDataLen)
- {
- unsigned char uchCRCHi = 0xFF ;
- unsigned char uchCRCLo = 0xFF ;
- unsigned int uIndex ;
- while (usDataLen--)
- {
- uIndex = uchCRCHi ^ *puchMsg++;
- uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex] ;
- uchCRCLo = auchCRCLo[uIndex] ;
- }
- return (((unsigned int)(uchCRCHi) << 8) | uchCRCLo) ;
- }
- void InitVar_my(void)
- {
- RecFinishF=0;
- CommIndex=0;
- CommIndexEnd=7; // First enlarge recieve lenght
- RecTimer=0;
- }
- void ParseRecieve(void) //包解析
- {
- unsigned int uIndex ;
- unsigned int crc16tem;
- unsigned char i = 0;
- sendnum=1;
-
- if (aRxBuff[0] == SlaveID) // 从机ID是否正确
- {
- crc16tem=((unsigned int)(aRxBuff[CommIndexEnd-1]) << 8) | aRxBuff[CommIndexEnd]; // calculate CRC check, erase two CRC byte
- uIndex=crc16(aRxBuff,CommIndexEnd-1);
- if (crc16tem==uIndex)// crc16检校正确
- {
- switch (aRxBuff[1]) // 判断功能码
- {
- case ReadCoilSta: // 0x01
- aTxBuff[0]=SlaveID;
- aTxBuff[1]=ReadCoilSta;
- aTxBuff[2]=0x01; //字节数
- nTxBuff=4;
- switch (aRxBuff[3]) //判断起始地址
- {
- /* case 0x32: //画面0
- aTxBuff[3]=Run|Stop|PPback|PPfo|FrePOne|FrePTwo;
- break;
- case 0x3c: //画面1
- aTxBuff[3]=TemR|TemD|SignSwD|SignSwS;
- break;*/
- case 0x46: //变频器设定
- aTxBuff[3]=FrePROne|FrePDOne|FrePRTwo|FrePDTwo|FreR|FreD;
- break;
- }
- crc16tem=crc16(aTxBuff,nTxBuff);
- aTxBuff[nTxBuff++]=(unsigned char)(crc16tem>>8);
- aTxBuff[nTxBuff]=(unsigned char)(crc16tem & 0x00ff);
- /* for(i = 0; i < nTxBuff+1; i++)
- {
- c[i]=aTxBuff[i];
- } */
- // USART1_Putc(c,n);
- USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
- USART_SendData(USART1, aTxBuff[0]);
- // sendnum=1;
- break;
- case ReadHoldReg: // 0x03 读取寄存器值
- aTxBuff[0]=SlaveID;
- aTxBuff[1]=ReadHoldReg;
- aTxBuff[2]=aRxBuff[5]*2; //每个数据占16位 要读的寄存器数量*2 相应的第3个字节
- nTxBuff=3;
- /* if(aRxBuff[3]==0x0a)
- {
- aTxBuff[nTxBuff++]=IntendDisH;
- aTxBuff[nTxBuff++]=IntendDisL; //设定袋长
- aTxBuff[nTxBuff++]=IntendNumH;
- aTxBuff[nTxBuff++]=IntendNumL; //整本数设定
- aTxBuff[nTxBuff++]=FailNumH;
- aTxBuff[nTxBuff++]=FailNumL; //脱标次数
- aTxBuff[nTxBuff++]=AutoRunH;
- aTxBuff[nTxBuff++]=AutoRunL; //自启动时间
- aTxBuff[nTxBuff++]=IntendSignH;
- aTxBuff[nTxBuff++]=IntendSignL; //追标长度
- aTxBuff[nTxBuff++]=TemH;
- aTxBuff[nTxBuff++]=TemL; //切刀加温
- crc16tem=crc16(aTxBuff,nTxBuff);
- aTxBuff[nTxBuff++]=(unsigned char)(crc16tem>>8);
- aTxBuff[nTxBuff]=(unsigned char)(crc16tem & 0x00ff);
- for(i = 0; i < nTxBuff+1; i++)
- {
- c[i]=aTxBuff[i];
- }
- // USART1_Putc(c,n);
- USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
- USART_SendData(USART1, aTxBuff[0]);
- sendnum=1;
- }
- if(aRxBuff[3]==0x00)
- {
- aTxBuff[nTxBuff++]=RecNumH;
- aTxBuff[nTxBuff++]=RecNumL; //计数
- aTxBuff[nTxBuff++]=GroupH;
- aTxBuff[nTxBuff++]=GroupL; //批次
- aTxBuff[nTxBuff++]=IntendVelH;
- aTxBuff[nTxBuff++]=IntendVelL; //速度
- aTxBuff[nTxBuff++]=FreOneH;
- aTxBuff[nTxBuff++]=FreOneL; //变频器一速度
- aTxBuff[nTxBuff++]=TemH;
- aTxBuff[nTxBuff++]=TemL; //切刀加热
- aTxBuff[nTxBuff++]=FreTwoH;
- aTxBuff[nTxBuff++]=FreTwoL; //变频器二速度
-
- crc16tem=crc16(aTxBuff,nTxBuff);
- aTxBuff[nTxBuff++]=(unsigned char)(crc16tem>>8);
- aTxBuff[nTxBuff]=(unsigned char)(crc16tem & 0x00ff);
- for(i = 0; i < nTxBuff+1; i++)
- {
- c[i]=aTxBuff[i];
- }
- // USART1_Putc(c,n);
- USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
- USART_SendData(USART1, aTxBuff[0]);
- sendnum=1;
- }
- */
- if(aRxBuff[3]==0x01)
- {
- aTxBuff[nTxBuff++]=0x01;
- aTxBuff[nTxBuff++]=0x02; //温度高低
- aTxBuff[nTxBuff++]=0x03;
- aTxBuff[nTxBuff++]=0x04; //湿度高低
- crc16tem=crc16(aTxBuff,nTxBuff); //crc校验函数
- aTxBuff[nTxBuff++]=(unsigned char)(crc16tem>>8);
- aTxBuff[nTxBuff]=(unsigned char)(crc16tem & 0x00ff); //生成校验码 高位加低位
- /* for(i = 0; i < nTxBuff+1; i++) //将相应数据发送出去
- {
- c[i]=aTxBuff[i];
- } */
- for(i=0;i<9;i++)
- {
- USART_SendData(USART1,aTxBuff[i]);
- while (!(USART1->SR & USART_FLAG_TXE));
- }
- // USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
- /* USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
- USART_Cmd(USART1, ENABLE);
- USART_SendData(USART1,aTxBuff[0]); */
- // sendnum=1;
- }
- if(aRxBuff[3]==0x14)
- {
- aTxBuff[nTxBuff++]=FreOneH;
- aTxBuff[nTxBuff++]=FreOneL; //变频器一
- aTxBuff[nTxBuff++]=FreTwoH;
- aTxBuff[nTxBuff++]=FreTwoL; //变频器二
- crc16tem=crc16(aTxBuff,nTxBuff);
- aTxBuff[nTxBuff++]=(unsigned char)(crc16tem>>8);
- aTxBuff[nTxBuff]=(unsigned char)(crc16tem & 0x00ff);
- for(i = 0; i < nTxBuff+1; i++)
- {
- c[i]=aTxBuff[i];
- }
- // USART1_Putc(c,n);
- USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
- USART_SendData(USART1, aTxBuff[0]);
- // sendnum=1;
- }
- /* if(aRxBuff[3]==0x1e)
- {
- aTxBuff[nTxBuff++]=IntendVelH;
- aTxBuff[nTxBuff++]=IntendVelL; //送料速度
- aTxBuff[nTxBuff++]=PluseNOH;
- aTxBuff[nTxBuff++]=PluseNOL; //步进脉冲数
- aTxBuff[nTxBuff++]=LineH;
- aTxBuff[nTxBuff++]=LineL; //曲线选择
- aTxBuff[nTxBuff++]=CheckH;
- aTxBuff[nTxBuff++]=CheckL; //修轮
- crc16tem=crc16(aTxBuff,11);
- aTxBuff[nTxBuff++]=(unsigned char)(crc16tem>>8);
- aTxBuff[nTxBuff]=(unsigned char)(crc16tem & 0x00ff);
- for(i =0; i <nTxBuff+1; i++)
- {
- c[i]=aTxBuff[i];
- }
- // USART1_Putc(c,n);
- USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
- USART_SendData(USART1, aTxBuff[0]);
- sendnum=1;
- } */
- break;
- case ForceSingleCoil: // 0x05
- aTxBuff[0]=SlaveID;
- aTxBuff[1]=ForceSingleCoil;
- aTxBuff[2]=aRxBuff[2];
- aTxBuff[3]=aRxBuff[3];
- nTxBuff=6;
- switch (aRxBuff[3]) //判断地址码
- {
- /*case 0x32: //启动
- if(aRxBuff[4] == 0x00)
- {
- aTxBuff[4]=0x00;
- Run=0x00;
- }
- else if(aRxBuff[4]==0xFF)
- {
- aTxBuff[4]=0xFF;
- Run=0x01;
- Stop=0x00;
- }
- break;
- case 0x33: //停止
- if(aRxBuff[4] == 0x00)
- {
- aTxBuff[4]=0x00;
- Stop=0x00;
-
- }
- else if(aRxBuff[4]==0xFF)
- {
- aTxBuff[4]=0xFF;
- Stop=0x02;
- Run=0x00;
- }
- break;
- case 0x34: //点退
- if(aRxBuff[4] == 0x00)
- {
- aTxBuff[4]=0x00;
- PPback=0x00;
- }
- else if(aRxBuff[4]==0xFF)
- {
- aTxBuff[4]=0xFF;
- if(Stop==0x02)
- {
- PPback=0x04;
- //此处加退料
- }
- if(Run==0x01)
- {
- PPback=0x04;
- RecNumL-=0x01;
- }
- }
- break;
- case 0x35: //点进
- if(aRxBuff[4] == 0x00)
- {
- aTxBuff[4]=0x00;
- PPfo=0x00;
- }
- else if(aRxBuff[4]==0xFF&&Stop==0x02)
- {
- aTxBuff[4]=0xFF;
- PPfo=0x08;
- //此处加送料
- }
- break; */
- case 0x36: //点动变频器1
- if(aRxBuff[4] == 0x00)
- {
- aTxBuff[4]=0x00;
- FrePOne=0x00;
- }
- else if(aRxBuff[4]==0xFF)
- {
- aTxBuff[4]=0xFF;
- FrePOne=0x10;
- }
- break;
- case 0x37: //点动变频器2
- if(aRxBuff[4] == 0x00)
- {
- aTxBuff[4]=0x00;
- FrePTwo=0x00;
- }
- else if(aRxBuff[4]==0xFF)
- {
- aTxBuff[4]=0xFF;
- FrePTwo=0x20;
- }
- break;
- /* case 0x3c: //切刀加温
- if(aRxBuff[4] == 0x00)
- {
- aTxBuff[4]=0x00;
- TemR=0x00;
- }
- else if(aRxBuff[4]==0xFF)
- {
- aTxBuff[4]=0xFF;
- TemR=0x01;
- if(TemL<0xDC)
- TemL+=0x01;
- else TemL+=0x00;
- }
- break;
- case 0x3d: //切刀降温
- if(aRxBuff[4] == 0x00)
- {
- aTxBuff[4]=0x00;
- TemD=0x00;
- }
- else if(aRxBuff[4]==0xFF)
- {
- aTxBuff[4]=0xFF;
- TemD=0x02;
- if(TemL>0)
- TemL-=0x01;
- else TemL-=0;
- }
- break;
- case 0x3e: //追标深
- if(aRxBuff[4] == 0x00)
- {
- aTxBuff[4]=0x00;
- SignSwD=0x00;
- }
- else if(aRxBuff[4]==0xFF)
- {
- aTxBuff[4]=0xFF;
- SignSwD=0x04;
- SignSwS=0x00;
- }
- break;
- case 0x3f: //追标浅
- if(aRxBuff[4] == 0x00)
- {
- aTxBuff[4]=0x00;
- SignSwS=0x00;
- }
- else if(aRxBuff[4]==0xFF)
- {
- aTxBuff[4]=0xFF;
- SignSwS=0x08;
- SignSwD=0x00;
- }
- break;*/
- case 0x46: //变频器一加
- if(aRxBuff[4] == 0x00)
- {
- aTxBuff[4]=0x00;
- FrePROne=0x00;
- }
- else if(aRxBuff[4]==0xFF)
- {
- aTxBuff[4]=0xFF;
- FrePROne=0x01;
- FreOneL+=0x01;
- }
- break;
- case 0x47: //变频器一减
- if(aRxBuff[4] == 0x00)
- {
- aTxBuff[4]=0x00;
- FrePDOne=0x00;
- }
- else if(aRxBuff[4]==0xFF)
- {
- aTxBuff[4]=0xFF;
- FrePDOne=0x02;
- FreOneL-=0x01;
- }
- break;
- case 0x48: //变频器二加
- if(aRxBuff[4] == 0x00)
- {
- aTxBuff[4]=0x00;
- FrePRTwo=0x00;
- }
- else if(aRxBuff[4]==0xFF)
- {
- aTxBuff[4]=0xFF;
- FrePRTwo=0x04;
- FreTwoL+=0x01;
- }
- break;
- case 0x49: //变频器二减
- if(aRxBuff[4] == 0x00)
- {
- aTxBuff[4]=0x00;
- FrePDTwo=0x00;
- }
- else if(aRxBuff[4]==0xFF)
- {
- aTxBuff[4]=0xFF;
- FrePDTwo=0x08;
- FreTwoL-=0x01;
- }
- break;
- case 0x4A: //统调加
- if(aRxBuff[4] == 0x00)
- {
- aTxBuff[4]=0x00;
- FreR=0x00;
- }
- else if(aRxBuff[4]==0xFF)
- {
- aTxBuff[4]=0xFF;
- FreR=0x10;
- FreOneL+=0x01;
- FreTwoL+=0x10;
- }
- break;
- case 0x4b: //统调减
- if(aRxBuff[4] == 0x00)
- {
- aTxBuff[4]=0x00;
- FreD=0x00;
- }
- else if(aRxBuff[4]==0xFF)
- {
- aTxBuff[4]=0xFF;
- FreD=0x20;
- FreOneL-=0x01;
- FreTwoL-=0x10;
- }
- break;
- }
- aTxBuff[5]=0x00;
- crc16tem=crc16(aTxBuff,nTxBuff);
- aTxBuff[nTxBuff++]=(unsigned char)(crc16tem>>8);
- aTxBuff[nTxBuff]=(unsigned char)(crc16tem & 0x00ff);
- for(i = 0; i < nTxBuff+1; i++)
- {
- c[i]=aTxBuff[i];
- }
- // USART1_Putc(c,n);
- USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
- USART_SendData(USART1, aTxBuff[0]);
- sendnum=1;
- break;
-
- case PresetSingleReg: // 0x06
- for(i=0;i<6;i++)
- {aTxBuff[i]=aRxBuff[i];}
- nTxBuff=6;
- switch (aRxBuff[3]) //判断地址码
- {
- case 0x14: //变频器一速度
- FreOneH=aRxBuff[4];
- FreOneL=aRxBuff[5];
- break;
- case 0x15: //变频器二速度
- FreTwoH=aRxBuff[4];
- FreTwoL=aRxBuff[5];
- break;
- /* case 0x0f: //切刀加温
- TemH=aRxBuff[4];
- TemL=aRxBuff[5];
- break;
- case 0x0b: //整本数设定
- IntendNumH=aRxBuff[4];
- IntendNumL=aRxBuff[5];
- break;
- case 0x0a: //设定袋长
- IntendDisH=aRxBuff[4];
- IntendDisL=aRxBuff[5];
- break;
- case 0x1e: //速度
- IntendVelH=aRxBuff[4];
- IntendVelL=aRxBuff[5];
- break;
- case 0x0e: //追标长度
- IntendSignH=aRxBuff[4];
- IntendSignL=aRxBuff[5];
- break;
- case 0x0c: //脱标次数
- FailNumH=aRxBuff[4];
- FailNumL=aRxBuff[5];
- break;
- case 0x0d: //自动起机
- AutoRunH=aRxBuff[4];
- AutoRunL=aRxBuff[5];
- break;
- case 0x1f: //步进脉冲数
- PluseNOH=aRxBuff[4];
- PluseNOL=aRxBuff[5];
- break;
- case 0x20: //曲线选择
- LineH=aRxBuff[4];
- LineL=aRxBuff[5];
- break;
- case 0x21: //修轮
- CheckH=aRxBuff[4];
- CheckL=aRxBuff[5];
- break; */
- }
- crc16tem=crc16(aTxBuff,nTxBuff);
- aTxBuff[nTxBuff++]=(unsigned char)(crc16tem>>8);
- aTxBuff[nTxBuff]=(unsigned char)(crc16tem & 0x00ff);
- for(i = 0; i < nTxBuff+1; i++)
- {
- c[i]=aTxBuff[i];
- }
- // USART1_Putc(c,n);
- USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
- USART_SendData(USART1, aTxBuff[0]);
- // sendnum=1;
- break;
-
- }
- }
- }
- }
复制代码
- /******************** (C) COPYRIGHT 2012 WildFire Team **************************
- * 文件名 :main.c
- * 描述 :实现sht10传感器的modbus与上位机的通讯。
- * 实验平台:奋斗mini开发板
- * 库版本 :ST3.5.0
- **********************************************************************************/
- #include "stm32f10x.h"
- #include "Init_Config.h"
- #include "modbus.h"
- #include "myiic.h"
- #include "MeasuerTEMP.h"
- #include "Systick.h"
- /*
- * 函数名:main
- * 描述 : 主函数
- * 输入 :无
- * 输出 : 无
- */
- int main(void)
- {
- RCC_Configuration();
- Init_Config();
-
- SHT10_Config();
-
- SysTick_Init();
- while (1)
- {
- if(Uart0_rev_comflag==0x01)
- {
- Uart0_rev_comflag=0x00;
-
- ParseRecieve();
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
- }
- }
- }
- /******************* (C) COPYRIGHT 2012 WildFire Team *****END OF FILE************/
复制代码
所有资料51hei提供下载:
STM32_MODBUS1.7z
(186.31 KB, 下载次数: 139)
|