在用新唐的NANO130 M0实现了modbus的03,和06 功能码,附代码
/**************************************************************************//**
* @file modbus.h
* @version V1.00
* $Revision: 00$
* $Date: 17/8/26
* @brief modbusÐÒéµÄÍ·Îļþ
*
* @note
* Copyright (C) ck.huang.
*****************************************************************************/
#ifndef __modbus_H__
#define __modbus_H__
typedef struct
{
uint8_t myadd; //±¾É豸µÄµØÖ·
uint8_t rcbuf[100]; //modbus½ÓÊÕµ½Êý¾Ý»º³åÇø
uint16_t timout; //Êý¾Ý³¬Ê±¼ÆÊýÆ÷
uint8_t recount; //modbus¶Ë¿ÚÒѾÊÕµ½µÄÊý¾Ý¸öÊý
uint8_t timrun; //¿ªÊ¼¼ÆʱµÄ±êÖ¾
uint8_t reflag; //ÊÕµ½Ò»Ö¡Êý¾ÝµÄ±êÖ¾
uint8_t sendbuf[100]; //·¢Ë͵ÄÊý¾Ý»º³åÇø
}MODBUS;
extern MODBUS modbus;
void modbusInit(void);
void modbusEvent(void);
#endif
C实现:
/**************************************************************************//**
* @file modbus.c
* @version V1.00
* $Revision: 00$
* $Date: 17/8/16
* @brief modbusµÄ³ÌÐòÎļþ
*
* @note
* Copyright (C) ck.huang.
*****************************************************************************/
#include "Nano100Series.h"
#include "modbus.h"
#include "modbusTimer.h"
#include "modbusUart.h"
#include "ModbusCRC.h"
MODBUS modbus; //ÉêÃ÷Ò»¸ömodbusÊý¾ÝÀàÐ͵ÄÊý¾Ý
extern uint16_t reg[];
void modbusInit(void)
{
modbus.myadd=4; //±¾´Ó»úµÄµØÖ·
modbus.timrun=0; //¼Æʱ±êÖ¾ÇåÁã
Timer0_Init();
RS485_Init();
UART_Write(UART0,"modbus run!", 11);
}
void modbusFun03(void) //03¹¦ÄÜÂë´¦Àí--Ö÷»úÒª¶ÁÈ¡±¾´Ó»úµÄ¼ÄÆ÷
{
uint16_t regAdd; //Òª¶ÁµÄ¼Ä´æÆ÷¿ªÊ¼µÄµØÖ·
uint16_t regLen; //¼Ä´æÆ÷¸öÊý
uint16_t i,j;
uint16_t byte;
uint16_t crc;
regAdd=modbus.rcbuf[2] *256+modbus.rcbuf[3];//µÃµ½¼Ä´æÆ÷µÄÊ×µØÖ·
regLen=modbus.rcbuf[4]*256+modbus.rcbuf[5]; //µÃµ½Òª¶ÁÈ¡µÄÊýÁ¿
i=0;
modbus.sendbuf[i++]=modbus.myadd; //±¾»úµØÖ·
modbus.sendbuf[i++]=0x03; //¹¦ÄÜÂë
byte = regLen*2;
modbus.sendbuf[i++]=byte%256;
for(j=0;j<regLen; j++)
{
modbus.sendbuf[i++]=reg[regAdd+j]/256;
modbus.sendbuf[i++]=reg[regAdd+j]%256;
}
crc=crc16(modbus.sendbuf,i);
modbus.sendbuf[i++]=crc/256;
modbus.sendbuf[i++]=crc%256;
UART_Write(UART0,modbus.sendbuf,i);
}
void modbusFun06(void) //06¹¦ÄÜÂë´¦Àí
{
uint16_t regAdd;
uint16_t val,byte;
uint16_t i;
uint16_t crc;
regAdd=modbus.rcbuf[2]*256+modbus.rcbuf[3]; //µÃµ½ÒªÐ޸ĵĵØÖ·
val=modbus.rcbuf[4]*256+modbus.rcbuf[5]; //Ð޸ĺóµÄÖµ
reg[regAdd]=val;
i=0;
modbus.sendbuf[i++]=modbus.myadd;
modbus.sendbuf[i++]=0x06;
modbus.sendbuf[i++]=regAdd/256; //·µ»ØÁ½¸ö×Ö½Ú
modbus.sendbuf[i++]=regAdd%256;
modbus.sendbuf[i++]=val/256;
modbus.sendbuf[i++]=val%256;
crc=crc16(modbus.sendbuf,i);
modbus.sendbuf[i++]=crc/256;
modbus.sendbuf[i++]=crc%256;
UART_Write(UART0,modbus.sendbuf,i);
}
void modbusEvent(void)
{
uint16_t crc,rcCrc;
if(0== modbus.reflag) return; //δÊÕµ½modbusµÄÊý¾Ý°ü
crc = crc16(modbus.rcbuf,modbus.recount-2); //¼ÆËãУÑéÂë
rcCrc =modbus.rcbuf[modbus.recount-2]*256+modbus.rcbuf[modbus.recount-1]; //ÊÕµ½µÄУÑéÂë
if(crc==rcCrc)
{
if(modbus.rcbuf[0]==modbus.myadd) //È·ÈÏÊý¾ÝÊDz»ÊǸøµ½±¾É豸µÄµØÖ·
{
switch (modbus.rcbuf[1]) //·ÖÎö¹¦ÄÜÂë
{
case 03: modbusFun03(); break;
case 06: modbusFun06(); break;
}
}else if(0==modbus.rcbuf[0]) //¹ã²¥µØÖ·
{
}
}
modbus.recount=0;
modbus.reflag = 0; //Çå±êÖ¾
}
在用新唐的NANO130 M0实现了modbus的03,和06 功能码,附代码
/**************************************************************************//**
* @file modbus.h
* @version V1.00
* $Revision: 00$
* $Date: 17/8/26
* @brief modbusÐÒéµÄÍ·Îļþ
*
* @note
* Copyright (C) ck.huang.
*****************************************************************************/
#ifndef __modbus_H__
#define __modbus_H__
typedef struct
{
uint8_t myadd; //±¾É豸µÄµØÖ·
uint8_t rcbuf[100]; //modbus½ÓÊÕµ½Êý¾Ý»º³åÇø
uint16_t timout; //Êý¾Ý³¬Ê±¼ÆÊýÆ÷
uint8_t recount; //modbus¶Ë¿ÚÒѾÊÕµ½µÄÊý¾Ý¸öÊý
uint8_t timrun; //¿ªÊ¼¼ÆʱµÄ±êÖ¾
uint8_t reflag; //ÊÕµ½Ò»Ö¡Êý¾ÝµÄ±êÖ¾
uint8_t sendbuf[100]; //·¢Ë͵ÄÊý¾Ý»º³åÇø
}MODBUS;
extern MODBUS modbus;
void modbusInit(void);
void modbusEvent(void);
#endif
C实现:
/**************************************************************************//**
* @file modbus.c
* @version V1.00
* $Revision: 00$
* $Date: 17/8/16
* @brief modbusµÄ³ÌÐòÎļþ
*
* @note
* Copyright (C) ck.huang.
*****************************************************************************/
#include "Nano100Series.h"
#include "modbus.h"
#include "modbusTimer.h"
#include "modbusUart.h"
#include "ModbusCRC.h"
MODBUS modbus; //ÉêÃ÷Ò»¸ömodbusÊý¾ÝÀàÐ͵ÄÊý¾Ý
extern uint16_t reg[];
void modbusInit(void)
{
modbus.myadd=4; //±¾´Ó»úµÄµØÖ·
modbus.timrun=0; //¼Æʱ±êÖ¾ÇåÁã
Timer0_Init();
RS485_Init();
UART_Write(UART0,"modbus run!", 11);
}
void modbusFun03(void) //03¹¦ÄÜÂë´¦Àí--Ö÷»úÒª¶ÁÈ¡±¾´Ó»úµÄ¼ÄÆ÷
{
uint16_t regAdd; //Òª¶ÁµÄ¼Ä´æÆ÷¿ªÊ¼µÄµØÖ·
uint16_t regLen; //¼Ä´æÆ÷¸öÊý
uint16_t i,j;
uint16_t byte;
uint16_t crc;
regAdd=modbus.rcbuf[2] *256+modbus.rcbuf[3];//µÃµ½¼Ä´æÆ÷µÄÊ×µØÖ·
regLen=modbus.rcbuf[4]*256+modbus.rcbuf[5]; //µÃµ½Òª¶ÁÈ¡µÄÊýÁ¿
i=0;
modbus.sendbuf[i++]=modbus.myadd; //±¾»úµØÖ·
modbus.sendbuf[i++]=0x03; //¹¦ÄÜÂë
byte = regLen*2;
modbus.sendbuf[i++]=byte%256;
for(j=0;j<regLen; j++)
{
modbus.sendbuf[i++]=reg[regAdd+j]/256;
modbus.sendbuf[i++]=reg[regAdd+j]%256;
}
crc=crc16(modbus.sendbuf,i);
modbus.sendbuf[i++]=crc/256;
modbus.sendbuf[i++]=crc%256;
UART_Write(UART0,modbus.sendbuf,i);
}
void modbusFun06(void) //06¹¦ÄÜÂë´¦Àí
{
uint16_t regAdd;
uint16_t val,byte;
uint16_t i;
uint16_t crc;
regAdd=modbus.rcbuf[2]*256+modbus.rcbuf[3]; //µÃµ½ÒªÐ޸ĵĵØÖ·
val=modbus.rcbuf[4]*256+modbus.rcbuf[5]; //Ð޸ĺóµÄÖµ
reg[regAdd]=val;
i=0;
modbus.sendbuf[i++]=modbus.myadd;
modbus.sendbuf[i++]=0x06;
modbus.sendbuf[i++]=regAdd/256; //·µ»ØÁ½¸ö×Ö½Ú
modbus.sendbuf[i++]=regAdd%256;
modbus.sendbuf[i++]=val/256;
modbus.sendbuf[i++]=val%256;
crc=crc16(modbus.sendbuf,i);
modbus.sendbuf[i++]=crc/256;
modbus.sendbuf[i++]=crc%256;
UART_Write(UART0,modbus.sendbuf,i);
}
void modbusEvent(void)
{
uint16_t crc,rcCrc;
if(0== modbus.reflag) return; //δÊÕµ½modbusµÄÊý¾Ý°ü
crc = crc16(modbus.rcbuf,modbus.recount-2); //¼ÆËãУÑéÂë
rcCrc =modbus.rcbuf[modbus.recount-2]*256+modbus.rcbuf[modbus.recount-1]; //ÊÕµ½µÄУÑéÂë
if(crc==rcCrc)
{
if(modbus.rcbuf[0]==modbus.myadd) //È·ÈÏÊý¾ÝÊDz»ÊǸøµ½±¾É豸µÄµØÖ·
{
switch (modbus.rcbuf[1]) //·ÖÎö¹¦ÄÜÂë
{
case 03: modbusFun03(); break;
case 06: modbusFun06(); break;
}
}else if(0==modbus.rcbuf[0]) //¹ã²¥µØÖ·
{
}
}
modbus.recount=0;
modbus.reflag = 0; //Çå±êÖ¾
}
调试的图片
工程的完整keil代码下载地址: http://download.csdn.net/download/shuishou47/9961177
希望能给你实现应用有帮助。
|