Port Selection
There may be platforms with more than one serial port. You can obtain the number of serial ports available for your platform using the ser.numofports property.
Since there can be multiple ports, you must state which port you are referring to when changing properties or invoking methods. This is done using the ser.num property. For example:
** Tibbo Basic **
ser.mode = PL_SER_MODE_WIEGAND
Can you tell what serial port the statement above applies to? Neither can the platform. Thus, the correct syntax would be:
** Tibbo Basic **
ser.num = 0
ser.mode = PL_SER_MODE_WIEGAND
Now the platform knows what port you're working with. Once you have set the port selector (using ser.num), every method and property after that point is taken to refer to that port. Thus:
** Tibbo Basic **
ser.num = 0
ser.enabled = 1
ser.baudrate = 1
ser.bits = 1 ' etc
The events generated by the ser object are not separate for each port. An event such as on_ser_data_arrival serves all serial ports on your platform. Thus, when an event handler for the serial port object is entered, the port selector is automatically switched to the port number on which the event has occurred:
** Tibbo Basic **
sub on_ser_data_arrival
dim s as string
s = ser.getdata(255) ' Note that you did not have use ser.num before this statement.
end sub
As a result of this automatic switching, when an event handler for a serial port event terminates, the ser.num property retains its new value (nothing changes it back). You must take this into account when processing other event handlers which make use of the serial port (and are not serial port events). In other words, you should explicitly set the ser.num property whenever entering such an event handler, because the property might have been automatically changed prior to this event. To illustrate:
** Tibbo Basic **
sub on_sys_init ' This is always the first event executed.
ser.num = 0 ' Supposedly, this would make all subsequent properties and methods refer to this port.
end sub
sub on_ser_data_arrival ' Then, supposing this event executes.
dim s as string
s = ser.getdata(255) ' However, this event happens on the second port. So now ser.num = 1.
end sub
sub on_sock_data_arrival ' And then this socket event executes.
ser.txclear ' You meant to do this for ser.num = 0 (as specified at on_sys_init). But now port.num was changed to 1. You did not explicitly specify a ser.num here, and now the tx buffer for the wrong port is cleared. Oops.
end sub
Same precautions should be taken when using doevents. This is because doevents will let other events execute and so serial object events will potentially execute and cause the ser.num to change.
To recap, only one of two things may change the current ser.num: (1) Manual change or (2) a serial port event. And you cannot assume the number has remained unchanged if you set it somewhere else (because a serial port event might have happened since).
Specifying a port number for a single-port platform may seem redundant, but it makes your program portable. You will have an easier time migrating your program to a multi-port platform in the future. |