c# - Adlink PCI-7250 eventcallback -


i wrote simple c# eventcallback pci-7250 (data acquisition card) when of digital inputs go high. here code:

public delegate void readdelegate(uint value) public void form1_load(object sender, eventargs e)  {      m_dev = dask.register_card(dask.pci_7250,0);      readdelegate readnow = new readdelegate(functiontocall)      dask.di_eventcallback((ushort)m_dev,1,(short)dask.dbevent,readnow)  }  private void functiontocall(uint int_value)  {  messagebox.show(int_value)  } 

when run keep"s throwing random numbers during runtime , crashes. believe has eventtype (dask.dbevent). went through manual nothing more mentioned dask.dbevent.

kindly please advise.

since device doesn't have support callbacks in driver, adapt driver's api synchronous calls callbacks polling device in background thread.

first, create class polls device physical event you're interested in responding to. then, in gui code, put polling work in background , respond callback in main thread.

i'm not familiar adlink driver, i'll summarize design approach , sketch pseudocode isn't threadsafe. naive approach using tasks, update use continuations or async/await. or, if need more 1 responder, make callback raise event other classes can subscribe to.

polling class

public class edgedetector {   public delegate void onrisingedgedetected(uint currentvalue, uint linesthatasserted);   private bool m_shouldpoll;    private void pollforrisingedge(pci_7250 device, onrisingedgedetected onrisingedgedetected)   {     while (m_shouldpoll)     {       // optional: sleep avoid consuming cpu        uint newportvalue = device.readalldigitallines();       uint changedlines = m_currentportvalue ^ newportvalue;       uint risingedges = newportvalue & changedlines;       m_currentportvalue = newportvalue;        if (risingedges != 0)       {         onrisingedgedetected(currentvalue: newportvalue,                              linesthatasserted: risingedges);       }   }    public void start(pci_7250 device, onrisingedgedetected onrisingedgedetected)   {       m_shouldpoll = true;       pollforrisingedge(device, onrisingedgedetected);   }    public void stop()   {       m_shouldpoll = false;   } } 

winform class

private void initialize() {   m_dev = dask.register_card(dask.pci_7250, 0);   m_mainthreadscheduler = taskscheduler.fromcurrentsynchronizationcontext(); }  private void startedgedetection() {   m_edgedetectiontask = task.factory.startnew( () =>     {       m_edgedetector.start(device: m_dev, onrisingedgedetected: rescheduleonmainthread);     }); }  private rescheduleonmainthread(uint currentvalue, uint linesthatasserted) {   m_onedgedetectiontask = task.factory.startnew(     action: () =>       {         messagebox.show(currentvalue);       },     cancellationtoken: null,     creationoptions: taskcreationoptions.none,     scheduler: m_mainthreadscheduler); }  private void cleanup() {   m_edgedetector.stop();   m_edgedetectiontask.wait();   m_onedgedetectiontask.wait(); }  public void form1_load(object sender, eventargs e) {   initialize();   startedgedetection(); }  public void form1_closed(object sender, eventargs e) {   cleanup(); } 

Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -