c# - Waiting for Serial Port response between command calls -


i working electrometer using rs-232(serial) communication. accepts 1 command @ time , sends response indicating if command completed or not.

for demonstration consider following method...

    private void setdevice()     {         sc.setbias();         sc.setrange();         sc.setcollectiontime();         sc.setid();     } 

the method signature sc.setid() follows.

    public void setid()     {         comport.write("*idn?");     } 

the above gives idea of other individual command calls like.

since datareceived event indicates raised on secondary thread how should proceed waiting response between each command call? presently after sc.setbias() called sc.setrange() fires without regards if previous command executed successfully.

i can appreciate starting datareceivedevent in new thread prevents ui locking in case when critical response gets paired appropriate command call i'm not sure of best available option , appreciate guidance in research. far i'm considering getting handle current ui thread , using thread.sleep(sleeptime) without knowing how reliable response time of electrometer best practice? , not need called after every command sent?

if need is:

  1. send command
  2. wait response
  3. send next command

then can use semaphore

it works following codes.

semaphore _sync = new semaphore(1, 1); serialport _port; ...  void send(...){     _sync.waitone()     _port.send(...); }  void _port_datareceived(...){     _sync.release(); } 

take try!


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -