Skip to Main Content
Cognex 支持中心
LogoLogo

How to Get Additional Serial Data to be Included with Output Results on a Dataman Reader

Instructions on getting user defined and transmitted serial data to be included with decode results on a DataMan reader

2024/09/15

Details

Question

Can a DataMan receive data from over RS232 and include that data with the read results going back to a PLC? For example - A DataMan is connected to a scale which sends data over RS232 - reads weight - weight goes to the DataMan and gets included with the read results being sent to the PLC.

Answer

There are several methods to accomplish this. The following is a simple method that can be altered to suit your needs. The method involves using two scripts - one Data Formatting script, and the other a Communications script.

BACKGROUND INFORMATION - serial devices are often configured to send, along with its data, a header and footer. These serial devices (not all) often use these specific ASCII control characters for start of text - STX (ASCII code 02) and end of text - ETX (ASCII code 03). This is the serial device's way of saying - my data is starting here and ending here. If that were the case we write the line below:

this.expectedFramed("\x02", "\x03",128);

Review the serial device's documentation to determine what header and footer is being used - some allow configuring the device with a unique header and footer.

A scale measuring weight (for example) reading 15.253oz sends the following:

\x0215.253oz\x03

The communications script will get the data from the device in the format above and store the data between the header and footer as a variable - Swag.

Now the camera is ready to take it's read results and include the Swag. We have included a space between the serial number and weight for clarity:

output.content = decodeResults[0].content + " " + Swag;

 

The following could be sent to the PLC:

8675309 15.253oz

In regards to the communications script - the two key points are the onConnect "this.expectedFramed("x", "z", n);" where x is the header, z is the footer, and n is the max number of characters. The other key point is the onExpectedData function. Here it will store a string that contains the data between the header and footer. In the following example I assigned it the variable name "Swag". This variable can then be referenced in the Data Formatting script:

function CommHandler() { 

    return { 

        onConnect: function(peerName) { 

            this.expectFramed("\x02", "\x03", 128); 

            return true; 

        }, 

        onDisconnect: function() {}, 

        onError: function(errorMsg) {}, 

        onExpectedData: function(inputString) { 

            Swag = inputString; 

            return true; 

        }, 

        onUnexpectedData: function(inputString) { 

            return true; 

        }, 

        onTimer: function() {}, 

        onEncoder: function() {} 

    }; 



  

Use the following for the Data Formatting script: 

function onResult(decodeResults, readerProperties, output) { 

    if (decodeResults[0].decoded) { 

        output.content = decodeResults[0].content + " " + Swag; //If no data was received only content will be sent 

        Swag = "" //This will clear results for next trigger 

    } 

Loading component...

Loading component...

相关资源

Loading component...