Using Buffers

Top  Previous  Next

Once you have allocated memory for the TX and RX buffers you can start sending and receiving data through them.

Sending Data (through TX buffer)

Sending data is a two-step process. First, you put the data in the TX buffer using the ser.setdata method, and then you perform the actual sending (commit the data) using the ser.send method. For example:

 

 

ser.setdata ("Foo") ' Placed our data in the TX buffer - not being sent out yet.

 

' ... more code...

 

ser.setdata ("Bar") ' Added even more data to our buffer, waiting to be sent.

 

ser.send ' Now data will actually start going out. Data sent will be 'FooBar'.

 

 

Since this is a two-step process, you may first prepare a large block of data in the TX buffer and only then commit this data (this may come handy in some applications).

note_further-wt

TiOS features non-blocking operation. This means that on ser.send, for example, the program does not halt and wait until the data is completely sent. In fact, execution resumes immediately, even before the first byte goes out. Your program will not freeze just because you ordered it to send a large chunk of data.

Receiving Data (through RX buffer)

Receiving data is a one-step process. To extract the data from a buffer, use the ser.getdata method. Data may only be extracted once from a buffer. Once extracted, it is no longer in the buffer. For example:

 

 

dim whatigot as string

whatigot = ser.getdata(255)

 

 

The variable whatigot now contains up to 255 bytes of data which came from the TX buffer of the serial port.

It is also possible to preview the data in the RX buffer without actually extracting it. Use ser.peekdata to achieve this.