writeBytes Function

Top  Previous  Next

Sends binary data to the serial port.

Syntax

Cell.Serial[n].writeBytes(data [, data ...])

Arguments

nThe serial port number, 0 - 2.
dataOne or more values to send as binary data.

Return Value

Zero if successful, or a negative number if failure.

Remarks

The data sent for each data argument depends on its type as shown in the table:

 

Type

Data Sent

Number

Truncated to an integer and only the low byte is sent.

String

The characters of the string are sent.

Boolean

Byte value 0 is sent if false or 1 if true.

Undefined

Skipped.

Null

Skipped.

Array

Each element handled per this table, except nested arrays not permitted.

 

If the serial port's output buffer has sufficient free space, this function will return immediately. Otherwise, execution will pause while data is sent. The bytesCanWrite property returns how many bytes are free in the output buffer.

 

This function can fail if the serial port has not been enabled by setting its bitRate property to a positive value.

Examples

// Command a DirecTV receiver to change channel

// 16-bit values are transmitted high byte first

function ChanChange(chan, subChan)

{

 Cell.Serial[0].bitRate = 9600;

 if (subChan == undefined)

   subChan = 0xFFFF;

 Cell.Serial[0].writeBytes(0xFA, 0xA6); // command

 Cell.Serial[0].writeBytes(chan.byteMidLo, chan.byteLo);

 Cell.Serial[0].writeBytes(subChan.byteMidLo, subChan.byteLo);

}

ChanChange(202);// tune to CNN

// 6 bytes sent: FA A6 00 CA FF FF

See Also

write Function, bytesCanWrite Property