The Serial Port commands in Python™ allow you to connect to a COM port and send or receive data.
See Python commands for other commands you can use with GainSeeker.
Syntax |
Example |
Description/Remarks |
New or changed in GainSeeker version |
serial.dispmessage("Place first sample in fixture") |
Opens a popup window with message text string and one button ("Click here to continue"). In the message, tabs ("\t") can be used to align and space the text. Valid options for textalign are 0 (align Center, the default), 1 (align Left), and 2 (align Right). If the message to be displayed is a number, convert the number to a string using the str() method. Returns no value. |
9.2 |
|
This first two lines of code are executed in the Inspection: serial.persist["name"] = "Bob" serial.persist["measurement"] = 14.7513 This second two lines of code are executed in the Device Profile: print serial.persist["name"] print str(serial.persist["measurement"]) The values printed in the Device Profile should be "Bob" and "14.7513". |
Stores values that can be accessed and changed by Inspection scripts and Device Profile scripts. Inspections and Device Profiles run within different scopes, so any information that must be passed between them may be stored using this command. "mykey" must be a string and is case-sensitive. For any "mykey", you can set its persistent value to a string or number. |
9.1 |
It is recommended that you use the Serial Setup action to configure your serial port connections.
All commands must use an index [x] to reference the connection to a COM port. This allows you to set up and use multiple serial connections.
There is no default serial connection. All serial connections are temporary.
Syntax |
Example |
Description/Remarks |
New or changed in GainSeeker version |
serial[0].allowkeyboard = True |
Set to False to prevent user from temporarily switching to Keyboard input by pressing CTRL+K on the serial input window. Defaults to True. Can affect the behavior of the serial[x].readcolumn command. |
|
|
serial[x].baudrate |
serial[0].baudrate = 9600 |
Gets or sets the baud rate. Defaults to 9600. |
|
serial[x].bufferlen |
print serial[0].bufferlen |
Returns the current length (in bytes) of data in the read buffer. -1 if the port is closed. |
|
serial[x].clearbuffer() |
serial[0].clearbuffer() |
Clears the current contents of the serial port buffer. |
|
serial[x].close() |
serial[0].close() |
Closes an open serial port connection. |
|
serial[x].databits |
serial[0].databits = 8 |
Gets or sets the number of data bits, usually 7 or 8. Defaults to 8. |
|
serial[0].eolstring = "\r" |
Gets or sets the end of line character(s). This is used by the serial[x].readline() command. Defaults to "\r". |
|
|
serial[x].filemode |
serial[0].filemode = True |
Set to True to set the serial port to file mode. Used for compatibility with serial port redirections. Defaults to False. |
|
serial[x].handshake |
serial[0].handshake = "None" |
Gets or sets the handshake, valid values are "None", "Xon", "Rts", or "RtsXon". Defaults to "None". "Rts" indicates that hardware flow control is used. Values are not case sensitive. |
|
serial[x].isopen |
print serial[0].isopen |
Returns True if the port is open. |
|
serial[x].maxbuffersize |
serial[0].maxbuffersize = 1200 |
Gets or sets the size of the read buffer. Default will vary based on hardware. |
|
serial[x].message |
serial[0].message = "Weigh item now" |
Sets the message displayed to the user while waiting for input from the serial port when serial[x].readline() or serial[x].readcolumn() is called. |
|
serial[x].open() |
serial[0].open() |
Opens a serial port connection. Returns True if successful. |
|
serial[x].parity |
serial[0].parity = "N" |
Gets or sets the parity. "N" = None, "E" = Even, "O" = Odd, "M" = Mark, and "S" = Space. Defaults to "N" (None). |
|
serial[0].portnumber = 3 |
Gets or sets the port number. Defaults to 1. |
|
|
print serial[0].readall() |
Reads the current contents of the buffer and returns it as a string. Does not prompt the user or wait for a new value to be added to the buffer. |
|
|
serial[x].readcolumn(index, spliton=",", isnumeric=True, noempty=False) |
print serial[0].readcolumn(1, ",", True, True) |
Performs a readline() and then splits the output into columns based on the character specified by spliton. Returns the value for the specified column index returned by the split. If user switches to keyboard input and submits data, this command returns the value for column index 0. If isnumeric is True, the value to be returned should be a number. If noempty is True, empty columns are automatically removed and not counted for the index. If the inspector clicks Cancel, this returns None. |
|
print serial[0].readline() |
Reads a line of content from the buffer and returns it as a string. A line is defined as a string that ends with the characters specified in the eolstring property. If the inspector clicks Cancel, this returns None. |
|
|
serial[x].readsysdefaults() |
serial[0].readsysdefaults() |
Reads the system settings for a particular serial[x].portnumber. Returns True if successful. |
|
serial[x].stopbits |
serial[0].stopbits = 1 |
Gets or sets the number of stop bits, valid values are 1, 1.5, or 2. Defaults to 1. |
|
serial[x].write(message) |
serial[0].write("signal") |
Writes the specified message to the serial port. |
|
Following is a sample RS232 script to read a value from Channel A or a GageWay 5 Plus.
#Script name: RS232
#setup parameters for COM port 3. Using 9600, N, 8 1.
serial[0].portnumber = 3
serial[0].baudrate = 9600
serial[0].parity = "N"
serial[0].databits = 8
serial[0].stopbits = 1
serial[0].handshake = "None"
serial[0].eolstring = "\r"
serial[0].filemode = False
#open the COM port
serial[0].open()
#this code reads in a value from a Mitutoyo Gage on a Microridge GageWay 5 Plus
#assumes the device output is similar to:
# 199.9\r
#Tell the GageWay to send a reading from Channel A
serial[0].write("<RA")
#read the value from the COM port buffer
strInput = serial[0].readline()
#do something with the value here. For this example, just write to the debug console.
print strInput
#All done, so close the serial port. This is not really needed as all serial ports get closed when the script finishes.
serial[0].close()