⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modbus ascii 开发3.txt

📁 用C编写的MODBUS协议程序,通讯方式为ASCII模式.
💻 TXT
字号:
Modbus ASCII 开发3-基于Scilab得SCADA system开发手记8 
推荐void construct_ascii_frm ( unsigned char *dst_buf, unsigned char *src_buf, unsigned char lenth)
{

     unsigned char lrc_tmp;

     lrc_tmp = lrc( src_buf, lenth);
     *(src_buf+lenth) = lrc_tmp;
     lenth++;
     *dst_buf = ':';
     rtutoascii( dst_buf,src_buf, lenth);
     *(dst_buf + 2 * lenth+1) = 0x0d;
     *(dst_buf + 2 * lenth+2) = 0x0a;
}
 /*

1 读取继电器状态

发送:
*/
 int ascii_read_coil_status ( unsigned char board_adr, unsigned char *com_buf,int start_address,int lenth) 
 {
    unsigned char tmp[256],tmp_lenth;
    tmp[0] = board_adr;
    tmp[1] = READ_COIL;
    tmp[2] = HI(start_address);
    tmp[3] = LOW(start_address);
    tmp[4] = HI(lenth);
    tmp[5] = LOW(lenth);
    tmp_lenth = 6;
    construct_ascii_frm (com_buf, tmp, tmp_lenth);
    return 18;
}
/*
2 读取开关量输入
发送:
*/
int ascii_read_input_status ( unsigned char board_adr,unsigned char *com_buf,int start_address,int lenth) 
{
    unsigned char tmp[256], tmp_lenth;

    tmp[0] = board_adr;
    tmp[1] = READ_DI;
    tmp[2] = HI(start_address);
    tmp[3] = LOW(start_address);
    tmp[4] = HI(lenth);
    tmp[5] = LOW(lenth);
    tmp_lenth = 6;
    construct_ascii_frm ( com_buf, tmp, tmp_lenth);
    return 18;
}
/*
3 读取保持寄存器
发送:
*/

int ascii_read_hldreg ( unsigned char board_adr,unsigned char *com_buf,int start_address,int lenth) 
{
    unsigned char tmp[256], tmp_lenth;
    tmp[0] = board_adr;
    tmp[1] = READ_HLD_REG;
    tmp[2] = HI(start_address);
    tmp[3] = LOW(start_address);
    tmp[4] = HI(lenth);
    tmp[5] = LOW(lenth);
    tmp_lenth = 6;
    construct_ascii_frm ( com_buf, tmp, tmp_lenth);
    return 18;
}

/* 4 发送读取模拟量输入*/
int ascii_read_anloginput( unsigned char board_adr,unsigned char *com_buf,int start_address,int lenth) 
{
    unsigned char tmp[256], tmp_lenth;
    tmp[0] = board_adr;
    tmp[1] = READ_AI;
    tmp[2] = HI(start_address);
    tmp[3] = LOW(start_address);
    tmp[4] = HI(lenth);
    tmp[5] = LOW(lenth);
    tmp_lenth = 6;
    construct_ascii_frm ( com_buf, tmp, tmp_lenth);
    return 18;
}
/*
5 设置继电器
发送:status =0 继电器释放 否则继电器吸合,address 为吸合的继电器编号,0为第一个继电器,依次类推
*/
 int ascii_set_coil ( unsigned char board_adr,unsigned char *com_buf,int start_address,int status )
{
    unsigned char tmp[256], tmp_lenth;
    tmp[0] = board_adr;
    tmp[1] = SET_COIL ;  
    tmp[2] = HI(start_address);
    tmp[3] = LOW(start_address);
     if ( status )
    {
       tmp[4] = 0xff;
       tmp[5] = 0;
    }
    else
    {
       tmp[4]= 0;
       tmp[5]= 0;
    } 
    tmp_lenth = 6;
    construct_ascii_frm ( com_buf, tmp, tmp_lenth);
    return 18;
}
/*
6 设置保持寄存器
*/

int ascii_set_hldreg( unsigned char board_adr,unsigned char *com_buf,int start_address,unsigned int value )
{
    unsigned char tmp[256], tmp_lenth;
    tmp[0] = board_adr;
    tmp[1] = SET_HLD_REG;
    tmp[2] = HI(start_address);
    tmp[3] = LOW(start_address);
    tmp[4] = HI(value);
    tmp[5] = LOW(value);    
    tmp_lenth = 6;
    construct_ascii_frm ( com_buf, tmp, tmp_lenth);
    return 18;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -