int i = 0; // int led = 13; void setup() { Serial.begin(9600); // pinMode(led, OUTPUT); // 13- } void loop() { i = i + 1; // String stringOne = "Info from Arduino "; stringOne += i; // Serial.println(stringOne); // char incomingChar; if (Serial.available() > 0) { // incomingChar = Serial.read(); // LED switch (incomingChar) { case '1': digitalWrite(led, HIGH); break; case '0': digitalWrite(led, LOW); break; } } delay(300); }
<StackPanel Orientation="Vertical"> <Label x:Name="lblPortData" FontSize="48" HorizontalAlignment="Center" Margin="0,20,0,0"> </Label> <Button x:Name="btnOne" Click="btnOne_Click" Width="100" Height="30" Margin="0,10,0,0"> 1</Button> <Button x:Name="btnZero" Click="btnZero_Click" Width="100" Height="30" Margin="0,10,0,0"> 0</Button> </StackPanel>
using System.Timers; using System.IO.Ports;
System.Timers.Timer aTimer; SerialPort currentPort; private delegate void updateDelegate(string txt);
bool ArduinoPortFound = false; try { string[] ports = SerialPort.GetPortNames(); foreach (string port in ports) { currentPort = new SerialPort(port, 9600); if (ArduinoDetected()) { ArduinoPortFound = true; break; } else { ArduinoPortFound = false; } } } catch { } if (ArduinoPortFound == false) return; System.Threading.Thread.Sleep(500); // currentPort.BaudRate = 9600; currentPort.DtrEnable = true; currentPort.ReadTimeout= 1000; try { currentPort.Open(); } catch { } aTimer = new System.Timers.Timer(1000); aTimer.Elapsed += OnTimedEvent; aTimer.AutoReset = true; aTimer.Enabled = true;
private bool ArduinoDetected() { try { currentPort.Open(); System.Threading.Thread.Sleep(1000); // , SerialPort string returnMessage = currentPort.ReadLine(); currentPort.Close(); // void loop() Serial.println("Info from Arduino"); if (returnMessage.Contains("Info from Arduino")) { return true; } else { return false; } } catch (Exception e) { return false; } }
private void OnTimedEvent(object sender, ElapsedEventArgs e) { if (!currentPort.IsOpen) return; try // { // currentPort.DiscardInBuffer(); // string strFromPort = currentPort.ReadLine(); lblPortData.Dispatcher.BeginInvoke(new updateDelegate(updateTextBox), strFromPort); } catch { } } private void updateTextBox(string txt) { lblPortData.Content = txt; }
private void Window_Closing(object sender, EventArgs e) { aTimer.Enabled = false; currentPort.Close(); }
private void btnOne_Click(object sender, RoutedEventArgs e) { if (!currentPort.IsOpen) return; currentPort.Write("1"); } private void btnZero_Click(object sender, RoutedEventArgs e) { if (!currentPort.IsOpen) return; currentPort.Write("0"); }
<Capabilities> <Capability Name="internetClient" /> <DeviceCapability Name="serialcommunication"> <Device Id="any"> <Function Type="name:serialPort"/> </Device> </DeviceCapability> </Capabilities>
using Windows.Devices.SerialCommunication; using Windows.Devices.Enumeration; using Windows.Storage.Streams; using System.Threading.Tasks;
string deviceId;
private async void Page_Loaded(object sender, RoutedEventArgs e) { string filt = SerialDevice.GetDeviceSelector("COM3"); DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(filt); if (devices.Any()) { deviceId = devices.First().Id; } }
private async Task Listen() { using (SerialDevice serialPort = await SerialDevice.FromIdAsync(deviceId)) { if (serialPort != null) { serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000); serialPort.BaudRate = 9600; serialPort.Parity = SerialParity.None; serialPort.StopBits = SerialStopBitCount.One; serialPort.DataBits = 8; serialPort.Handshake = SerialHandshake.None; try { using (DataReader dataReaderObject = new DataReader(serialPort.InputStream)) { Task<UInt32> loadAsyncTask; uint ReadBufferLength = 64; dataReaderObject.InputStreamOptions = InputStreamOptions.Partial; loadAsyncTask = dataReaderObject.LoadAsync(ReadBufferLength).AsTask(); UInt32 bytesRead = await loadAsyncTask; if (bytesRead > 0) { txtPortData.Text = dataReaderObject.ReadString(bytesRead); txtStatus.Text = "Read operation completed"; } } } catch (Exception ex) { txtStatus.Text = ex.Message; } } } }
private async Task sendToPort(string sometext) { using (SerialDevice serialPort = await SerialDevice.FromIdAsync(deviceId)) { Task.Delay(1000).Wait(); if ((serialPort != null) && (sometext.Length != 0)) { serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000); serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000); serialPort.BaudRate = 9600; serialPort.Parity = SerialParity.None; serialPort.StopBits = SerialStopBitCount.One; serialPort.DataBits = 8; serialPort.Handshake = SerialHandshake.None; Task.Delay(1000).Wait(); try { using (DataWriter dataWriteObject = new DataWriter(serialPort.OutputStream)) { Task<UInt32> storeAsyncTask; dataWriteObject.WriteString(sometext); storeAsyncTask = dataWriteObject.StoreAsync().AsTask(); UInt32 bytesWritten = await storeAsyncTask; if (bytesWritten > 0) { txtStatus.Text = bytesWritten + " bytes written"; } } } catch (Exception ex) { txtStatus.Text = ex.Message; } } } }
Source: https://habr.com/ru/post/275439/
All Articles