5 using single byte reads – Casio Naurtech CETerm Ver.5.5 Scripting Guide User Manual

Page 117

Advertising
background image

N

AURTECH

W

EB

B

ROWSER AND

T

ERMINAL

E

MULATION FOR

W

INDOWS

CE

AND

W

INDOWS

M

OBILE




CETerm Scripting Guide

Page 117


5.8.5 Using Single Byte Reads

One pattern which often works well for handling serial port data is to read a
single byte at a time and to accumulate data until a “complete message” can be
processed. Your application and peripheral device wil

l define what a “complete

message” contains, but often messages are terminated by special characters
such as ASCII ETX. When using single-byte reads, you can keep read timeouts
short and optimize responsiveness.

To use this pattern, you would use WaitForEvent with the EV_RXCHAR event
and process the data within your handler. Here is a template for a single byte
read handler

// Serial port data handler
var responseData = [];
var responseState = "PRE_STX";

function MyReadAndProcess( portIndex )
{
var c;
var readTries = 4;
var sp = Device.SerialPort( portIndex );

while (responseState !== "DONE" &&
readTries > 0)
{
c=sp.ReadByte();
if (c < 0)
{
// Read error, try again
OS.Sleep( 20 );
--readTries;
continue;
}

// Reset tries after successful read
readTries = 4;

switch(c)
{
case ASCII_STX:
// Start of response.
responseState = "DATA";
responseData = [];
break;

case ASCII_ETX:
// End of content
responseState = "DONE";
break;

Advertising
This manual is related to the following products: