Redirecting Buffers

Top  Previous  Next

The following example appeared under Receiving Data:

 

 

sub on_ser_data_arrival

 ser.setdata(ser.getdata(ser.txfree))

 ser.send

end sub

 

 

This example shows how to send all data incoming to the RX buffer out from the TX buffer, in just two lines of code. However fast, this technique still passes all data through your BASIC code, even though you are not processing (altering, sampling) it in any way.

A much more efficient and advanced way to do this would be using a technique called buffer redirection (buffer shorting). With buffer shorting, instead of receiving the data into the RX buffer of your serial port, you are receiving it directly into the TX buffer of another object which is supposed to send out this data. This can be a serial object (the same port or a different one), a socket object, etc.

To use buffer shorting, you invoke the ser.redir method and specify the buffer to which the data is to be redirected. Once this is done, the on_ser_data_arrival event won't be generated at all, because the data will be going directly to the TX buffer that you have specified. As soon as the data enters this buffer, it will be automatically committed for sending.

The ser.redir method will only work if the serial port is closed (ser.enabled= 0- NO) at the time when this method is executed. Therefore, it makes sense to check the result of ser.enabled execution, as in the example below:

 

 

sub on_sys_init

 

 if ser.redir(PL_REDIR_SER)= PL_REDIR_SER then

         'redirection succeeded

 else

         'redirection failed (perhaps, the port is opened?)

 end if

end sub

 

 

The performance advantage of buffer shorting is enormous, due to two factors: first, you are not handling the data programmatically, so the VM isn't involved at all. And second, the data being received is received directly into the TX buffer from which it is transmitted, so there is less copying in memory.

Of course you cannot do anything at all with this data — you are just pumping it through. However, very often this is precisely what is needed! Additionally, you can still catch escape sequences.

To stop redirection, you can use ser.redir(0), which means "receive data into the RX buffer in a normal fashion".