This article will cover setting the general purpose outputs through LLRP in C#. First, let's take a look at the simplest example: turning a single GPO on or off. In LLRP, GPOs are controlled through the SET_READER_CONFIG message. The function below creates a SET_READER_CONFIG message, sets the GPO state within that message and then sends the message to the reader.
static void Set_GPO_State(ushort port, bool state) { MSG_ERROR_MESSAGE msg_err; MSG_SET_READER_CONFIG msg = new MSG_SET_READER_CONFIG(); // Build the message msg.GPOWriteData = new PARAM_GPOWriteData[1]; msg.GPOWriteData[0] = new PARAM_GPOWriteData(); msg.GPOWriteData[0].GPOPortNumber = port; msg.GPOWriteData[0].GPOData = state; // Send the message MSG_SET_READER_CONFIG_RESPONSE rsp = reader.SET_READER_CONFIG(msg, out msg_err, 2000); if (rsp != null) { // Success Console.WriteLine(rsp.ToString()); } else if (msg_err != null) { // Error Console.WriteLine(msg_err.ToString()); } else { // Timeout Console.WriteLine("Timeout Error."); } }
Here's an example of how to call the function we just created.
static void Main(string[] args) { const ushort GPO_PORT = 1; const bool GPO_STATE = true; // Create a LLRPClient instance. reader = new LLRPClient(); /* Connect to the reader. Replace "SpeedwayR-10-25-32" with your reader's hostname. The second argument (2000) is a timeout value in milliseconds. If a connection cannot be established within this timeframe, the call will fail. */ ENUM_ConnectionAttemptStatusType status; reader.Open("SpeedwayR-10-25-32", 2000, out status); // Check for a connection error if (status != ENUM_ConnectionAttemptStatusType.Success) { // Could not connect to the reader. // Print out the error Console.WriteLine(status.ToString()); // Do something here. // Your application should not continue. return; } // Set the GPO state Set_GPO_State(GPO_PORT, GPO_STATE); }
Now, let's take the example one step further. Suppose you want to use all four GPO ports to represent a binary word. Some peripheral devices can be controlled this way. A multiplexer, for example, might allow you to select the active antenna by setting a group of inputs to a binary value. Setting the multiplexer control pins to 0011 selects antenna #3, 1001 selects antenna #9, etc. Here's a C# function that takes a number as an input and sets the GPOs to the binary value of that number.
static void Set_GPO_Word(int word) { const int NUM_GPO_PORTS = 4; int i, bitVal; bool[] bits = new bool[NUM_GPO_PORTS]; // Loop through the last four bits in the // word and store their values in an array for (i = 0; i < NUM_GPO_PORTS; i++) { bitVal = word & 1; if (bitVal == 1) bits[i] = true; else bits[i] = false; word = word >> 1; } MSG_ERROR_MESSAGE msg_err; MSG_SET_READER_CONFIG msg = new MSG_SET_READER_CONFIG(); // Build the message msg.GPOWriteData = new PARAM_GPOWriteData[NUM_GPO_PORTS]; for (i = 0; i < NUM_GPO_PORTS; i++) { msg.GPOWriteData[i] = new PARAM_GPOWriteData(); msg.GPOWriteData[i].GPOPortNumber = (ushort)(i + 1); msg.GPOWriteData[i].GPOData = bits[i]; } // Send the message MSG_SET_READER_CONFIG_RESPONSE rsp = reader.SET_READER_CONFIG(msg, out msg_err, 2000); if (rsp != null) { // Success Console.WriteLine(rsp.ToString()); } else if (msg_err != null) { // Error Console.WriteLine(msg_err.ToString()); } else { // Timeout Console.WriteLine("Timeout Error."); } }
Comments
Article is closed for comments.