|Top Previous Next|
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:
Can you tell what serial port the statement above applies to? Neither can the platform. Thus, the correct syntax would be:
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:
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:
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:
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).