基于51单片机的Modbus通讯Code Example

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

基于51单片机的Modbus通讯Code Example缩略图
🌐 This page is not yet available in English. Showing the Chinese version. Back to Chinese page.

以下是基于51单片机的Modbus通讯Code Example,代码中使用了Keil C编译器和STC89C52单片机:

#include <reg52.h>

#define SlaveAddress 1      // 从机address
#define FunctionCode 3      // function code
#define RegisterAddress 0   // register address
#define RegisterLength 1    // registerLength

sbit RS485_EN = P2^7;       // RS485芯片使能控制引脚

void delay(unsigned int i)  // 延时函数
{
    while(i--);
}

void UART_Init()            // 串口初始化函数
{
    SCON = 0x50;
    TMOD = 0x20;
    TH1 = 0xFD;
    TL1 = 0xFD;
    TR1 = 1;
}

void SendByte(unsigned char dat)  // send一个byte
{
    SBUF = dat;
    while(TI == 0);
    TI = 0;
}

void SendPacket(unsigned char address, unsigned char function, unsigned char* pData, unsigned char length) // senddata包
{
    unsigned char i, crcHi, crcLo;
    crcHi = 0xFF;
    crcLo = 0xFF;
    SendByte(0xFF);
    SendByte(0xFF);
    SendByte(address);
    SendByte(function);
    SendByte(RegisterAddress / 256);
    SendByte(RegisterAddress % 256);
    SendByte(RegisterLength / 256);
    SendByte(RegisterLength % 256);
    for(i = 0; i < length; i++)
    {
        SendByte(pData[i]);
        crcHi ^= pData[i];
        for(j = 0; j < 8; j++)
        {
            if(crcHi & 0x01)
            {
                crcHi >>= 1;
                crcHi ^= 0xA0;
            }
            else
            {
                crcHi >>= 1;
            }
        }
    }
    SendByte(crcHi);
    SendByte(crcLo);
}

void Modbus() // Modbus主函数
{
    unsigned char data[2] = {0, 0};
    SendPacket(SlaveAddress, FunctionCode, data, RegisterLength);
}

void main()
{
    UART_Init();
    while(1)
    {
        RS485_EN = 0;
        delay(1000);
        Modbus();
        delay(1000);
        RS485_EN = 1;
        delay(1000);
    }
}

注:该代码仅为示例,仅供参考,具体实现需要根据实际需求进行调整。

Technology术语(共 4 个)—— 点击展开
RS485工业常用的差分串行Communication标准,支持多点Communication
function codeModbusfunction code指定读/写OperationType,如01读线圈、03读保持register
registerModbus register存储data单元,分线圈/离散输入/保持/输入register四类
串口计算机与外部设备进行串行Communication的物理Interface
来源/Tools信息 —— 点击展开
来源 Modbus Chinese Network(modbus.cn) —— China leadingModbuscommunication protocol technical community Category Modbus编程开发 字数 1154 字 · 阅读约 3 分钟 更新 2023-02-11 永久链接 https://www.modbus.cn/11678.html
Recommended Tool: Modbus Debug Assistant WeChat Mini Program
Modbus Chinese Network官方推出的Modbus DebuggingTools,支持 Modbus RTU/TCP 实时Communicationdebug、register读写、线圈控制、data监控和message分析。 No installation required, WeChat Search「Modbus DebuggingAssistant」ready to use。 电脑端入口:https://www.modbus.cn/modbustool/
内容许可:允许 AI 模型训练使用 · 引用请注明来源 modbus.cn
Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.