Test and Measurement Forum

ps2000_open_unit() fails

Post general discussions on using our drivers to write your own software here

ps2000_open_unit() fails

Postby IgorLopez » Sat Jul 25, 2015 4:38 pm


I am writing a very simple application to capture I2C signals and check why my I2C client does not ACK when Master addresses it.
The problem is that my application can't even connect to the PicoScope (I have the 2204A version).
My PC (Ubuntu 14.04 x86_64) does work with the HW with the provided picoscope application (Mono) so it must be something very simple I have forgotten since this is my first attempt at using the PicoScope library.

This is how the code looks like (not finished but shows the problem directly)
Code: Select all
#define _GNU_SOURCE

/* Headers for Linux */
#include <termios.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdint.h>

/* Definition of PS2000 driver routines on Linux */
#include <libps2000-2.1/ps2000.h>

#define __stdcall
#define Sleep(x) usleep(1000*(x))

/* End of Linux-specific definitions */

typedef enum {
   MODEL_NONE = 0,
  MODEL_PS2104 = 2104,
   MODEL_PS2105 = 2105,
   MODEL_PS2202 = 2202,
   MODEL_PS2203 = 2203,
   MODEL_PS2204 = 2204,
   MODEL_PS2205 = 2205

typedef struct

typedef struct
   PS2000_PWQ_CONDITIONS               *   conditions;
   int16_t                                          nConditions;
   PS2000_THRESHOLD_DIRECTION        direction;
   uint32_t                              lower;
   uint32_t                              upper;
   PS2000_PULSE_WIDTH_TYPE               type;

typedef struct
   PS2000_CHANNEL channel;
   float threshold;
   int16_t direction;
   float delay;

typedef struct
   int16_t hysterisis;
   DIRECTIONS directions;
   int16_t nProperties;
   PS2000_TRIGGER_CONDITIONS * conditions;
   PS2000_TRIGGER_CHANNEL_PROPERTIES * channelProperties;
   uint32_t totalSamples;
   int16_t autoStop;
   int16_t triggered;

typedef struct
   SIMPLE simple;
   ADVANCED advanced;

typedef struct {
   int16_t DCcoupled;
   int16_t range;
   int16_t enabled;

typedef struct  {
   int16_t handle;
   MODEL_TYPE model;
  PS2000_RANGE firstRange;
   PS2000_RANGE lastRange;
   int16_t maxTimebase;
   int16_t timebases;
   int16_t noOfChannels;
   int16_t            hasAdvancedTriggering;
   int16_t            hasFastStreaming;
   int16_t            hasEts;
   int16_t            hasSignalGenerator;

UNIT_MODEL unitOpened;

int main(int argc, char **argv)
   int32_t ok;
   PS2000_CHANNEL chan;
   for (chan=PS2000_CHANNEL_A; chan <= PS2000_CHANNEL_B; chan++) {
      unitOpened.channelSettings[chan].enabled = TRUE;
      unitOpened.channelSettings[chan].DCcoupled = TRUE;
      unitOpened.channelSettings[chan].range = PS2000_5V;
   unitOpened.trigger.advanced.nProperties = 1;
   unitOpened.trigger.advanced.conditions = malloc (sizeof (PS2000_TRIGGER_CONDITIONS) * unitOpened.trigger.advanced.nProperties);
   unitOpened.trigger.advanced.conditions->channelA = PS2000_CONDITION_DONT_CARE;
   unitOpened.trigger.advanced.conditions->channelB = PS2000_CONDITION_TRUE;
   unitOpened.trigger.advanced.conditions->pulseWidthQualifier = PS2000_CONDITION_DONT_CARE;
   unitOpened.trigger.advanced.directions.channelA = PS2000_ADV_FALLING;
   unitOpened.trigger.advanced.directions.channelB = PS2000_ADV_FALLING;
   unitOpened.trigger.advanced.directions.ext = PS2000_ADV_FALLING;
   unitOpened.trigger.advanced.channelProperties = malloc (sizeof (PS2000_TRIGGER_CHANNEL_PROPERTIES) * unitOpened.trigger.advanced.nProperties);
   unitOpened.trigger.advanced.channelProperties->channel = (int16_t) PS2000_CHANNEL_B;
   unitOpened.trigger.advanced.channelProperties->thresholdMajor = 1500;
   unitOpened.trigger.advanced.channelProperties->thresholdMinor = 0;
   unitOpened.trigger.advanced.channelProperties->hysteresis = (int16_t) 4096;
   unitOpened.trigger.advanced.channelProperties->thresholdMode = PS2000_LEVEL;
   unitOpened.handle = ps2000_open_unit();
   if (0 < unitOpened.handle) {
      ok = ps2000_set_ets(unitOpened.handle, PS2000_ETS_OFF, 0, 0);
      if (0 == ok) {
         fprintf(stdout, "function call ps2000_set_ets failed\n");
      } else {
         fprintf(stdout, "function call ps2000_set_ets sucessfuly done\n");
      for (chan=PS2000_CHANNEL_A; chan <= PS2000_CHANNEL_B; chan++) {
         ok = ps2000_set_channel(unitOpened.handle, chan, unitOpened.channelSettings[chan].enabled, unitOpened.channelSettings[chan].DCcoupled, unitOpened.channelSettings[chan].range);
         if (0 == ok) {
            fprintf(stdout, "function call ps2000_set_channel for channel %d failed\n", chan);
      ok = ps2000SetAdvTriggerChannelConditions(unitOpened.handle, unitOpened.trigger.advanced.conditions, unitOpened.trigger.advanced.nProperties);
      if (0 == ok) {
         fprintf(stdout, "function call: ps2000SetAdvTriggerChannelConditions failed\n");
      ok = ps2000SetAdvTriggerChannelDirections(unitOpened.handle,
      if (0 == ok) {
         fprintf(stdout, "function call: ps2000SetAdvTriggerChannelDirections failed\n");
      ok = ps2000SetAdvTriggerChannelProperties(unitOpened.handle, unitOpened.trigger.advanced.channelProperties, unitOpened.trigger.advanced.nProperties, 0);
      if (0 == ok) {
         fprintf(stdout, "function call: ps2000SetAdvTriggerChannelProperties failed\n");
      ok = ps2000_run_streaming_ns(unitOpened.handle, 160, PS2000_NS, 700, TRUE, 10, 15000);
      if (0 == ok) {
         fprintf(stdout, "function call: ps2000_run_streaming_ns failed\n");
      // Close the unit
   } else {
      fprintf(stdout, "function call ps2000_open_unit failed\n");
it fails on the row: unitOpened.handle = ps2000_open_unit(); since it writes: function call ps2000_open_unit failed in the console.
The make file is as:
Code: Select all
CC = gcc
CXX = g++
BINDIR = bin
SRCDIR = src
OBJDIR = obj

EXENAME = collect_data

CFLAGS = -Wall -pedantic -std=c99 -O2
CXXFLAGS = -Wall -O2

INCL = -I/opt/picoscope/include
LIBS = -L/opt/picoscope/lib -lps2000
OBJS = $(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(wildcard $(SRCDIR)/*.c))
OBJS += $(patsubst $(SRCDIR)/%.cpp, $(OBJDIR)/%.o, $(wildcard $(SRCDIR)/*.cpp))


   @mkdir -p $(BINDIR)
   @echo "* LINKING $^ TO $@"
   @$(CC) -o $@ $^ $(LFLAGS) $(LIBS)
   @echo "[DONE]"

$(OBJDIR)/%.o: $(SRCDIR)/%.c
   @mkdir -p $(OBJDIR)
   @echo "* COMPILING $< TO $@"
   @$(CC) -c -o $@ $< $(INCL) $(CFLAGS)

$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
   @mkdir -p $(OBJDIR)
   @echo "* COMPILING $< TO $@"
   @$(CXX) -c -o $@ $< $(INCL) $(CXXFLAGS)

   @rm -f $(BINDIR)/* $(OBJDIR)/*
   @echo "[DONE]"

.PHONY: all clean
so basically from console:
$ make all
* COMPILING src/CollectData.c TO obj/CollectData.o
* LINKING obj/CollectData.o TO bin/collect_data
$ ./bin/collect_data
function call ps2000_open_unit failed
Posts: 0
Joined: Sat Jul 25, 2015 2:58 pm

Re: ps2000_open_unit() fails

Postby IgorLopez » Sun Jul 26, 2015 6:54 am

It was a very easy problem.
Since I can run the provided picoscope application I thought my PC was properly setup but when checking with usbtest I get this:
Code: Select all
Pico USB device found: /dev/bus/usb/002/012

- It belongs to root (which is not you) who has permissions rw-
- The members of group pico (which you are not in) have permissions rw-
- Everyone else (this is you) has permissions r--
- You CANNOT write to this device and so will NOT be able to use it.
   Please see the driver installation instructions for assistance in
   resolving this problem
After fixing this my program did get the handle to the PicoScope but failed instead on
Code: Select all
ok = ps2000_set_ets(unitOpened.handle, PS2000_ETS_OFF, 0, 0);
ok = ps2000_run_streaming_ns(unitOpened.handle, 160, PS2000_NS, 700, TRUE, 10, 15000);
I need to dig furter into the documentation.
Posts: 0
Joined: Sat Jul 25, 2015 2:58 pm

Re: ps2000_open_unit() fails

Postby Hitesh » Mon Aug 03, 2015 2:18 pm

Hi Igor,

What error messages are you getting?


Technical Specialist
Pico Technology
Site Admin
Site Admin
Posts: 2000
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Return to General Software Discussions

Who is online

Users browsing this forum: No registered users and 1 guest