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

📄 mbus_test.c

📁 OpenMODBUS/TCP to RS-232/485 MODBUS RTU gateway librar Zhejiang Univ. Hangzhou, China Dec.2006
💻 C
字号:
/* * mbus_test.c - test example file for mbus library * * Copyright (c) 2003, Victor Antonovich (avmlink@vlink.ru) *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *  * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. *  * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. *  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $Id: mbus_test.c,v 1.2 2003/09/09 18:47:20 avm Exp $ */#include "mbus.h"void usage(void);#define SLAVE_ADDR 1#define MBUSD_ADDR "127.0.0.1"#define MBUSD_PORT 502#ifndef PATH_MAX#define PATH_MAX 1023#endif#define DATA_MAX 255voidusage(void){  printf("Test utility for libmbus library.\n"     "Copyright (c) Victor Antonovich, 2002-2003 // 2:5055/124.13@fidonet\n"	 "usage: mbus_test [<options>] \n"	 "-h             this help\n"	 "-s             set MODBUSD device slave address (default %d)\n"	 "-A<number>     set mbusd address to connect (default %s)\n"	 "-P<number>     set mbusd port number (default %d)\n"	 "\n", SLAVE_ADDR, MBUSD_ADDR, MBUSD_PORT);  exit(0);}intmain(int argc, char *argv[]){  int mbusd_port = MBUSD_PORT, rc, data_ptr = 1;  char mbusd_addr[PATH_MAX + 1];  mbus_struct *mbusd;  mbus_ubyte slave_addr = SLAVE_ADDR;  mbus_ubyte slave_data[DATA_MAX], data_ctr;    strncpy(mbusd_addr, MBUSD_ADDR, PATH_MAX);    /* command line parsing */  while ((rc = getopt(argc, argv, "hA:P:s:")) != -1)  {    switch (rc)    {      case 'A':        strncpy(mbusd_addr, optarg, PATH_MAX);        break;      case 'P':        mbusd_port = strtoul(optarg, NULL, 0);      case 's':        slave_addr =          (mbus_ubyte)(strtoul(optarg, NULL, 0) & 0xff);        break;      case 'h':        usage();    }  }    printf("\nLibrary information: %s\n",         mbus_version(NULL));    if ((mbusd = mbus_init(NULL)) == NULL)  {    printf("Can't init mbus structure (%s), exiting",           strerror(errno));    exit(0);  }    printf("Connecting to mbusd server (%s, %u)...\n",         mbusd_addr, mbusd_port);  if (mbus_connect(mbusd,                    mbusd_addr, mbusd_port, 0))  {    printf("Can't connect (%s), exiting...\n",           strerror(errno));    exit(0);  }  else    printf("Connected successfully.\n");    printf("Sending ReportSlaveID command to "         "slave device with address %d...\n",         slave_addr);  if ((rc = mbus_cmd_report_slave_id(mbusd, slave_addr,        &data_ctr, slave_data)) < 0)  {    printf("Command error (%s), exiting...\n",           strerror(errno));    goto ret;  }    if (rc == 1)  {    printf("Exception code %d returned.\n",           mbus_excode(mbusd));        goto ret;  }    printf("Device responding, Run Indicator status: %s (%d)\n",         slave_data[0] ? "ON" : "OFF", slave_data[0]);  printf("Dump of the response data rest (total %d bytes): ",         --data_ctr);  while (data_ctr--)  {    printf("0x%02X%s", slave_data[data_ptr++],           data_ctr ? ", " : "\n");  }  ret:  printf("Closing connection to mbusd server...\n");    if (mbus_close(mbusd))  {    printf("Connection closing error (%s), exiting.\n",           strerror(errno));  }  else    printf("Connection closed successfully, exiting.\n");    return 0;}

⌨️ 快捷键说明

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