2014-08-29 102 views
0

我有Arduino的烏諾R3與無線原盾,我想8不通過藍牙在Windows Phone的接收來自Arduino的多個數據8

我使用從例如底座與Windows Phone的溝通Windows_Phone_8_communicating_with_Arduino_using_Bluetooth

這是一個異步接收方法:

private async void ReceiveMessages(object sender, DoWorkEventArgs e) 
    { 
     try 
     { 
      while (true) 
      { 

       // Read first byte (length of the subsequent message, 255 or less). 
       uint sizeFieldCount = await dataReader.LoadAsync(1); 
       if (sizeFieldCount != 1) 
       { 
        // The underlying socket was closed before we were able to read the whole data. 
        return; 
       } 

       // Read the message. 
       uint messageLength = dataReader.ReadByte(); 
       uint actualMessageLength = await dataReader.LoadAsync(messageLength); 
       if (messageLength != actualMessageLength) 
       { 
        // The underlying socket was closed before we were able to read the whole data. 
        return; 
       } 
       // Read the message and process it. 
       string message = dataReader.ReadString(actualMessageLength); 
       MessageReceived(message); 
      } 
     } 
     catch (Exception ex) 
     { 
      Debug.WriteLine(ex.Message); 
     } 

    } 

我有一些方法發送我回來信息從阿爾杜伊諾,(SendCarId,SendRideId)

如果我嘗試單獨發送這些命令,它會發送給我(來自Arduino)正確的數據。

但是,當我一個接一個地發送請求時,它只響應第一個而不是第二個。 (它從來沒有得到方法ReceiveMessages第二次)

任何人都有想法是什麼問題?

回答

0

我發現,如果我終止連接時,我得到的答案, 然後我就可以初始化新的連接(也許不是最有效的解決方案,但它的工作)

public void Terminate() 
    { 
     connected = false; 

     dataReadWorker.DoWork -= new DoWorkEventHandler(ReceiveMessages); 

     if (socket != null) 
     { 
      socket.Dispose(); 
      socket = null; 
     } 
     if (dataReadWorker != null) 
     { 
      dataReadWorker.CancelAsync(); 

     } 
    } 


public void Initialize() 
    { 
     socket = new StreamSocket(); 
     dataReadWorker = new BackgroundWorker(); 
     dataReadWorker.WorkerSupportsCancellation = true; 
     dataReadWorker.DoWork += new DoWorkEventHandler(ReceiveMessages); 
    }