1

I started example SerialSample, and it works well, but I have a question:

How do I send something to the com port without the use of a button, i.e. directly with the code, for example at application startup or after the occurrence of an event?

Additionally: How to choose a serial on board without a button only when the application is started?

Example from microsoft: SerialSample

1
  • What is SerialSample, and where can I find it? Consider asking one question per... erm... question. The way you have done it now - with two questions in one form - makes it hard for people to answer and to later find good questions with concise answers. – Bex Jan 23 '17 at 7:52
1

I found the solution: - write to serial:

           using (var dataWriter = new DataWriter(serialPort.OutputStream))
            {
                dataWriter.WriteString("what u want to send ");
                await dataWriter.StoreAsync();
                //await dataWriter.FlushAsync();
                dataWriter.DetachStream();
            }

and serial initialization:

public sealed partial class MainPage : Page
{
    private SerialDevice serialPort = null;
    DataWriter dataWriteObject = null;
    DataReader dataReaderObject = null;

    private ObservableCollection<DeviceInformation> listOfDevices;
    private CancellationTokenSource ReadCancellationTokenSource;


    public MainPage()
    {
        this.InitializeComponent();            
        comPortInput.IsEnabled = false;
        sendTextButton.IsEnabled = false;
        listOfDevices = new ObservableCollection<DeviceInformation>();
        ListAvailablePorts();

        InitSerial();
    }
private async void InitSerial()
    {

        string portNameN = "UART0";

        string aqs = SerialDevice.GetDeviceSelector(portNameN);

        var myDevices = await DeviceInformation.FindAllAsync(aqs, null);

        if (myDevices.Count == 0)
        {
            status.Text = "Device not found...";
            return;
        }
        serialPort = await SerialDevice.FromIdAsync(myDevices[0].Id);

        // Configure serial settings
        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;

        // Display configured settings
        status.Text = "Serial port configured successfully: ";
        status.Text += serialPort.BaudRate + "-";
        status.Text += serialPort.DataBits + "-";
        status.Text += serialPort.Parity.ToString() + "-";
        status.Text += serialPort.StopBits;
     }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.