none
Help disconnecting serial port RRS feed

  • Question

  • Hello,

    I need some help in disconnecting a serial port.  I can connect to it ok, but my disconnect button does not do anything.  I am using a Bluetooth serial device to indicate connection to the serial port.  The led changes color and shows it is connected to the COM port.  I cannot disconnect for some reason.  Here is the code:

    ……

    using Windows.Devices.SerialCommunication;

    using Windows.Devices.Enumeration;

    using Windows.Storage.Streams;

     

     

     

    namespace BluetoothPlain

    {

        public sealed partial class MainPage : Page

        {

            public SerialDevice serialDevice;

     

            public MainPage()

            {

                this.InitializeComponent();

            }

     

            public async void connect_Click(object sender, RoutedEventArgs e)

            {

                string comboChoice = Combo.SelectionBoxItem.ToString();

                string selector = SerialDevice.GetDeviceSelector(comboChoice);

                DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selector);

                if(devices.Count >0)

                {

                    DeviceInformation deviceInfo = devices[0];

                    SerialDevice serialDevice = await SerialDevice.FromIdAsync(deviceInfo.Id);

                    serialDevice.BaudRate = 115200;

                    serialDevice.DataBits = 8;

                    serialDevice.StopBits = SerialStopBitCount.One;

                    serialDevice.Parity = SerialParity.None;

                    textBlock.Text = "Device is connected.";

                }

                else

                {

                    textBlock.Text = "Sorry, no device found.";

                }

            }

     

            public async void dispose_Click(object sender, RoutedEventArgs e)

            {

                if (serialDevice != null)

                {

                    serialDevice.Dispose();

                    serialDevice = null;

     

                    textBlock.Text = "Device is disconnected.";

                }

            }

     

            private void comboselection_Click(object sender, RoutedEventArgs e)

            {

                textBlock.Text = Combo.SelectionBoxItem.ToString();

            }

        }

    }

    How do you dispose of the SerialDevice and disconnect from the COM port andBluetooth module?

    Thanks,

    JT

    Wednesday, July 4, 2018 2:35 PM

Answers

  • I fixed this with the following changes. 

    using Windows.Devices.SerialCommunication;

    using Windows.Devices.Enumeration;

    using Windows.Storage.Streams;

     

    namespace BluetoothPlain

    {

        public sealed partial class MainPage : Page

        {

            public SerialDevice serialDevice; 

            public MainPage()

            {

                this.InitializeComponent();

            } 

            public async void connect_Click(object sender, RoutedEventArgs e)

            {         

                Close();

               string comboChoice = Combo.SelectionBoxItem.ToString();

                string selector = SerialDevice.GetDeviceSelector(comboChoice);

                DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selector);

                if(devices.Count >0)

                {

                    DeviceInformation deviceInfo = devices[0];

                    this.serialDevice = await SerialDevice.FromIdAsync(deviceInfo.Id);

                    this.serialDevice.BaudRate = 115200;

                    this.serialDevice.DataBits = 8;

                    this.serialDevice.StopBits = SerialStopBitCount.One;

                    this.serialDevice.Parity = SerialParity.None;

                    textBlock.Text = "Device is connected.";

                }

                else

                {

                    textBlock.Text = "Sorry, no device found.";

                }

            }

     

            public void Dispose(object sender, RoutedEventArgs e)

            {

               Close();

            }

            public void Close()

            {

                if (this.serialDevice != null)

                {

                    this.serialDevice.Dispose();

                    this.serialDevice = null;

                   textBlock.Text = "Device is disconnected.";

                }

            }

    This now connects and disconnects the Bluetooth connection.  This was a little challenging in UWP, since you cannot use the System.IO.Ports API like in .NET Framework.

    John

    Thursday, July 5, 2018 3:49 PM

All replies

  • Hi,

    Are you developing a UWP proejct? What do you mean by can not disconnect, are there some error message?

    What's the excepted behavior and what's the behavior you are getting now?

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 5, 2018 7:54 AM
  • 1) ues the code button (the one with the brackets <>) to include formatted code into a post.

    2) You don't call Close().

    Thursday, July 5, 2018 8:02 AM
  • I fixed this with the following changes. 

    using Windows.Devices.SerialCommunication;

    using Windows.Devices.Enumeration;

    using Windows.Storage.Streams;

     

    namespace BluetoothPlain

    {

        public sealed partial class MainPage : Page

        {

            public SerialDevice serialDevice; 

            public MainPage()

            {

                this.InitializeComponent();

            } 

            public async void connect_Click(object sender, RoutedEventArgs e)

            {         

                Close();

               string comboChoice = Combo.SelectionBoxItem.ToString();

                string selector = SerialDevice.GetDeviceSelector(comboChoice);

                DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selector);

                if(devices.Count >0)

                {

                    DeviceInformation deviceInfo = devices[0];

                    this.serialDevice = await SerialDevice.FromIdAsync(deviceInfo.Id);

                    this.serialDevice.BaudRate = 115200;

                    this.serialDevice.DataBits = 8;

                    this.serialDevice.StopBits = SerialStopBitCount.One;

                    this.serialDevice.Parity = SerialParity.None;

                    textBlock.Text = "Device is connected.";

                }

                else

                {

                    textBlock.Text = "Sorry, no device found.";

                }

            }

     

            public void Dispose(object sender, RoutedEventArgs e)

            {

               Close();

            }

            public void Close()

            {

                if (this.serialDevice != null)

                {

                    this.serialDevice.Dispose();

                    this.serialDevice = null;

                   textBlock.Text = "Device is disconnected.";

                }

            }

    This now connects and disconnects the Bluetooth connection.  This was a little challenging in UWP, since you cannot use the System.IO.Ports API like in .NET Framework.

    John

    Thursday, July 5, 2018 3:49 PM