Giter Site home page Giter Site logo

Comments (1)

happypanda94 avatar happypanda94 commented on September 16, 2024

import time
import os

from pythonnet import load ### Without this, the *.dll file was not loading due to belonging to a lower .NET version
load("coreclr")

import clr
import ctypes
import numpy as np
import matplotlib.pyplot as plt

from System import *
clr.AddReference('System.Collections')
from System.Collections.Generic import List

from clr_array_to_numpy import asNumpyArray

clr.AddReference(os.getcwd() + '\..\..\McsUsbNet\x64\\McsUsbNet.dll')
from Mcs.Usb import CMcsUsbListNet
from Mcs.Usb import DeviceEnumNet

from Mcs.Usb import CMeaDeviceNet
from Mcs.Usb import McsBusTypeEnumNet
from Mcs.Usb import DataModeEnumNet
from Mcs.Usb import SampleSizeNet
from Mcs.Usb import SampleDstSizeNet

#%%
def OnChannelData(x, cbHandle, numSamples):
if dataMode == DataModeEnumNet.Unsigned_16bit:
data, frames_ret = device.ChannelBlock.ReadFramesUI16(0, 0, callbackThreshold, Int32(0));
np_data = asNumpyArray(data, ctypes.c_uint16)
print(".Net numSamples: %d frames_ret: %d size: %d" % (numSamples, frames_ret, len(np_data)))
else: # dataMode == DataModeEnumNet.Signed_32bit
data, frames_ret = device.ChannelBlock.ReadFramesI32(0, 0, callbackThreshold, Int32(0));
np_data = asNumpyArray(data, ctypes.c_int32)
print(".Net numSamples: %d frames_ret: %d size: %d" % (numSamples, frames_ret, len(np_data)))

if len(np_data)<1000:
    return

plt.figure()
plt.plot(np_data)

def OnError(msg, info):
print(msg, info)

#%%
deviceList = CMcsUsbListNet(DeviceEnumNet.MCS_DEVICE_USB)
DataModeToSampleSizeDict = {
DataModeEnumNet.Unsigned_16bit : SampleSizeNet.SampleSize16Unsigned,
DataModeEnumNet.Signed_32bit : SampleSizeNet.SampleSize32Signed
}

DataModeToSampleDstSizeDict = {
DataModeEnumNet.Unsigned_16bit: SampleDstSizeNet.SampleDstSize16,
DataModeEnumNet.Signed_32bit: SampleDstSizeNet.SampleDstSize32
}

#%%
print("found %d devices" % (deviceList.Count))

for i in range(deviceList.Count):
listEntry = deviceList.GetUsbListEntry(i)
print("Device: %s Serial: %s" % (listEntry.DeviceName,listEntry.SerialNumber))

#%%
dataMode = DataModeEnumNet.Unsigned_16bit;
#dataMode = DataModeEnumNet.Signed_32bit;

device = CMeaDeviceNet(McsBusTypeEnumNet.MCS_USB_BUS);

device.ChannelDataEvent += OnChannelData

device.ErrorEvent += OnError

#%%
device.Connect(deviceList.GetUsbListEntry(0))

Samplingrate = 10000;
device.SetSamplerate(Samplingrate, 1, 0);

#%%
miliGain = device.GetGain();
voltageRanges = device.HWInfo.GetAvailableVoltageRangesInMicroVoltAndStringsInMilliVolt(miliGain);
for i in range(0, len(voltageRanges)):
print("(" + str(i) + ") " + voltageRanges[i].VoltageRangeDisplayStringMilliVolt);

#%%
device.SetVoltageRangeByIndex(0, 0);

device.SetDataMode(dataMode, 0)
device.SetNumberOfChannels(1) ### Here the number of channels is changed from 2 to 1
device.EnableDigitalIn(Boolean(False), UInt32(0))
device.EnableChecksum(False, 0) ### For now, we disabled the acquisition for checksum channels
block = device.GetChannelsInBlock(0);
print("Channels in Block: ", block)
callbackThreshold = Samplingrate // 10

#%%
if dataMode == DataModeEnumNet.Unsigned_16bit:
mChannels = device.GetChannelsInBlock(0)
else: # dataMode == DataModeEnumNet.Signed_32bit
mChannels = device.GetChannelsInBlock(0) // 2;
print("Number of Channels: ", mChannels)
device.ChannelBlock.SetSelectedChannels(mChannels, callbackThreshold * 10, callbackThreshold, DataModeToSampleSizeDict[dataMode], DataModeToSampleDstSizeDict[dataMode], block)

analogChannels,digitalChannels,checksumChannels,timestampChannels,mChannels=0,0,0,0,0
analogChannels,digitalChannels,checksumChannels,timestampChannels,mChannels=device.GetChannelLayout(analogChannels,digitalChannels,checksumChannels,
timestampChannels,mChannels,0)
print(analogChannels,digitalChannels,checksumChannels,timestampChannels,mChannels)

#%%
device.StartDacq()

time.sleep(0.99)

The code below is copied from the MATLAB example and converted to suit Python

x=0
while True:
for i in range(1):
while device.ChannelBlock.AvailFrames(i, -1)<1000:
time.sleep(0.01)
number = device.ChannelBlock.AvailFrames(i, -1)
print('Number: ',number)
#wait until at least 5000 samples are available
if number >= 1000:
print('Bingo')
#read 5000 samples
data, frames_ret = device.ChannelBlock.ReadFramesUI16(i, 0, 1000, Int32(0));

        if i == 0:
           #Because the data is acquired in 16 Bit unsigned format, we need to subtract 2^15 to center it around Zero.
           y = asNumpyArray(data, ctypes.c_uint16)
           # mask = bitshift(1, 15); # Create a mask with the 16th bit set to 1
           # y = bitxor(y, mask); # XOR the variable with the mask
           y = y-32768;
           plt.figure()
           plt.plot(y[0:100]);

time.sleep(0.1);
x = x + 1;
print("x: ",x)
if (x == 5):
    break # stop after 500 data packets (~ 5 seconds)

device.StopDacq()

#%%
device.Disconnect()

from mcsusbnet_examples.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.