Can't connect to Arduino from C# WIndows host on first try


hi all,

i using c# 2010, windows 7 , arduino nano ch340g (serial usb interface) serial data source.

the arduino configured such when send asterisk respond plus sign. code works , tested using arduino ide serial monitor , putty.

in form load event of windows forms app list of comm ports , traverse list looking 1 corresponding arduino test device. odd behavior experiencing when first connect serial device , run app program not detect arduino. however, if run app again locates without problems.

i got work "polling" comm port twice. first time around fails; second time around succeeds. here working c# code:

code: [select]

private void frmmain_load(object sender, eventargs e)
{

      int icboidx;
      int iloop = 0;

      //get serial ports list.
      list<string> lsports = new list<string>(system.io.ports.serialport.getportnames());

      lsports.sort();
     
      foreach (string sport in lsports){

        //add com id dropdown list, list index.
        icboidx = cboport.items.add(sport);

        //test whether arduino reader connected port. return true if so
        //or false if not.

        iloop = 0;

        //i need loop because unknown reason software not locate
        //the reader on first attempt right after reader has been connected pc.
        //it seems work on second attempt though.
        do
          if (comtest4reader(sport))
          {
            //yes, set selected index item.
            cboport.selectedindex = icboidx;

            lblstatus.text = "reader found on " + sport;
            break;
          }
          else
          {
            iloop++;
            thread.sleep(500);
          }
        while(iloop<2);

        }
}



i found code not work if sleep statement removed or low. gives me suspicion issue time based, unable see how.

the c# code function follows:

code: [select]

    boolean comtest4reader(string scomport)
    {
      //routine tests see whether com port corresponds arduino reader.
     
      //use local serial port object.
      serialport serrdr = new serialport(scomport, 9600);

      try
      {

        if (!serrdr.isopen)
        {
          string sretmsg = "";
          serrdr.readtimeout = 1000;

          //bad serrdr.dtrenable = true;

          serrdr.open();

          serrdr.write("*");
          thread.sleep(1000);

          if (serrdr.bytestoread > 0)
          {
            byte[] buffer = new byte[3];

            // there no accurate method checking how many bytes read
            // unless check return read method.
            int ibytesread = serrdr.read(buffer, 0, buffer.length);

            sretmsg = system.text.encoding.default.getstring(buffer);
          }

          serrdr.close();

          return sretmsg.contains("+");

        }
        else
        {
          //port open, can't test.

          return false;
        }
      }
      catch (exception ex)
      {
        messagebox.show(ex.message);
        return false;
      }
    }


i can't seem figure out effect above function has on serial port leaving functional on second run. before putting in loop run app, let fail, exit , run again, succeeding on second try. seems initializes serial port in way not able determine. testing different arduino discard possibility of being ch340g serial/usb interface. can test on different computer.

i aware of issue surrounding usb/serial interface chip, far have been able make arduino (technically called dccduino) work ardiuno ide , aside odd glitch app works fine.

has 1 experienced behavior? suggestions on sort of initialization procedure can follow each comm port? remove loop performs each test twice slows down start of app. , orientation. saga



what happens if close arduino serial port, send message visual studio, , open arduino com port. see anything?


Arduino Forum > Using Arduino > Programming Questions > Can't connect to Arduino from C# WIndows host on first try


arduino

Comments