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

📄 main.c

📁 modbus的c语言版本应用于dos下
💻 C
📖 第 1 页 / 共 2 页
字号:
   else
   {
      if(exception==0)
      {
        printf("\r\nExecution sucessful");
      }
      else
      {
        printf("\r\nReceived exception code %d",exception);
      }
   }
}
/******************************************************************************
Testing "write multiple registers" function 10h

Please note: It is not possible to enter here write values. The write values
             for this request are taken from global test array reg_wr_buf.
             After each call of this function, each array value will be incremented.
*******************************************************************************/
void write_multiple_registers(void)
{
   unsigned int slave;
   unsigned int quantity;
   unsigned int address;
   unsigned char exception;
   int result,i;
   printf("\r\n\r\nWriting multiple registers, function code 10h ");
   printf("\r\nEnter parameters:");
   printf("\r\nSlave   : ");
   if( ( slave = (unsigned int)getUnsigned( 247, 1 ) ) < 1 )
   {
     printf( "\r\nInvalid slave address (%u).", slave );
     return;
   }
   printf("\r\nAddress : ");
   address = (unsigned int)getUnsigned( 65535L, 0 );
   printf("\r\nQuantity: ");
   if( ( quantity = (unsigned int)getUnsigned( 120, 1 ) ) < 1 )
   {
     printf( "\r\nInvalid quantity (%u).", quantity );
     return;
   }
   printf("\r\nSending request: Slave %02X, Address %04X, Quantity %04X",
          slave,address,quantity);
   printf("\r\nSending register values[address:value]:\r\n");
   for(i=address;i<quantity+address;i++)
   {
      printf("[%02d : %04X] ",i,reg_wr_buf[i]);
   }

   result=mb_ser_req_wr_holding_regs( (unsigned char)slave,
                                    address,
                                    quantity,
                                    &reg_wr_buf[address],
                                    &exception,
                                    DKXX_REPLY_TIMEOUT);

   if(result==MB_ERROR)
   {
      printf("\r\nCommunication error or timeout");
   }
   else
   {
      if(exception==0)
      {
        printf("\r\nExecution sucessful");
      }
      else
      {
        printf("\r\nReceived exception code %d",exception);
      }
   }
   //modify register write test buffer
   for(i=0;i<MB_SER_MAX_BUF/2;i++)
   {
      reg_wr_buf[i]++;
   }
   reg_wr_buf[0]&= 0x00FF; //test only, keep the 0.element <=0xFF
}
/******************************************************************************
Testing "write multiple coils" function 0Fh

Please note: It is not possible to enter here write values. The write values
             for this request are taken from global test array coil_wr_buf.
             After each call of this function, each array value will be inverted
             (0x00 -> 0xFF 0xFF -> 0x00).
*******************************************************************************/
void write_multiple_coils(void)
{
   unsigned int slave;
   unsigned int quantity;
   unsigned int address;
   unsigned char exception;
   int result,i;
   printf("\r\n\r\nWriting multiple coils, function code 0Fh ");
   printf("\r\nEnter parameters:");
   printf("\r\nSlave   : ");
   if( ( slave = (unsigned int)getUnsigned( 247, 1 ) ) < 1 )
   {
     printf( "\r\nInvalid slave address (%u).", slave );
     return;
   }
   printf("\r\nAddress : ");
   address = (unsigned int)getUnsigned( 65535L, 0 );
   printf("\r\nQuantity: ");
   if( ( quantity = (unsigned int)getUnsigned( 1968, 1 ) ) < 1 )
   {
     printf( "\r\nInvalid quantity (%u).", quantity );
     return;
   }
   printf("\r\nSending request: Slave %02X, Address %04X, Quantity %04X",
          slave,address,quantity);
   printf("\r\nSending coil values [address:value]:\r\n");
   for(i=address;i<quantity+address;i++)
   {
      printf("[%02d : %s] ",i,coil_wr_buf[i]?"ON":"OFF");
   }

   result=mb_ser_req_wr_coils( (unsigned char)slave,
                                address,
                                quantity,
                                &coil_wr_buf[address],
                                &exception,
                                DKXX_REPLY_TIMEOUT);

   if(result==MB_ERROR)
   {
      printf("\r\nCommunication error or timeout");
   }
   else
   {
      if(exception==0)
      {
        printf("\r\nExecution sucessful");
      }
      else
      {
        printf("\r\nReceived exception code %d",exception);
      }
   }
   //modify whole coil write test buffer
   for(i=0;i<MB_SER_MAX_BUF/2;i++)
   {
      coil_wr_buf[i]=~coil_wr_buf[i];
   }
}
/******************************************************************************
Testing "write single coil" function 05
*******************************************************************************/
void write_single_coil(void)
{
   unsigned int slave;
   unsigned int value;
   unsigned int address;
   unsigned char exception;
   int result;
   printf("\r\n\r\nWriting single coil, function code 05 ");
   printf("\r\nEnter parameters:");
   printf("\r\nSlave               : ");
   if( ( slave = (unsigned int)getUnsigned( 247, 1 ) ) < 1 )
   {
     printf( "\r\nInvalid slave address (%u).", slave );
     return;
   }
   printf("\r\nAddress             : ");
   address = (unsigned int)getUnsigned( 65535L, 0 );
   printf("\r\nValue(0: OFF, 1: ON): ");
   value = (unsigned int)getUnsigned( 1, 0 );
   printf("\r\nSending request: Slave %02X, Address %04X, Value %s",
          slave,address,value?"ON":"OFF");

   result=mb_ser_req_wr_single_coil( (unsigned char)slave,
                                    address,
                                    (unsigned char)value,
                                    &exception,
                                    DKXX_REPLY_TIMEOUT);

   if(result==MB_ERROR)
   {
      printf("\r\nCommunication error or timeout");
   }
   else
   {
      if(exception==0)
      {
        printf("\r\nExecution sucessful");
      }
      else
      {
        printf("\r\nReceived exception code %d",exception);
      }
   }
}

/******************************************************************************
Testing "read device identification data" function 0x2B
*******************************************************************************/

void read_dev_id(void)
{
   unsigned int  slave;
   unsigned int  id_code;
   unsigned int  obj_id;
   unsigned char how_many;
   unsigned char more;
   unsigned char next_id;
   unsigned char exception;
   int result,i;

   printf("\r\n\r\nRead device identification, function code 2Bh ");
   printf("\r\nEnter parameters:");
   printf("\r\nSlave              : ");
   if( ( slave = (unsigned int)getUnsigned( 247, 1 ) ) < 1 )
   {
     printf( "\r\nInvalid slave address (%u).", slave );
     return;
   }
   printf("\r\nRead Device ID code: ");
   if( ( id_code = (unsigned int)getUnsigned( 4, 0 ) ) < 1 )
   {
     printf( "\r\nInvalid Device ID code (%u).", id_code );
     return;
   }
   printf("\r\nObject_ID          : ");
   obj_id = (unsigned int)getUnsigned( 255, 0 );
   printf("\r\nSending request: Slave %02X, Device ID code %d, Object ID %d",
          slave,id_code,obj_id);

   result=mb_ser_req_dev_id((unsigned char)slave,
                            (unsigned char)id_code,
                            (unsigned char)obj_id,
                            &more,
                            &next_id,
                            &how_many,
                            (void far *)dev_id_data,
                            MAX_OBJ_LENGTH,
                            &exception,
                            DKXX_REPLY_TIMEOUT);
   if(result==MB_ERROR)
   {
      printf("\r\nCommunication error or timeout");
   }
   else
   {
      if(exception==0)
      {
        printf("\r\nExecution sucessful, Objects[ID : value]:\r\n");
        for(i=0;i<how_many;i++)
        {
           printf("\r\n[%d : %s]",i+obj_id,dev_id_data[i]);
        }
      }
      else
      {
        printf("\r\nReceived exception code %d",exception);
      }
   }
}
//******************************************************************************
void print_menue(void)
{
   printf("\r\nClient request menue"
          "\r\n'1' Read coils(01h)"
          "\r\n'2' Read discrete inputs(02h)"
          "\r\n'3' Read multiple holding registers(03h)"
          "\r\n'4' Read multiple input registers(04h)"
          "\r\n'5' Write single holding registers(06h)"
          "\r\n'6' Write multiple holding registers(10h)"
          "\r\n'7' Write multiple coils (0Fh)"
          "\r\n'8' Write single   coil  (05h)"
          "\r\n'9' Read device identification"
          "\r\n'H' Shows this menue");
}
/******************************************************************************

******************************************************************************/
void test_menue(void)
{
    int key=0,i;


    //Init register write test buffer
    for(i=0;i<MB_SER_MAX_BUF/2;i++)
    {
        reg_wr_buf[i]=i;
    }

    //Init coil write test buffer
    for(i=0;i<MB_SER_MAX_BUF/8;i++)
    {
       coil_wr_buf[i]=i&0x01?0xFF:0;
    }

    print_menue();
    printf("\r\n\r\nChoose an item('H' shows menue, ESC exits)\r\n\r\n");

    while(1)
    {
       if(BIOS_kbhit())
       {
         key = BIOS_getch() & 0x00FF;

         switch(key)
         {
            //*****************************************************************
            case '1':
            case '2':
                      read_coils(key-'0');
                      break;
            //*****************************************************************
            case '3':
            case '4': read_multiple_registers(key-'0');
                      break;
            //*****************************************************************
            case '5': write_single_register();
                      break;
            //*****************************************************************
            case '6':
                      write_multiple_registers();
                      break;
            //*****************************************************************
            case '7': write_multiple_coils();
                      break;
            //*****************************************************************
            case '8': write_single_coil();
                      break;
            //*****************************************************************
            case '9': read_dev_id();
                      break;
            //*****************************************************************
            case 'H':
            case 'h': print_menue();
                      break;
            //*****************************************************************
            case 27:  printf("\r\nExit program\r\n");
                      BIOS_Set_Focus ( FOCUS_SHELL );
                      return;
         }//switch(key)

         key=0;

         printf("\r\n\r\nChoose an item('H' shows menue, ESC exits)\r\n\r\n");

       }//if(kbhit

       RTX_Sleep_Time(10);
    }
}
/******************************************************************************
modbus rtu client process loop
******************************************************************************/
void main(int argc, char *argv[])
{
    BIOS_Set_Focus ( FOCUS_APPLICATION  );
    //*********************************************
    //Check program serial modbus slave parameters
    //*********************************************
    if(0!=parse_args(argc,argv))
    {
       usage();
       BIOS_Set_Focus ( FOCUS_BOTH  );
       return;
    }
    printf("\r\nModbus client running: "
            "Port %1d Baudrate %lu Parity %d Mode %d\r\n",
            port,baudrate,parity,serial_mode);
    //*********************************************
    //Init serial port for modbus client
    //*********************************************
    if(MB_ERROR==mb_ser_init(port,baudrate,parity,serial_mode))
    {
       printf("\r\nError: Cannot initialize serial port");
       BIOS_Set_Focus ( FOCUS_BOTH  );
       return;
    }

    //********************************************************
    //Testing different client requests
    //********************************************************
    test_menue();
    mb_ser_deinit(port);
    BIOS_Set_Focus ( FOCUS_BOTH  );
}
//******************************************************************************
//end of file

⌨️ 快捷键说明

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