STC89C58RD+写的modbus rtu master主站的程序,使用modbus slave调试,尚存在问题
我用STC89C58RD+写的modbus rtu master主站的程序,使用modbus slave调试,尚存在问题,希望和大家一起讨论,将其完善1 这个程序只是用于测试03功能
2 能够正常的发送主站的命令数组,调试工具modbus slave也能够正常的回应,
3 我在接收和发送程序中都加了标记的指示灯,指示灯的显示结果表明程序能够进入发送和接收到函数,但stc单片机端收不到slave回应的数据
//==============main.c==============================
//==================================================
#include <STC89.H>//单片机51头文件,存放着单片机的寄存器
#include "mod.h"
unsigned char dat; //用于存储单片机接收发送缓冲寄存器SBUF里面的内容
#define uchar unsigned char
#define uint unsigned int
#define PLC_address 0x01// PLC地址定义
//******功能码定义******//
#define R_Q 0x01
#define R_I 0x02
#define R_VB 0x03
#define R_AIW 0x04
#define W_Q_SIGNAL 0x05
#define W_VB_SIGNAL 0x06
#define W_Q_MORE 0x15
#define W_VB_MORE 0x16
uchar *add;
unsignedshort count0_1ms;
unsignedshort count_1ms;
unsignedshort count0_2ms;
void send_shuzu(uchar shuzu);
u8 r_vb={PLC_address,R_VB,0x00,0x00,0x00,0x01};//read VB
extern u16 crc16(u8 *puchMsg, u16 usDataLen);
extern code u8 auchCRCLo[];
extern code u8 auchCRCLo[];
void rvb();
void jieshou();
u16 crcData;
u8 sendok=0,jieshouok=0;
extern void display_smg(uchar smg,uchar value);
void Delay(unsigned int tc) //延时程序
{
while( tc != 0 )
{unsigned int i;
for(i=0; i<100; i++);
tc--;}
}
void Init_Com(void)//功能:串口初始化,波特率9600,方式1
{
TMOD |= 0x20;
PCON = 0x00;
SCON = 0x50;
TH1 = 0xFd;
TL1 = 0xFd;
TR1 = 1;
ES = 1;
}
//------------------------------------------------
// 定时器初始化子程序
void Init_Timer0(void)
{
TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
TH0=0xff; //给定初值,这里使用定时器最大值从0开始计数一直到65535溢出
TL0=0xa3;
EA=1; //总中断打开
ET0=1; //定时器中断打开
TR0=1; //定时器开关打开
}
//------------------------------------------------//
void main()//主程序功能:实现接收数据并把接收到的数据原样发送回去///////
{
uchar m,r_data;
uchar *p_data;
// uchar code r_vb={PLC_address,R_VB,0x00,0x00,0x00,0x01};//read VB
Init_Timer0();
Init_Com();//串口初始化
while(1)
{
receTimeOutFlag =2;
rvb();
send_shuzu(r_vb);
jieshou();
Delay(100);
m=receBuf&0x0f;
display_smg(0,m);
}
}
//--------------------------------------------------------
void send_char(uchar ch)//发送
{
SBUF=ch;
while(!TI);
TI=0;
}
void send_shuzu(u8 shuzu)
{
uchar i,j,jj;
Delay(20);
for(i=0;i<8;i++)
{
SBUF=shuzu;
while(!TI);
TI=0;
P10=~P10;
}
Delay(20);
}
void rvb()
{
crcData = crc16(r_vb,6);
r_vb = crcData >> 8;
r_vb = crcData & 0xff;
}
void jieshou()
{
unsigned char Temp; //定义临时变量
// if(count0_2ms>39)
{ P14=~P14;
count0_2ms=0;
if(RI) //判断是接收中断产生
{ P15=~P15;
RI=0;
receTimeOutCount = 0;
receTimeOutFlag = 0;
// receBuf = USART_ReceiveData(USART1); // 读数清除接收中断标志位,否则会一直进入中断;
receBuf = SBUF; // 读数清除接收中断标志位,否则会一直进入中断;
if(receCount<8)
receCount++; //接收地址偏移寄存器加1
else
REN=0;
}
}
}
//==============crc16.c==============================
//==================================================
#include<STC89.h>
#include "mod.h"
/* CRC 高位字节值表 */
code u8 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,
0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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
} ;
u16 crc16(u8 *puchMsg, u16 usDataLen)
{
u8 uchCRCHi = 0xFF ; /* 高CRC字节初始化 */
u8 uchCRCLo = 0xFF ; /* 低CRC 字节初始化 */
u32 uIndex ; /* CRC循环中的索引 */
while (usDataLen--) /* 传输消息缓冲区 */
{
uIndex = uchCRCHi ^ *puchMsg++ ; /* 计算CRC */
uchCRCHi = uchCRCLo ^ auchCRCHi ;
uchCRCLo = auchCRCLo ;
}
return (uchCRCHi << 8 | uchCRCLo) ;
}//u16 crc16(u8 *puchMsg, u16 usDataLen)
//==================xianshi.c=====================================
//=============================================================
#include<stc89.h>
#define uint unsigned int
#define uchar unsigned char
sbit smgdl=P2^6;
sbit smgwl=P2^7;
void delayxx(uint a)
{
uint b;
uchar c;
for(b=a;a>0;a--)
for(c=100;c>0;c--);
}
void display_smg(uchar smg,uchar value)
{
uchar code tablewe[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
uchar code tabledu[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
smgwl=1;
P0=tablewe;
smgwl=0;
smgdl=1;
P0=tabledu;
smgdl=0;
delayxx(50);//延长显示时间 避开视觉暂留 看数码管显示过程
//delay(10);
}
//==============mod.h====================================================
//===================================================================================
#ifndef __mod_h__
#define __mod_h__
typedefunsigned char u8;
typedefunsigned short u16;
typedefunsigned long u32;
void ModbusRTU_Init(void); // ModbusRTU的相关变量初值
void beginSend(void); // 启动串口发送
void checkComm0Modbus(void);
void readCoil(void);
void readRegisters(void);
void forceSingleCoil(void);
void presetSingleRegister(void);
void presetMultipleRegisters(void);
void forceMultipleCoils(void);
u16 getRegisterVal(u16 addr,u16 *tempData);
u16 setRegisterVal(u16 addr,u16 tempData);
u16 getCoilVal(u16 addr,u16 *tempData);
u16 setCoilVal(u16 addr,u16 tempData);
#endif 回复【楼主位】dongnan2007
-----------------------------------------------------------------------
http://cache.amobbs.com/bbs_upload782111/files_38/ourdev_628042XA69HO.GIF
(原文件名:1.GIF) 问题已经解决, 好东西额...
我也再做主机的... MARK mark
页:
[1]