C# Signal Generator Problem

Post your .Net discussions here
Post Reply
Posts: 3
Joined: Sun Sep 14, 2008 12:47 pm
Location: Germany

C# Signal Generator Problem

Post by picouser99 » Sun Sep 14, 2008 1:01 pm

I am using the RedScope 25MHz, which is obviously a PicoScope 2205 internally.
(Hardwareversion: 16 driver version: 2, 0, 5, 7)

Annoying but not critical:
  • - no code examples are installed, neither by installing picoscope 6 nor picoscope 5.20
    - After some searching I manually extracted code sample files from the .cab files of ps 5.20. on the CD.
    - There are NO Code samples for C#, neither on the CD nor on the website, at least I could find them. Why is this language ignored ?

The Signal Generator is NOT working when called in C#. I wrote the necessary calls to the ps2000.dll like this:

Code: Select all

public enum PS2000_WAVE_TYPE { PS2000_SINE, PS2000_SQUARE ,PS2000_TRIANGLE ,PS2000_RAMP_UP ,PS2000_RAMP_DOWN ,PS2000_DC_VOLTAGE};
public enum PS2000_SWEEP_TYPE {PS2000_UP,PS2000_DOWN,PS2000_UPDOWN,PS2000_DOWNUP};
[DllImport("PS2000.dll",EntryPoint = "ps2000_open_unit")]
public static extern short OpenUnit();

[DllImport("PS2000.dll", EntryPoint = "ps2000_set_sig_gen_built_in")]
public static extern short SetSigGen( short handle, long offsetVoltage, ulong pkToPk, PS2000_WAVE_TYPE wavetype, float startFrequency, float stopFrequency, float increment, float dwellTime, PS2000_SWEEP_TYPE sweeptype, ulong sweeps);

[DllImport("PS2000.dll", EntryPoint = "ps2000_close_unit")]
public static extern short CloseUnit(short handle);
and call it in the form like this (example with simple DC output):

Code: Select all

short handle = 1;
label1.Text = SetSigGen(handle, 1000000, 1000000,PS2000_WAVE_TYPE.PS2000_DC_VOLTAGE, 1, 1,0,0,PS2000_SWEEP_TYPE.PS2000_UPDOWN, 0).ToString();
The openunit() obviously works (device clicks and flashes), but SetSigGen() ALWAYS returns 0, which means "no errors", even if I send incorrect handles or data values (too high frequencies etc.).

Could someone please provide me with a short example how to use the built in signal generator within C# correctly ?

Hint: I noticed that the signal generator also does not work when started within picoscope 5.20 software (there its interface is rudimentary anyway...), but works fine from picoscope 6.0.

Posts: 3
Joined: Sun Sep 14, 2008 12:47 pm
Location: Germany

Post by picouser99 » Tue Sep 16, 2008 9:28 am

It would be great if someone could help me with my signal-generator problem, is anyone using this feature within a .NET application sucessfully ?

I guess I am making a mistake in the way I reference the ps2000_set_sig_gen_built_in function, but I can't find the error.

Please tell me if I should quote the whole code here so the problem can be reproduced directly (although the above mentioned lines are all you need anyway... :-) )

Active User
Active User
Posts: 11
Joined: Fri Aug 01, 2008 9:49 am
Location: Wuppertal/Germany

Post by wayoda » Wed Sep 17, 2008 10:16 am

I don't have a PicoScope with a signal generator, but the way you call
looks suspicious:
A long in C# is always 64 bits wide, but the C-library might expect the long arguments only to be 32Bits wide!

You could try to cast your long (ulong) arguments to System.Int32 (or System.UInt32).


Posts: 3
Joined: Sun Sep 14, 2008 12:47 pm
Location: Germany

Post by picouser99 » Wed Sep 17, 2008 8:35 pm

Dear Wayoda, you are absolutely right, the problem was the different "long" meaning. Thanks! :idea:
A comparison of typical types (see e.g. here: MSDN Article) shows that "long" is the only one who makes trouble. Tricky.
I had actually already managed to solve this problem on my own as the project is VERY urgent (and i am very late...).

The pico "programmers guide" only states "long" but not the actual size. This is a bit misleading for someone like me who only writes code occasionally and is not aware of the the internal bitlength.

The relevant hint for me was the post by AnthonyCSheehy in this forum (here) who obviously also stumbled across this problem.
By the way: The function call list for VB.NET in his post here is possibly also wrong in several calls. I noticed some "long" definitions there too...

One more thing: The programmer guide states that "0" will be returned by the ps2000_set_sig_gen_built_in function if everything was OK. After I finally managed to call the function correctly I found that actually "1" is returned for no errors. On the contrary, "0" means that problems have occured (e.g. too high voltages set). Am I doing something wrong ? This discrepancy was also very misleading during the debugging of the above mentioned problem!

@PICOTECH: This "long" problem is obviously a typical problem someone will face when he tries to adopt the picoscopes for .NET. Please update your documents accordingly. So hopefully other people could be spared time consuming debugging and forum waiting. (Or create some decent examples for all languages alltogether..., e.g. the Delphi, LV and VB examples don't even mention the usefull signal generator feature from what I have seen.)

Posts: 0
Joined: Wed Nov 10, 2010 1:12 pm

Re: C# Signal Generator Problem

Post by BoLundgren » Fri Nov 12, 2010 4:33 pm

I am using a PicoScope4226 with PicoScope6 on a Sony VAIO Model PCG-3J1M with a Windows 7 Home OS and it works perfectly.

I have also tried to use the C#-example from the SDK Version r10_1_0_8 using Visual Studio 2010 Professional. The code compiles fine and runs as it should if one rembers to set the processor type to x86. Unfortunately the example does not deal with the signal generator, so I have, like picouser99, tried to add code to import the signal generator functions into Imports class in the ps4000Imports.cs file:

Code: Select all

        public enum SweepType : int

        public enum Wavetype : short

        public enum SigGenTrigType : int

        public enum SigGenTrigSource : int

        [DllImport(_DRIVER_FILENAME, EntryPoint = "ps4000SetSigGenBuiltIn")]
        public static extern short SetSigGenBuiltIn(
            short handle,
            int offsetVoltage,
            uint pkToPk,
            short waveType,
            float startFrequency,
            float stopFrequency,
            float increment,
            float dwellTime,
            Imports.SweepType sweepType,
            short whiteNoise,
            uint shots,
            uint sweeps,
            Imports.SigGenTrigType triggerType,
            Imports.SigGenTrigSource triggerSource,
            short extInThreshold);
The function is called by the following code after OpenUnit and GetDeviceInfo has been called successfully:

Code: Select all

      public short StartGenerator(uint amplitude, float frequency)
          uint ampPP = amplitude;
          short wType = (short)Imports.Wavetype.Square;
          int offsetV = 10;
          float incr = 0f;
          float dwellT = 0f;
          Imports.SweepType swType = Imports.SweepType.Up;
          short whiteN = 0;
          uint shts = 0;
          uint swps = 0;
          Imports.SigGenTrigType trigTp = Imports.SigGenTrigType.Rising;
          Imports.SigGenTrigSource trigSrce = Imports.SigGenTrigSource.None;
          short extInThr = 1000;

          return Imports.SetSigGenBuiltIn(_handle, offsetV, ampPP, wType, frequency, frequency, incr, dwellT,
              swType, whiteN, shts, swps, trigTp, trigSrce, extInThr);
Unfortunately the function return value is 46 (0x2E) PICO_SIGGEN_PARAMETER_ERROR, regardless of the values of amplitude and frequency. At first I had also made the mistake of using long and ulong, but correcting that after reading through the forum topic did not help.

Does anyone have a suggestion to solve the problem?

Other functions like BlockDataHandler runs fine reading the input and delivering sensible data.

The signal generator code in ps4000con.c transferred to C++ works after some type errors in the file have been corrected.

Site Admin
Site Admin
Posts: 169
Joined: Tue Aug 17, 2010 9:00 am
Location: St. Neots

Re: C# Signal Generator Problem

Post by Chris » Sat Nov 13, 2010 10:00 pm

I've tried your code and it works ok for me.

Note the amplitude range is 500mV to 4V

As this parameter is in microvolts, it needs to be
from 500000 to 4000000


Posts: 0
Joined: Wed Nov 10, 2010 1:12 pm

Re: C# Signal Generator Problem

Post by BoLundgren » Sun Nov 14, 2010 9:40 am

Many thanks Chris for the tip. I had made a mistake of using an erronous factor when converting from V to uV.
10^6 should be 1e6 not 10e6. Your fast answer saved my weekend. Thanks!

Post Reply