Instrument control package: Difference between revisions
Jump to navigation
Jump to search
→Compatibility
No edit summary |
|||
(45 intermediate revisions by 21 users not shown) | |||
Line 1: | Line 1: | ||
Instrument-Control is a package for interfacing the outside world of hardware via Serial, i2c or Parallel interfaces. It is currently under development by Andrius Sutas and Stefan Mahr, you can browse the | Instrument-Control is a package for interfacing the outside world of hardware via Serial, i2c or Parallel interfaces. It is currently under development by Andrius Sutas and Stefan Mahr, you can browse the [https://sourceforge.net/p/octave/instrument-control/ci/default/tree/ mercurial repository here] and download the package [http://octave.sourceforge.net/instrument-control/index.html here]. | ||
= Compatibility = | = Compatibility = | ||
Line 7: | Line 7: | ||
{| class="wikitable" style="text-align:center" | {| class="wikitable" style="text-align:center" | ||
|- | |- | ||
! !! Linux !! Windows (Cygwin) !! FreeBSD !! Mac OS X | ! !! Linux !! [[Octave for Microsoft Windows:GNU Octave on cygwin | Windows (Cygwin)]] !! Windows (native) !! FreeBSD !! Mac OS X | ||
|- | |- | ||
! Serial | ! Serial | ||
| bgcolor="green" | v0.1.0 || bgcolor=" | | bgcolor="green" | v0.1.0 || bgcolor="green" | v0.2.0 || bgcolor="green" | v0.2.1 || bgcolor="skyblue" | stalled || bgcolor="green" | v0.2.2 | ||
|- | |- | ||
! Parallel | ! Parallel | ||
| bgcolor="green" | v0.1.0 || - || bgcolor=" | | bgcolor="green" | v0.1.0 || - || - || bgcolor="skyblue" | stalled || - | ||
|- | |- | ||
! i2c | ! i2c | ||
| bgcolor="green" | v0.1.0 || - || bgcolor=" | | bgcolor="green" | v0.1.0 || - || - || bgcolor="skyblue" | stalled || - | ||
|- | |||
! modbus | |||
| bgcolor="green" | v0.8.0 || bgcolor="green" | v0.8.0 || bgcolor="green" | v0.8.0 || - || bgcolor="green" | v0.8.0 | |||
|- | |||
! spi | |||
| bgcolor="green" | v0.6.0 || - || - || - || - | |||
|- | |- | ||
! TCP | ! TCP | ||
| bgcolor=" | | bgcolor="green" | v0.2.0 || bgcolor="green" | v0.2.0 || bgcolor="green" | v0.2.0 || bgcolor="skyblue" | stalled || bgcolor="green" | v0.2.2 | ||
|- | |||
! UDP | |||
| bgcolor="green" | v0.3.0 || bgcolor="green" | v0.3.0 || bgcolor="green" | v0.3.0 || - || bgcolor="green" | v0.3.0 | |||
|- | |- | ||
! USBTMC | ! USBTMC | ||
| bgcolor=" | | bgcolor="green" | v0.2.0 || - || - || - || - | ||
|- | |- | ||
! GPIB | ! GPIB | ||
| bgcolor=" | | bgcolor="green" | v0.2.0 [[#Linux|(*)]] || bgcolor="green" | v0.2.0 [[#Windows_.28cygwin.29|(*)]] || - ||- || - | ||
|- | |- | ||
! VXI11 | ! VXI11 | ||
| bgcolor=" | | bgcolor="green" | v0.2.0|| bgcolor="green" | v0.2.0 [[#Windows_.28cygwin.29|(*)]] || - || - || bgcolor="yellow" | WIP [[#MacOS|(*)]] | ||
|} | |} | ||
Where: <span style="background:green">(VER)</span> - Included since VER version; <span style="background:yellow">(WIP)</span> - Work In Progress and will probably be included in next release; (-) - Not supported / Untested. | Where: <span style="background:green">(VER)</span> - Included since VER version; <span style="background:yellow">(WIP)</span> - Work In Progress and will probably be included in next release; <span style="background:skyblue">(stalled)</span> - Some work has been done, but it's not tested and no one is actively contributing; (-) - Not supported / Untested. | ||
You might see errors while doing "pkg install", which mean that package is unable to support related interface on your platform (e.g. due to missing header files), therefore one should <b>always</b> check for the supported interfaces before trying to use them: | You might see errors while doing "pkg install", which mean that package is unable to support related interface on your platform (e.g. due to missing header files), therefore one should <b>always</b> check for the supported interfaces before trying to use them: | ||
Line 37: | Line 46: | ||
{{Code|Check for interface support|<syntaxhighlight lang="octave" style="font-size:13px"> | {{Code|Check for interface support|<syntaxhighlight lang="octave" style="font-size:13px"> | ||
pkg load instrument-control | pkg load instrument-control | ||
supportedinterfaces = instrhwinfo().SupportedInterfaces | |||
if ( | if ! isempty(strfind (supportedinterfaces , "serial")) | ||
disp("Serial: Supported") | disp("Serial: Supported") | ||
else | else | ||
Line 45: | Line 55: | ||
#similarly with: | #similarly with: | ||
# | # ! isempty(strfind (supportedinterfaces , "parallel")) | ||
# | # ! isempty(strfind (supportedinterfaces , "i2c")) | ||
</syntaxhighlight>}} | </syntaxhighlight>}} | ||
= Requirements = | |||
== Linux == | |||
For GPIB support, please install [http://linux-gpib.sourceforge.net linux-gpib] before installing instrument-control. | |||
== Windows (cygwin) == | |||
For VXI11 support, please install 'rpcgen', 'libtirpc-devel', and 'libtirpc1' before installing instrument-control. | |||
To be able to use GPIB on windows, the linux-gpib library needs to be 'faked'. This can be done by copying the following script and run it in a bash-terminal. | |||
<syntaxhighlight lang="bash"> | |||
#!/bin/bash | |||
# fake_linux_gpib.sh, Kire Pûdsje, Dec 2017 | |||
# This file generates a fake linux-gpib library on Windows, based on the | |||
# installed NI libraries. | |||
# Prerequisits are that during install of the NI drivers, the C/C++ support | |||
# and the NI Measurement & Automation explorer has been enabled. | |||
# set the base for includes and libraries on your system | |||
# it is usually /usr, but on newer windows installs of octave, it will be | |||
# /mingw64 | |||
INSTALL_BASE=/usr | |||
# this define should find the NI header file, if properly installled | |||
# otherwise just point it to the proper header file. | |||
NI_H_FILE="${NIEXTCCOMPILERSUPP}/include/ni4882.h" | |||
NI_DLL_FILE=ni4882.dll | |||
NI_DEF_FILE=/tmp/${0##*/}-$$.def | |||
# symlink NI header file | |||
mkdir -p ${INSTALL_BASE}/include/gpib/ | |||
ln -sf "${NI_H_FILE}" ${INSTALL_BASE}/include/gpib/ib.h | |||
# generate .def for all functions in the NI header file with a NI488CC prefix | |||
echo LIBRARY ${NI_DLL_FILE} > ${NI_DEF_FILE} | |||
echo EXPORTS >> ${NI_DEF_FILE} | |||
grep "NI488CC *[A-Za-z]" ${INSTALL_BASE}/include/gpib/ib.h \ | |||
| sed "s/^.*NI488CC *//" \ | |||
| sed "s/(.*$//" >> ${NI_DEF_FILE} | |||
# generate the wrapper library simulating gpib-linux | |||
dlltool -d ${NI_DEF_FILE} -l ${INSTALL_BASE}/lib/libgpib.a | |||
#cleanup | |||
rm -f ${NI_DEF_FILE} | |||
</syntaxhighlight> | |||
== MacOS == | |||
You need to build '''rpcgen''' from source [http://mirror.ancl.hawaii.edu/pub/FreeBSD/FreeBSD-current/src/usr.bin/rpcgen]. | |||
If rpcgen cannot be build from source, the following error might be obtained: | |||
<syntaxhighlight lang="text" style="font-size:13px"> | |||
pkg: error running `make' for the instrument-control package. | |||
error: called from | |||
configure_make at line 99 column 9 | |||
install at line 184 column 7 | |||
pkg at line 437 column 9 | |||
</syntaxhighlight> | |||
If the '''VXI-11 interface''' is not required, a workarround is proposed in [https://stackoverflow.com/questions/46720092/make-error-when-installing-instrument-control-package-for-octave/52860282#52860282] after the bug reported on [https://savannah.gnu.org/bugs/?54842]. | |||
= Examples = | = Examples = | ||
Line 69: | Line 143: | ||
Here is a simple example to get started with the serial package. It tests the RxD and TxD lines. Make sure you have defined a loopback or connected the pins of your adapter. | Here is a simple example to get started with the serial package. It tests the RxD and TxD lines. Make sure you have defined a loopback or connected the pins of your adapter. | ||
{{Code|Serial port example|<syntaxhighlight lang="octave" style="font-size:13px"> | {{Code|Serial port example|<syntaxhighlight lang="octave" style="font-size:13px"> | ||
# Opens serial port ttyUSB1 with baudrate of 115200 (config defaults to 8-N-1) | # Opens serial port ttyUSB1 with baudrate of 115200 (config defaults to 8-N-1) | ||
s1 = serial("/dev/ttyUSB1", 115200) | s1 = serial("/dev/ttyUSB1", 115200) | ||
Line 84: | Line 156: | ||
}} | }} | ||
Changing some configurations is simply done by calling helper functions | |||
{{Code|Serial port example: helper functions|<syntaxhighlight lang="octave" style="font-size:13px"> | {{Code|Serial port example: helper functions|<syntaxhighlight lang="octave" style="font-size:13px"> | ||
set(s1, "baudrate", 9600) # Change baudrate | |||
set(s1, "bytesize", 5) # Change byte size (config becomes 5-N-1) | |||
set(s1, "parity", "E") # Changes parity checking (config becomes 5-E-1), | |||
# possible values [E]ven, [O]dd, [N]one. | |||
set(s1, "stopbits", 2) # Changes stop bits (config becomes 5-E-2), possible | |||
# values 1, 2. | |||
set(s1, "dataterminalready", "on") # Enables DTR line | |||
set(s1, "requesttosend", "on") # Enables RTS line | |||
# possible values "on", "off". | |||
</syntaxhighlight> | </syntaxhighlight> | ||
}} | }} | ||
Line 103: | Line 180: | ||
Do not forget to close the ports when you are done! | Do not forget to close the ports when you are done! | ||
{{Code||<syntaxhighlight lang="octave" style="font-size:13px"> | {{Code||<syntaxhighlight lang="octave" style="font-size:13px"> | ||
fclose(s1) # Closes and releases serial interface object | |||
fclose(s2) # Closes and releases serial interface object | |||
</syntaxhighlight> | |||
}} | |||
=== Example: Windows, serial port >COM9 === | |||
Opening a serial port higher than COM9 requires special syntax. [https://support.microsoft.com/en-us/kb/115831] | |||
{{Code|open Windows higher serial port example|<syntaxhighlight lang="octave" style="font-size:13px"> | |||
# Opens serial port COM10 | |||
s1 = serial("\\\\.\\COM10"); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
}} | }} | ||
Line 283: | Line 367: | ||
== i2c == | == i2c == | ||
i2c | Example with a Si7021 i2c breakout board (https://www.sparkfun.com/products/1376) connected to a i2c master interface. | ||
To work out the devices available (assuming i2ctools are installed) run the i2cdetect command from a terminal window. | |||
<syntaxhighlight lang="bash" style="font-size:13px"> | |||
$ i2cdetect -l | |||
i2c-1 i2c i2c-ch341-usb at bus 002 device 008 I2C adapter | |||
i2c-0 unknown SMBus I801 adapter at 4000 N/ | |||
</syntaxhighlight> | |||
In the example case the temperature sensor is connected to the i2c-ch341-usb device, and it is assumed the user has sufficient permissions to access the device. | |||
According to the datasheet, the temperature device uses address 0x40, so create a i2c device using the linux device and address: | |||
<syntaxhighlight lang="octave" style="font-size:13px"> | |||
i2cdev = i2c("/dev/i2c-1", 0x40) | |||
</syntaxhighlight> | |||
To read the temperature, register 0xF3 must be addressed and read (2 bytes of data): | |||
<syntaxhighlight lang="octave" style="font-size:13px"> | |||
TEMP_MEASURE_NOHOLD = hex2dec("F3"); | |||
fwrite (i2cdev, uint8([TEMP_MEASURE_NOHOLD])); | |||
pause (0.02); | |||
data = fread (i2cdev, 3); | |||
</syntaxhighlight> | |||
The data must be converted to a deg K value by making 16 bit number of the value and masking out unused bits. | |||
<syntaxhighlight lang="octave" style="font-size:13px"> | |||
value = uint16(data(1))*256 + uint16(data(2)); | |||
value = bitand (value, hex2dec("FFFC")); | |||
temp_Code = double(value); | |||
</syntaxhighlight> | |||
Now convert the temperature to degrees C and display: | |||
<syntaxhighlight lang="octave" style="font-size:13px"> | |||
C = (175.72*temp_Code/65536)-46.85; | |||
printf ("temperature read %f C\n", C); | |||
</syntaxhighlight> | |||
== TCP == | == TCP == | ||
Line 347: | Line 471: | ||
# Convert uint8 array to string, | # Convert uint8 array to string, | ||
char(data) | char(data) | ||
# close | # close VXI11 session | ||
vxi11_close(t0) | vxi11_close(t0) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
}} | }} | ||
=== Limitations === | === Limitations === | ||
For now, | For now, | ||
Line 378: | Line 503: | ||
* Every read or write command opens and closes a new gpib session, since the linux-gpib session pointer is only valid for single command. | * Every read or write command opens and closes a new gpib session, since the linux-gpib session pointer is only valid for single command. | ||
[[Category: | == SPI == | ||
[[Category:Packages]] | === Configuring interface === | ||
SPI currently only works in linux. | |||
instrhwinfo will display the avilable devices in the system | |||
{{Code|SPI example|<syntaxhighlight lang="octave" style="font-size:13px"> | |||
instrhwinfo("spi") | |||
</syntaxhighlight> | |||
}} | |||
You will need read/write permissions to the device used. | |||
=== Example: basic use === | |||
Example opening an performing IO on a device /dev/spidev0.0. | |||
{{Code|SPI example|<syntaxhighlight lang="octave" style="font-size:13px"> | |||
# open device | |||
spidev = spi("/dev/spidev0.0", 'clockpolarity', 'idlehigh', 'clockphase', 'firstedge') | |||
# I/O to device | |||
data = writeAndRead (spidev, uint8([0x01 0x00])) | |||
# close the device | |||
clear spidev | |||
</syntaxhighlight> | |||
}} | |||
[[Category:Octave Forge]][[Category:Packages]] |