Hello to all. I do the project "Arduino + windows 10 app". My MK connects to the application via WiFi. I want to write an application for communication MK program. MK acts as a server, the program - the client. I manage to connect with the device, transfer data, but it cannot accept. As far as I understand, the input stream is set incorrectly. P.S. I already considered this example, did not help ( https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/StreamSocket ). Maybe someone knows, has a similar program, like eta
My program:
public sealed partial class MainPage : Page { StreamSocket _socket; HostName _hostName; DataWriter writer; public MainPage() { this.InitializeComponent(); _socket = new StreamSocket(); } private async void connectButton_Click(object sender, RoutedEventArgs e) { #region _check_parameters if (String.IsNullOrEmpty(hostName_Box.Text)) { _statusBar.Text += "[ERROR] Set the Host Name !!!\n"; return; } if (String.IsNullOrEmpty(serviceName_Box.Text)) { _statusBar.Text += "[ERROR] Set the Service Name !!!\n"; return; } try { _hostName = new HostName(hostName_Box.Text); } catch (Exception) { _statusBar.Text += "[ERROR] Invalid Host Name !!!\n"; return; } #endregion // If necessary, tweak the socket's control options before carrying out the connect operation. // Refer to the StreamSocketControl class' MSDN documentation for the full list of control options. _socket.Control.KeepAlive = false; try { _statusBar.Text += "Connecting...\n"; await _socket.ConnectAsync(_hostName, serviceName_Box.Text); _statusBar.Text += "Connected\n"; // go to send mode connectButton.Content = "Send"; connectButton.Click -= connectButton_Click; connectButton.Click += send_Data; hostName_Box.PlaceholderText = "data to send"; hostName_Box.Text = ""; writer = new DataWriter(_socket.OutputStream); } catch(Exception exception) { // If this is an unknown status it means that the error is fatal and retry will likely fail. if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown) { throw; } _statusBar.Text += "[ERROR] Connect failed with error:\n" + exception.Message + "\n"; } } private async void read_data() { DataReader dataReader = new DataReader(_socket.InputStream); dataReader.InputStreamOptions = InputStreamOptions.Partial; try { await dataReader.LoadAsync(5); string s; uint length; length = dataReader.ReadUInt32(); s = dataReader.ReadString(length); _statusBar.Text += "Read successful\n" + s + "\n"; } catch (Exception exception) { _statusBar.Text += "[ERROR] Fail to load dada !!!\n" + exception.Message + "\n"; } } private async void send_Data(object sender, RoutedEventArgs e) { if (String.IsNullOrEmpty(hostName_Box.Text)) { _statusBar.Text += "Write anything\n"; return; } if(hostName_Box.Text == "read") { try { hostName_Box.Text = ""; read_data(); return; } catch (Exception ex) { _statusBar.Text += "[ERROR] Reading data failed:\n" + ex.Message + "\n"; } } writer.WriteUInt32(writer.MeasureString(hostName_Box.Text)); writer.WriteString(hostName_Box.Text); try { await writer.StoreAsync(); _statusBar.Text += "\"" + hostName_Box.Text + "\" sent successfully\n"; hostName_Box.Text = ""; } catch(Exception ex) { if (SocketError.GetStatus(ex.HResult) == SocketErrorStatus.Unknown) { throw; } _statusBar.Text += "[ERROR] Send failed with error:\n" + ex.Message + "\n"; } } } <Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="100" /> </Grid.RowDefinitions> <ScrollViewer> <TextBlock Name="_statusBar" FontSize="24" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Visible"/> </ScrollViewer> <Grid Grid.Row="1"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Button Name="connectButton" Content="Connect" FontSize="24" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="connectButton_Click" BorderThickness="1,1,0,0"/> <TextBox Name="hostName_Box" PlaceholderText="Host Name" FontSize="24" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="0,1,1,0"/> <TextBox Name="serviceName_Box" PlaceholderText="Service Name" FontSize="24" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="1,1,1,0"/> </Grid> </Grid>
StreamSocket
- to synchronous, see if the problem disappears. - Alexis