Overview
This technical note demonstrates a request/response exchange using a Crimson Raw Serial Port. The transmit program clears the receive buffer and sends a command. The receive program waits for a carriage-return-terminated response, writes non-empty data to a tag, and calls the transmit program again.
Applicable Products and Versions
- Crimson 3.2-based products
- The exact hardware model and firmware version were not provided.
Prerequisites
- Configure and enable a Raw Serial Port.
- Configure the serial port to match the connected device.
- Connect a device that accepts the transmitted command and returns a response terminated by a carriage return.
- Create a String tag named
Basic.Response. - Create a transmit program named
Basic.Tx(). - Create a receive program named
AdvancedNotAbbr.Rx().
Serial Port Configuration
The supplied configuration image shows these settings:
| Setting | Value |
|---|---|
| Driver | Raw Serial Port |
| Mode | Master |
| On Update | AdvancedNotAbbr.Rx() |
| Baud Rate | 9600 |
| Data Bits | Seven |
| Stop Bits | One |
| Parity | Odd |
| Port Mode | 2-Wire RS485 |
Transmit Program
// declare local variable int port = 2; // clear the port's buffer ClearRx(port); // print N01TA$ out of Port 2 PortPrint(port, "N01TA$");
Receive Program
// declare local variables
cstring input;
int port = 2;
// read Port 2's buffer: no start character, <CR> end character,
// 500 ms timeout, no length restriction
input = PortInput(port, 0, 13, 500, 0);
// check for non-empty input
if(input != "") {
// populate tag with data
Basic.Response = input;
}
// request more data
Basic.Tx();Code Operation
Transmit Program
- Assigns Port 2 to the local
portvariable. - Clears pending receive data from Port 2 with
ClearRx(). - Sends the ASCII string
N01TA$from Port 2 withPortPrint().
Receive Program
- Declares the
inputstring and assigns Port 2 to the localportvariable. - Calls
PortInput(port, 0, 13, 500, 0)to read the receive buffer. - Uses no start character and ASCII 13 (
<CR>) as the end character. - Uses a 500 ms timeout and no length restriction.
- Writes a non-empty response to
Basic.Response. - Calls
Basic.Tx()to request more data.
Applying the Code in Crimson
- Configure the RS-485 communications port with the settings shown above.
- Add the transmit code to
Basic.Tx(). - Add the receive code to
AdvancedNotAbbr.Rx(). - Create the String tag
Basic.Response. - Set the Raw Serial Port On Update property to
AdvancedNotAbbr.Rx(). - Initiate the first transmission by calling
Basic.Tx()from an appropriate Crimson event or action. The initiating event was not provided. - Download the database and monitor
Basic.Response.
Validation
- Confirm that Port 2 transmits
N01TA$. - Confirm that the connected device returns a carriage-return-terminated response within 500 ms.
- Confirm that the returned value is written to
Basic.Response. - Confirm that
Basic.Tx()is called after the receive attempt.
Troubleshooting
| Symptom | Check |
|---|---|
| No response | Verify wiring, device address, port number, baud rate, data bits, stop bits, and parity. |
| Empty response | Verify that the device responds within 500 ms. |
| Incomplete or timed-out response | Verify that the device terminates the response with ASCII 13 (<CR>). |
| Polling does not start | Verify that an event or action calls Basic.Tx() for the first request. |
| Repeated requests are not issued | Verify that the receive program reaches the Basic.Tx() call. |
Limitations and Assumptions
- The example uses Port 2.
- The command is
N01TA$. - The expected response terminator is ASCII 13 (
<CR>). - The receive timeout is 500 ms.
- The example does not define the event that initiates the first call to
Basic.Tx(). - The exact connected device, protocol, hardware model, and firmware version were not provided.