Comments (1)
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)
- Python does not support SetElectrodeMode HOT 12
- C# MEA demo app throws 'Operation not supported' on StartDacq. HOT 2
- Possible Expansion of STG channels? HOT 1
- Timing the Stimulation Code HOT 7
- Stop recording after a few seconds HOT 5
- device.StartDacq() Operation not supported in Matlab HOT 6
- FIFO queue seems not to be supplemented in 5.1.24 vs 3.2.45 HOT 3
- setup hardware filtering on MEA2100 using matlab HOT 8
- MATLAB HOT 9
- How to access streamed data HOT 2
- CStg200xDownloadBasicNet is there a way to get an event/timestamp out upon each stimulation data point? HOT 1
- How to use python to control MEA2100_256 to achieve electrical stimulation of custom electrodes for brain organoids HOT 21
- How to stimulate using ElectrodeModeEnumNet.emAutomatic on internal stimulator of MEA2100 IFB-Lite HOT 9
- Error using run_mea HOT 7
- About setting up the stimulation prior to placing cells using the signal generator
- Get timestamp of the stimulation for the multiwell system
- running Recording and stimulation module HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mcsusbnet_examples.