Alex Albu
Alex Albu

Reputation: 733

Sending commands to a serial device in UWP

I need to send a command to a laser sensor. It is connected to USB via an RS232 to USB adapter.

It works using the "old" SerialPort class, meaning that the laser receives the command and turns on (I can see the beam for 3 seconds, and then it dissapears, as expected). The code is this:

private void OpenAndCloseLaser()
        {
            serialPort = new SerialPort();
            serialPort.PortName = "COM1";
            serialPort.BaudRate = 19200;
            serialPort.DataBits = 7;
            serialPort.StopBits = StopBits.One;
            serialPort.Parity = Parity.Even;

            serialPort.Open();

            //initialization command
            serialPort.WriteLine("s0c");

            //open laser
            serialPort.WriteLine("s0o");

            Thread.Sleep(3000);
            //shut down laser
            serialPort.WriteLine("s0p");

            serialPort.Close();
        }

I tried to replicate the same functionality using the SerialDevice class based on this tutorial.

The code is this:

First I find the device (and it works, I get "COM1" as PortName)

private async Task GetDeviceAsync()
        {
            string aqs = SerialDevice.GetDeviceSelector();

            var dis = await DeviceInformation.FindAllAsync(aqs);

            device = await SerialDevice.FromIdAsync(dis[0].Id);

            device.BaudRate = 19200;
            device.DataBits = 7;
            device.StopBits = SerialStopBitCount.One;
            device.Parity = SerialParity.Even;
            device.ReadTimeout = TimeSpan.FromMilliseconds(2000);
            device.WriteTimeout = TimeSpan.FromMilliseconds(2000);

            device.ErrorReceived += Device_ErrorReceived;
        }

This is what I am using to send commands to it:

 private async Task WriteCommand(string command)
        {

            var dataWriter = new DataWriter(device.OutputStream);

            dataWriter.WriteString(command);
            await dataWriter.StoreAsync();

            dataWriter.DetachStream();
            dataWriter = null;

        }

And this is how I send the actual commands:

private async Task OpenAndCloseLaser()
        {
            //initialize
            await WriteCommand(INIT_COMMAND);
            // open laser
            await WriteCommand("s0o");

            await Task.Delay(3000);
            //shutdown laser
            await WriteCommand("s0p");
        }

When using the SerialDevice class and the DataWriter it seems that no commands are sent... the laser is not turning on and off and I am not getting any errors. Any ideas?

Upvotes: 1

Views: 1352

Answers (1)

Alex Albu
Alex Albu

Reputation: 733

The problem here was that I did not send a \r\n termination with the command.The "old" .Net Framework with the SerialPort class was probably doing this by default (the method is called WriteLine).

So now it works with this:

device.IsDataTerminalReadyEnabled = true;
            device.IsRequestToSendEnabled = true;

            var dataWriter = new DataWriter(device.OutputStream);

            dataWriter.WriteString(command + "\r\n");

            await dataWriter.StoreAsync();

            dataWriter.DetachStream();
            dataWriter = null;

And with this:

device.IsDataTerminalReadyEnabled = true;
        device.IsRequestToSendEnabled = true;

        var dataWriter = new DataWriter(device.OutputStream);

        byte[] message = System.Text.Encoding.ASCII.GetBytes(command + "\r\n");
        dataWriter.WriteBytes(message);

        await dataWriter.StoreAsync();

        dataWriter.DetachStream();
        dataWriter = null;

Upvotes: 1

Related Questions