Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 10


An example of an application that expects to receive data at specific times is a
computer that sends commands and expects a response after each command. If
the remote computer typically responds quickly, the application might wait for
the response:

 Dim receivedData As String


' Send a command.

myComPort.WriteLine("CMD1")

' Wait for a response.

receivedData = myComPort.ReadLine
Console.WriteLine(receivedData)

 String receivedData;


// Send a command.

myComPort.WriteLine("CMD1");

// Wait for a response.

receivedData = myComPort.ReadLine();
Console.WriteLine(receivedData);
Another option for polling is to use a Timer component to trigger periodic
reads of the port. Drag a Timer component from Visual Studio’s Toolbox onto
a form. Set the interval in milliseconds for polling the port.

 tmrPollForReceivedData.Interval = 1000
tmrPollForReceivedData.Stop()


 tmrPollForReceivedData.Interval = 1000;
tmrPollForReceivedData.Stop();
When the port has been opened, start the timer:


 tmrPollForReceivedData.Start()


 tmrPollForReceivedData.Start();

Free download pdf