Instrument control package: Difference between revisions
no edit summary
(→i2c) |
No edit summary |
||
(13 intermediate revisions by 7 users not shown) | |||
Line 17: | Line 17: | ||
! i2c | ! i2c | ||
| bgcolor="green" | v0.1.0 || - || - || bgcolor="skyblue" | stalled || - | | bgcolor="green" | v0.1.0 || - || - || bgcolor="skyblue" | stalled || - | ||
|- | |||
! modbus | |||
| bgcolor="yellow" | WIP || bgcolor="yellow" | WIP || bgcolor="yellow" | WIP || - || bgcolor="yellow" | WIP | |||
|- | |||
! spi | |||
| bgcolor="green" | v0.6.0 || - || - || - || - | |||
|- | |- | ||
! TCP | ! TCP | ||
Line 40: | 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 48: | Line 55: | ||
#similarly with: | #similarly with: | ||
# | # ! isempty(strfind (supportedinterfaces , "parallel")) | ||
# | # ! isempty(strfind (supportedinterfaces , "i2c")) | ||
</syntaxhighlight>}} | </syntaxhighlight>}} | ||
Line 63: | Line 70: | ||
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. | 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 | #!/bin/bash | ||
# fake_linux_gpib.sh, Kire Pûdsje, Dec 2017 | # fake_linux_gpib.sh, Kire Pûdsje, Dec 2017 | ||
Line 99: | Line 106: | ||
#cleanup | #cleanup | ||
rm -f ${NI_DEF_FILE} | rm -f ${NI_DEF_FILE} | ||
</ | </syntaxhighlight> | ||
== MacOS == | == MacOS == | ||
Line 364: | Line 371: | ||
To work out the devices available (assuming i2ctools are installed) run the i2cdetect command from a terminal window. | 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 | $ i2cdetect -l | ||
i2c-1 i2c i2c-ch341-usb at bus 002 device 008 I2C adapter | i2c-1 i2c i2c-ch341-usb at bus 002 device 008 I2C adapter | ||
i2c-0 unknown SMBus I801 adapter at 4000 N/ | 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, | 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) | i2cdev = i2c("/dev/i2c-1", 0x40) | ||
</syntaxhighlight> | |||
To read the | 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"); | TEMP_MEASURE_NOHOLD = hex2dec("F3"); | ||
fwrite (i2cdev, uint8([TEMP_MEASURE_NOHOLD])); | fwrite (i2cdev, uint8([TEMP_MEASURE_NOHOLD])); | ||
pause (0.02); | pause (0.02); | ||
data = fread (i2cdev, 3); | data = fread (i2cdev, 3); | ||
</syntaxhighlight> | |||
The data | 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 = uint16(data(1))*256 + uint16(data(2)); | ||
value = bitand (value, hex2dec("FFFC")); | value = bitand (value, hex2dec("FFFC")); | ||
temp_Code = double(value); | temp_Code = double(value); | ||
</syntaxhighlight> | |||
Now convert the temperature to degrees C and display: | Now convert the temperature to degrees C and display: | ||
<syntaxhighlight lang="octave" style="font-size:13px"> | |||
C = (175.72*temp_Code/65536)-46.85; | C = (175.72*temp_Code/65536)-46.85; | ||
printf ("temperature read %f C\n", C); | printf ("temperature read %f C\n", C); | ||
</syntaxhighlight> | |||
== TCP == | == TCP == | ||
Line 464: | 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 495: | 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:Octave Forge]] | == SPI == | ||
=== 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]] |