USB TC-08 in python

Post general discussions on using our drivers to write your own software here
cwhite00
Newbie
Posts: 0
Joined: Fri Jan 06, 2012 12:54 pm

USB TC-08 in python

Post by cwhite00 »

Hi,

I am hoping someone a lot more clued up on programming can help me with this:

Here is the code I am using

Code: Select all

import ctypes

mydll = ctypes.windll.LoadLibrary('usbtc08.dll')
device = mydll.usb_tc08_open_unit()
mydll.usb_tc08_set_mains(device, 0)

import numpy as np
temp = np.zeros( (9,), dtype=np.float32)
overflow_flags = np.zeros( (9,), dtype=np.int16)


for i in range(0,9):
    mydll.usb_tc08_set_channel(device, i, 'K' )
    
mydll.usb_tc08_get_single( device, temp.ctypes.data, overflow_flags.ctypes.data, 0) 

temp
The result I get is:


1
1
0
0
0
0
0
0
0
0
1
array([ 22.58154488, nan, nan, nan,
nan, nan, nan, nan, nan], dtype=float32)

There seems to be a problem with the 'mydll.usb_tc08_set_channel command'
It will accept 0 but not 1 - 8. This is why I only get the cold junction temperature.

Does anyone have any ideas??

Hitesh

Re: USB TC-08 in python

Post by Hitesh »

Hi cwhite00,

Looking at your programming calls, it would be helpful if you can call usb_tc08_get_last_error after each call to set the channel and send in the error codes returned by the function.

Alternatively, just try setting the cold junction and channels 1 and 2, and send in the error codes.

Have you got thermocouples in all of the ports?

Unfortunately I have not programmed using Python before.

Regards,

sgunthr
Newbie
Posts: 0
Joined: Fri Jan 20, 2012 6:14 pm

Re: USB TC-08 in python

Post by sgunthr »

Hello,
Using cwhite00's code I get an: "AttribueError: 'numpy.ndarray' object has no attribute 'ctypes'"

Any one know why?

miky
Newbie
Posts: 0
Joined: Mon May 27, 2013 3:22 pm

Re: USB TC-08 in python

Post by miky »

did any one resolve this problem.
I get the same result. The error that i get after each channel settings is:
<_FuncPtr object at 0x03681D50>

Martyn
Site Admin
Site Admin
Posts: 4483
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: USB TC-08 in python

Post by Martyn »

This code, supplied by Miky, works.

Code: Select all

import ctypes
import numpy as np
from ctypes import *
mydll = ctypes.windll.LoadLibrary('usbtc08.dll')
device = mydll.usb_tc08_open_unit()
mydll.usb_tc08_set_mains(device,50)

temp = np.zeros( (9,), dtype=np.float32)
overflow_flags = np.zeros( (1,), dtype=np.int16)
mydll.usb_tc08_set_channel(device, 0, 0 )
tc_type=ord('K')
for i in xrange(1,3):
    mydll.usb_tc08_set_channel(device,i,tc_type)

mydll.usb_tc08_get_single( device, temp.ctypes.data, overflow_flags.ctypes.data, 0) 
mydll.usb_tc08_close_unit(device)
print temp[0],temp[1],temp[2]
Martyn
Technical Support Manager

scls19fr
Active User
Active User
Posts: 15
Joined: Fri Mar 13, 2009 1:14 pm

Re: USB TC-08 in python

Post by scls19fr »

Hello,

Here is some Python code to use Pico TC-08 USB with DLL in Python using usb_tc08_get_single.
tc08usb.py
(3.88 KiB) Downloaded 2279 times
sample_tc08usb.py
(360 Bytes) Downloaded 2013 times
TC08USB class should be improve to support other methods.

Kind regards
Last edited by scls19fr on Mon Jun 15, 2015 11:18 am, edited 2 times in total.

Martyn
Site Admin
Site Admin
Posts: 4483
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: USB TC-08 in python

Post by Martyn »

Thank you for uploading your code.

I have added .py as an allowable extension
Martyn
Technical Support Manager

Anonymoose
Newbie
Posts: 0
Joined: Thu Sep 07, 2017 4:18 pm

Re: USB TC-08 in python

Post by Anonymoose »

For MacOS:

Install picoscope6. In the Applications right click on Picoscope6>Show Package Contents. Find the driver at Contents>Resources>lib>libusbtc08.2.dylib.

You can interface with the tc08 library via the python library ctypes.

Code: Select all

import ctypes as C

tc08=C.CDLL("libusbtc08.2.dylib")

temp_array_obj=C.c_float*9
overflow_obj=C.c_int16
chan_obj=C.c_int16
tc_obj=C.c_char
units_obj=C.c_int16

temp_array=temp_array_obj()
overflow=overflow_obj()
chan=chan_obj()
tc=tc_obj()
units=units_obj()

# Plug a type J thermocouple into channel 4 on the tc08

chan.value=4
tc.value='J'
units.value=0

# Find the tc08
handle=tc08.usb_tc08_open_unit()

# Initialize the channel
tc08.usb_tc08_set_channel(handle,chan,tc)

# Make a single measurement
tc08.usb_tc08_get_single(handle,temp_array,overflow,units)

print temp_array[4] # temp_array[0] is the cold junction temperature


Hitesh

Re: USB TC-08 in python

Post by Hitesh »

Hi Anonymoose,

Thank you for sharing your code :D

Best wishes,

colinb
Newbie
Posts: 0
Joined: Thu Dec 14, 2017 7:51 am

Re: USB TC-08 in python

Post by colinb »

Hello,
I have tried using the code as supplied by Martyn to control a TC-08 temperature logger, but am getting the following error
mydll.usb_tc08_get_single(device, temp.ctypes.data, overflow_flags.ctypes.data, 0)
ctypes.ArgumentError: argument 2: : int too long to convert
I am using Python 3.5.2 | Anaconda 4.2.0 (64-bit) on a windows 10 PC with 64 bit architecture, and the driver I am using is the 64 bit driver usbtc08.dll provided by the SDK PicoSDK 10.6.12 (64-bit)
I have also received the same error when using the code supplied by scls19fr
Any ideas what could be going wrong?
Many thanks for your help :)
Colin

Hitesh

Re: USB TC-08 in python

Post by Hitesh »

Hi colinb,

The temp buffer should be an array of 9 floating point numbers. Please check the data type used for this array.

If you are still having trouble, please post your code here. You may also wish to refer to some of the Customer examples that we have linked to from our GitHub repository.

Regards,

colinb
Newbie
Posts: 0
Joined: Thu Dec 14, 2017 7:51 am

Re: USB TC-08 in python

Post by colinb »

Hello Hitesh,
Thank you for your kind reply and help.

The code I am using is shown here

Code: Select all

import ctypes
import numpy as np
from ctypes import *
mydll = ctypes.windll.LoadLibrary('usbtc08.dll')
device = mydll.usb_tc08_open_unit()
mydll.usb_tc08_set_mains(device,50)

temp = np.zeros( (9,), dtype=np.float32)
overflow_flags = np.zeros( (1,), dtype=np.int16)
mydll.usb_tc08_set_channel(device, 0, 0 )
tc_type=ord('K')
for i in range(1,3):
    mydll.usb_tc08_set_channel(device,i,tc_type)
print(temp.ctypes.data)
mydll.usb_tc08_get_single( device, temp.ctypes.data, overflow_flags.ctypes.data, 0)
mydll.usb_tc08_close_unit(device)
print(temp[0],temp[1],temp[2])
The datatype used for the temp buffer is indeed an array of of 9 floating point numbers. And if I ask to print temp i get [ 0. 0. 0. 0. 0. 0. 0. 0. 0.].
However if I probe the query temp.ctypes.data, the the repsonse I get is 30910073648. I suspect this may be the issue but do not know what to do about it.
Best regards,
Colin

Hitesh

Re: USB TC-08 in python

Post by Hitesh »

Hi Colin,

Have you tried running the application with the 32-bit usbtc08.dll? Is the PicoLog application also installed on your PC?

Regards,

colinb
Newbie
Posts: 0
Joined: Thu Dec 14, 2017 7:51 am

Re: USB TC-08 in python

Post by colinb »

Hello Hitesh,
Thank you for your reply.
When I run with the 32 bit driver (and the picolog application installed) I get the following error

Traceback (most recent call last):
File "temp.py", line 4, in
mydll = ctypes.windll.LoadLibrary('usbtc08.dll')
File "C:\Users\colinb\AppData\Local\Continuum\Anaconda3\lib\ctypes\__init__.py", line 425, in LoadLibrary
return self._dlltype(name)
File "C:\Users\colinb\AppData\Local\Continuum\Anaconda3\lib\ctypes\__init__.py", line 347, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application

Thank you again for your help.
Colin

Hitesh

Re: USB TC-08 in python

Post by Hitesh »

Hi Colin,

Did you use a 32-bit Python environment or a 64-bit Python environment for this test?

Regards,

Post Reply