User Tools

Site Tools

한국어

cuwin:reading_from_the_cuwin_s_serial_port:index

Reading from the CUWIN's Serial Port

We have successfully built an program that writes data to the CUWIN's serial port, sending it to HyperTerminal on the PC, but communication is usually bi-directional. We will now modify this program to read input from HyperTerminal, and send it to the CUWIN.

1. Add a “Label” from the Toolbox to the form, and label it “PC: ” by changing its Text property.

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
 
    //Listen for incoming data
    _port.DataReceived += new SerialDataReceivedEventHandler(_port_DataReceived);
    _port.Open();
}
 
void _port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    //Update label1 with the data read
    string dataRead = _port.ReadExisting();
    label1.Invoke(new Action(() => label1.Text += dataRead));
}

2. Go to the Form.cs tab, and add the _port_DataReceived event handler exactly as pictured above.

How the Code Works:

1. When data is received on the CUWIN's serial port, the DataReceived event is fired, which executes the _port_DataRecieved event handler.

2. In the _port_DataRecieved event handler, we read the data from the CUWIN's serial port, and append the data to the “PC:” label. NOTE: The Invoke method is necessary because the _port_DataRecieved event handler will be running on a different thread than the one used to create the label. The code inside the Invoke method is a lambda expression. See the Microsoft Development Network (MSDN) documentation for Control.Invoke and Lambda Expressions for more information.

Configuring HyperTerminal to Accept Keystrokes

Now we must configure HyperTerminal to accept keystrokes, show them on the screen, and send them to the CUWIN.

1. In HyperTerminal with the CUWIN connection open, select File→Properties from the menu. The “CUWIN Properties” dialog will appear. On the “Settngs” tab, click the “ASCII Settup…” button.

2. Check the “Echo typed characters locally” checkbox. This will ensure that as we type in HyperTerminal, our keystrokes will appear on the screen. Click the “OK” button, then click the “OK” button on the “CUWIN Properties” dialog.

3. Hyperterminal will not accept keyboard input until the Scroll Lock is turned off. Press the “Scroll Lock” key on your keyboard until the status bar shows “Scroll” in gray (disabled). Now if we type in HyperTerminal, what we type will display on the screen and will be sent to the CUWIN.

Running the Program

We are now ready to run the program.

1. Once again, execute the CUWIN program from Visual Studio.

2. When the form appears on the CUWIN, touch the “Say Hello” button and “Hello!” will appear in HyperTerminal. Then type “Hi!” in HyperTerminal and the CUWIN will display “Hi!” in the “PC:” label.

cuwin/reading_from_the_cuwin_s_serial_port/index.txt · Last modified: 2016/04/14 11:07 (external edit)