Using the embedded Python and the IDL Python bridge, you can easily communicate between IDL and serial ports on your system.

To get started, first install the pyserial package:

IDL> pyutils.pipinstall, 'pyserial'

Next, plug your serial device into your computer. Then, in IDL, you can import the pyserial library and get a list of the available ports:

serial = Python.import('serial')
ports = Python.import('serial.tools.list_ports')
com_ports = ports.comports()
foreach p, com_ports do print, p.device

On Windows this will print out ports such as COM2 or COM3. On Linux and Mac the port names will look like /dev/ttyUSB0.

Once you have identified the correct port, you can then connect to it and begin communicating.

For example, here we connect an Arduino to a Windows machine and send a string back and forth:

serial = Python.import('serial')
arduino = serial.Serial(port="COM3", baudrate=9600, timeout=0.1)
wait, 2 ; give time to connect
!null = arduino.write(byte("hello"))
wait, 0.05
data = arduino.readline()
print, data
!null = arduino.close()

The above code assumes that we have a script running on the Arduino that can echo input (see below for the complete example).

Example Arduino Script


Using the Arduino Development Environment (IDE), upload the following C++ script to your Arduino:

String x;
void setup() {
  Serial.begin(9600);
  Serial.setTimeout(1);
}
void loop() {
  while (!Serial.available());
  x = Serial.readString();
  Serial.print(x);
}

This script will simply look for available data on the serial port, and then echo it back out.

Example IDL Script


The following IDL code will let you type strings at the command line. These strings will be sent to the Arduino, then read back into IDL and printed out to the console.

function serial_write_read, arduino, x
  !null = arduino.write(byte(x))
  wait, 0.05
  data = arduino.readline()
  return, data
end
 
serial = Python.import('serial')
ports = Python.import('serial.tools.list_ports')
com_ports = ports.comports()
foreach p, com_ports do print, p.device
port = com_ports[0].device   ; for example, 'COM3' on Windows or '/dev/ttyUSB0' on Linux
 
arduino = serial.Serial(port=port, baudrate=9600, timeout=0.1)
str = ""
while (!true) do begin
  read, str
  if (str eq "") then begin
    print, "Closing connection."
    !null = arduino.close()
    break
  endif
  value = serial_write_read(arduino, str)
  print, value
endwhile
end

Save the above code in an IDL file, called for example testserial.pro, and then run it. IDL will then prompt you for input. Once you type a string, it will send the string to the Arduino, which will immediately echo it back to be printed out at the IDL console. To stop the program and close the connection, just press return/enter to input an empty string.

IDL> .r testserial
% Compiled module: SERIAL_WRITE_READ.
% Compiled module: $MAIN$.
COM3
: hello
hello
: IDL is great!
IDL is great!
:
Closing connection.

Conclusion


The pyserial package has many more methods available on the serial class, all of which are available for use with IDL via the Python bridge. Consult the pyserial documentation for details.

See Also


Python Bridge, PyUtils