====== Interfacing the CUWIN to Other Devices ======
The CUWIN has several different interfaces with which it can interface to other devices. In the
exercises to follow we will make use of the Recommended Standard 232 (RS-232) interface, also
known as a serial port or com port. We will use RS-232 to have the CUWIN communicate with a PC
and the CUBLOC.
===== RS-232 =====
RS-232 has been around for many years and is commonly used in the HMI industry as a way for
devices to communicate with one another. To illustrate how the CUWIN can use RS-232 to
communicate with other devices, we will program the CUWIN to send messages to a PC.
The PC will display the received messages in HyperTerminal. HyperTerminal is an application included in all the
latest versions of Windows, and is well-suited for RS-232 communication. Finally we will modify this
program to read keystrokes in HyperTerminal and display them on the CUWIN.
====== Writing to the CUWIN's Serial Port ======
For this exercise, you will need to connect an RS-232 cable from CUWIN's COM1 serial port to a PC's
serial port. The CUWIN and most PCs have more than one serial port, so be aware which serial port
you are using. For this exercise, we will assume that both the CUWIN and the PC will use their COM1
serial port.
{{ :cuwin:writing_to_the_cuwin_s_serial_port:cuwin_serial_port.png?nolink |}}
You should now have two connections between the CUWIN and the PC: USB and RS-232.
{{ :cuwin:writing_to_the_cuwin_s_serial_port:cuwin_serial_port1.png?nolink |}}
1. Create a new smart device project as we did for HelloWorld, and name it HelloHyperTerminal.
{{ :cuwin:writing_to_the_cuwin_s_serial_port:cuwin_serial_port2.png?nolink |}}
2. Make the project a “Device Application” just as we did for the HelloWorld project.
{{ :cuwin:writing_to_the_cuwin_s_serial_port:cuwin_serial_port3.png?nolink |}}
3. Add a large button to the form and label it “Say Hello” just as we did for the HelloWorld project
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO.Ports;
namespace HelloHyperTerminal
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SerialPort _port;
private void Form1_Load(object sender, EventArgs e)
{
_port = new SerialPort("Com1", //Com port
19200, //Baud Rate
Parity.None, //Parity
8, //Data Bits
StopBits.One); //Stop Bits
_port.Open();
}
}
}
4. Double-click in any blank space on the form, and Visual Studio will create a ''Form1_OnLoad''
event handler for you. Add code to this event handler exactly as pictured above.
{{ :cuwin:writing_to_the_cuwin_s_serial_port:cuwin_serial_port4.png?nolink |}}
5. Go back to the “Form1.cs [Design]” tab. Select the form, and in the “Properties” window click
the lightning bolt icon . This will display all of the events that you can attach event handlers
to. Double-click the //Closing// event and a ''Form1_Closing'' event handler will be created.
private void Form1_Closing(object sender, CancelEventArgs e)
{
//Close the serial port
_port.Close();
}
6. Add code to this event handler exactly as pictured above.
private void button1_Click(object sender, EventArgs e)
{
//Send "Hello!" to HyperTerminal
_port.Write("Hello!\r\n");
}
7. Go back to the “Form1.cs [Design]” tab, and double-click the “Say Hello” button. This will add
a ''button1_Click'' event handler. Add code to this event handler exactly as pictured above.
{{ :cuwin:writing_to_the_cuwin_s_serial_port:cuwin_serial_port5.png?nolink |}}
8. Finally adjust the project properties just as we did in the HelloWorld project
===== How the Code Works: =====
1. When the form opens, the form's ''Load'' event fires executing the ''Form1_Load'' event handler.
This event handler configures the COM1 serial port and opens it so we can write to it.
2. When we close the form, the form's ''Closing'' event fires calling the ''Form1_Closing'' event
handler. This event handler closes the COM1 serial port.
3. When we touch the “Say Hello” button, the button's ''Click'' event fires calling the
''button1_Click'' event handler. This event handler writes “Hello!” to the serial port. The “\r”
and “\n” characters are a carriage return and line feed, so each “Hello!” we send appears on a
new line.
===== Displaying our “Hello!” Message on the PC =====
We now need some device to read and display the “Hello!” being sent from the CUWIN. For this we
will use the program HyperTerminal which is included in all recent versions of Windows.
{{ :cuwin:writing_to_the_cuwin_s_serial_port:display_on_pc.png?nolink |}}
1. Start HyperTerminal by selecting Start-->All Programs-->Accessories-->Communications--
>HyperTerminal in the Windows start menu.
{{ :cuwin:writing_to_the_cuwin_s_serial_port:display_on_pc1.png?nolink |}}
2. HyperTerminal will open and display a “Connection Description” dialog. It is here we need to
configure the PC's serial port. Enter “CUWIN” in the “Name” textbox and click the “OK” button.
{{ :cuwin:writing_to_the_cuwin_s_serial_port:display_on_pc2.png?nolink |}}
3. In the “Connect Using” drop-down list, select the serial port you wish to use. Note that this is
the PC's serial port, not the CUWIN's serial port; they may be different. Then, click the “OK”
button.
{{ :cuwin:writing_to_the_cuwin_s_serial_port:display_on_pc3.png?nolink |}}
4. A new dialog will appear asking for your port settings. These settings must match those
settings made in the Form1_Load event handler of the CUWIN program or the PC and the
CUWIN will not be able to communicate with one another. Adjust the settings appropriately,
then click the “OK” button.
{{ :cuwin:writing_to_the_cuwin_s_serial_port:display_on_pc4.png?nolink |}}
5. Execute the CUWIN program from Visual Studio.
{{ :cuwin:writing_to_the_cuwin_s_serial_port:display_on_pc5.png?nolink |}}
6. The form will display on the CUWIN. Click the “Say Hello” button.
{{ :cuwin:writing_to_the_cuwin_s_serial_port:display_on_pc6.png?nolink |}}
7. Take a look at the PC's HyperTerminal window. You will see the “Hello!” message sent from the
CUWIN.