Keysight M8000 Series of BER Test Solutions User Guide 577
Utilities 9
buf = System.Array[System.Byte] ([0x5A, 0x02, 0x00,
0xaf])
# send 4 bytes beginning at offset 0
serialPort.Write(buf, 0, 4)
# reading 100 bytes
numBytes = 100
buf = System.Array.CreateInstance(System.Byte, numBytes)
serialPort.Read(buf, 0, numBytes)
Closing the COM Port
The following code snippet shows the required code to close the COM port
and discard the SerialPort instance. This code should be called in the
OnUninstall function of the DUT Control Interface in order to ensure that
the COM port becomes unavailable after the currently active script
becomes removed from the computer's memory.
if (serialPort != None):
# check if COM port is open
if (serialPort.IsOpen):
serialPort.Close()
# dispose the serialPort instance
serialPort.Dispose()
# ensure that the disposed object is not used in future
serialPort = None
Common Pitfalls when Working with COM Ports
A COM port cannot be opened more than once. Therefore it is very
important to close the port before the script is unloaded.
This does have several implications:
• If the COM port is not available when the script is being loaded (e.g.
when recalling a setting), then the script will fail opening the COM port
and the DUT will not be accessible.