.net - Programmatically Automating Getting the Correct COM Port, based on the Serial Device, through C# -


i trying set program automatically detect com port connected device, end, have done far:

i com ports using

string[] comports = serialport.getportnames(); 

once have available com ports pc, create serialport instance inside foreach loop open , send string command through each available com port. device attached 1 of com ports respond string command, catch through serialport.datareceived event.

foreach(string port in comports) {     serialport sp = new serialport(port, 9600, parity.none, 8, stopbits.one);     sp.handshake = handshake.none;      sp.open();     sp.write("<id01>\r\n"); // command wakes device.  device sends string "<id01>s" in 's' code success. } 

this works good, success string fine. reason want program automatically recognize com port device set on or not setup on. problem time datareceived event has returned success string program has gone past point me because main thread program running on seems have progressed on ahead. thought maybe pause main thread doing thread.sleep(10000); seemed paused serialport it. serialport.datareceived event still passed data after main thread had gone by.

so question is: there way pause main thread allow datareceived information pass can process code based on whether or not device attached specific com port?

if not have suggestions how might able accomplish this?

roadmaster

your problem you're using asynchronous read method, need synchronous one.

this approximately same thing, on single thread:

foreach(serialport port in serialport.getportnames()) {     serialport sp = new serialport(port, 9600, parity.none, 8, stopbits.one);     sp.handshake = handshake.none;      sp.open();     sp.write("<id01>\r\n");      thread.sleep(250);  // give time respond      string response = sp.readexisting();     if(response == "<id01>s")     {         found = true;         // useful     }  } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -