c# - Cannot read data from Arduino bluetooth serial data in WinRT -


i using arduino uno connected bluetooth module. have below code in arduino, listen specific input , glow led.

int led= 13;   char input;    void setup()  {     serial.begin(9600);     pinmode(led, output);      serial.println(">> start<<");     serial.flush(); }    void loop()  {     serial.flush();   if(serial.available()>0)     {       input= serial.read();        if(input=='1')       {         serial.write(1);       serial.println('a');         digitalwrite(led, high);       }       else if(input=='0')       {         serial.println("off");         digitalwrite(led, low);       }       else     {         serial.println("no input");         serial.println(input);       }     }    }   

from windows 8.1 (xaml/c#) application sending data through bluetooth. , works expected. trying read data arduino. have below code in c#.

socket = new streamsocket(); connectaction = socket.connectasync(rfcommservice.connectionhostname, rfcommservice.connectionservicename, socketprotectionlevel.bluetoothencryptionallownullauthentication); await connectaction;//to make cancellable writer = new datawriter(socket.outputstream); reader = new datareader(socket.inputstream); task.run(() => {        listenformessagesasync(); }); 

the listenformessagesasync method supposed keep listening datareader. waiting infinite time , never returns.

    private async task listenformessagesasync()     {         while (reader != null)         {             try             {                 uint sizefieldcount = await reader.loadasync(1);// taskloadlength.getresults();                  if (sizefieldcount != 1)                 {                     // underlying socket closed before able read whole data.                     return;                 }                  // read message.                 uint messagelength = reader.readbyte();                 uint actualmessagelength = await reader.loadasync(messagelength);                 if (messagelength != actualmessagelength)                 {                     // underlying socket closed before able read whole data.                     return;                 }                 // read message , process it.                 string message = reader.readstring(actualmessagelength);             }             catch (exception ex)             {             }         }     } 

what doing wrong here?

you need return after successful readstring , of course need message.


Comments