Skip to Main Content
Cognex 지원 허브
Logo

Comms Script to Trigger Read and Read X-number of Codes by Sending a Single Number

In this solution a communication script connects the the RS232 interface of the DataMan reader and will listen for '1' or '2' in RS232 input. Depending on the input value the DataMan will trigger and read x number of Data Matrix Codes.

2026. 02. 07.

Details

Solution: controlling the number of read codes via RS-232 input

Problem description

Some applications require external control over how many Data Matrix codes a DataMan reader should read per trigger event. For example, a PLC or host system may need to dynamically switch between reading one code or multiple codes based on process state.

Solution overview

This solution uses a communication script to monitor the RS-232 (COM1) interface of the DataMan reader.
Based on simple ASCII commands received over RS-232 ("1" or "2"), the script adjusts the Multi-Code configuration and triggers a read accordingly.

Solution script

function CommHandler()
{
   return {
       onConnect: function (peerName)
       {
           my_name = peerName;
           if (my_name != "COM1")
               return false;

           this.send(my_name + ": connected\r\n");
           return true;
       },

 onDisconnect: function ()
       {
       },

       onError: function (errorMsg)
       {
       },

       onExpectedData: function (inputString)
       {
           if (inputString == "1") {
               dmccSet("MULTICODE.NUM-CODES", 1);
               dmccCommand("TRIGGER", true);
           }

 else if (inputString == "2") {
               dmccSet("MULTICODE.NUM-CODES", 2);
               dmccSet("MULTICODE.MAX-NUM-CODES", 2);
               dmccCommand("TRIGGER", true);
           }
           return true;
       },

       onUnexpectedData: function (inputString)
       {
           return true;
       },

       onTimer: function ()
       {
       },

       onEncoder: function ()

   {
       }
   };
}

How it works

  • The script listens for incoming data on COM1 (RS-232).
  • When a command is received:
    • "1" → Configure the reader to read one Data Matrix code
    • "2" → Configure the reader to read two Data Matrix codes
  • After configuration, a software trigger is issued.
  • The Multi-Code settings are updated dynamically before each trigger.

Configuration notes

  • Ensure RS-232 communication parameters (baud rate, parity, stop bits) match the host system.
  • The input commands are case-sensitive and must exactly match "1" or "2".
  • This approach can be extended to support additional commands (e.g., "3", "4") if required.

Summary

  • RS-232 input can be used to dynamically control Multi-Code behavior.
  • Simple ASCII commands allow flexible runtime configuration.
  • This method is well suited for PLC- or host-controlled reading scenarios.

관련 자원