none
COM Exception on serial port RRS feed

  • Question

  • Hi! 

    I'm working on reading a serial port where is attached a USB/Modbus converter. I wrote the code following the official Microsoft UART example ( write and read  from a serial port).

    But it randomly gives me this type of exception.(first code block)

    Checking on serial port I figured out that the message sent like"hello" sometimes is sent two times consequently like "hellohello" which makes out the issue.

    How can I avoid it? May I need a cancellation token? Can someone give me some advice on this topic? thank you very much!

    "System.Runtime.InteropServices.COMException HResult=0x800710DD Messaggio=L'identificatore di operazione non è valido. L'identificatore di operazione non è valido. Origine=Windows Analisi dello stack: at Windows.Storage.Streams.DataReader.LoadAsync(UInt32 count) at DQ61_Quanta.ModBus.<ReadAsync>d__9.MoveNext() in C:\Users\mtroplini\source\repos\DQ61_Alfa\DQ61_Quanta\ModBus.cs:line 88 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at DQ61_Quanta.MainPage.<VisualizzaDati>d__13.MoveNext() in C:\Users\mtroplini\source\repos\DQ61_Alfa\DQ61_Quanta\MainPage.xaml.

    using System;
    using System.Diagnostics;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Media;
    using System.Threading.Tasks;
    using Windows.UI;
    
    namespace DQ61_Quanta
    
    
    {
        public sealed partial class MainPage : Page
        {//non relevant code           
      
            private DispatcherTimer Timer;
    
            private string Ricevuto;
    
    
            //non relevantcode
    
            public MainPage()
            {
                this.InitializeComponent();
                AvvioTimer();
                AvvioSeriale();
                AvvioF03();
                AvvioProb();
                
            }
    
            private async void MainTimer(object sender, object e)
            {
                try
                {
                    if (modBus.Opened)
                    {
                        await modBus.WriteAsync();
                        VisualizzaDati();
                    }
                    
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Errore riscontrato al Timer:" + ex);
                }
            } //Timer Tick a 10 sec
    
            private void AvvioTimer()
            {
                Timer = new DispatcherTimer();
                Timer.Interval = TimeSpan.FromSeconds(10);
                Timer.Tick += MainTimer;
                Timer.Start();
    
            } //avvio timer set a 10 sec
           
    
            public async void VisualizzaDati()
            {
                if (modBus.Opened)
                {
                    Ricevuto = await modBus.ReadAsync(1024);
    
                    if (Ricevuto != null)
                    {
                        probDATA = F03.TryParse(Ricevuto);
                        
                       
                    }
                    if (iotHUB.Opened_)
                    {
                        iotHUB.InvioDati(probDATA);
                    }
                   
                }
            } //funzione per visualizzare i dati, farci un parse, e inviarli ad iot hub
    
    
    
           

    
        
    
            public async Task WriteAsync()
            {
                DataWriter dataWriter = new DataWriter(PortaSeriale.OutputStream);
    
                #region: conversione in hex
                string Funzione03 = "0x01 0x03 0x80 0x01 0x00 0x04 0x3C 0x09";
    
                // Spezzo Array negli spazzi //converto in byte base 16// Passo ad array
                byte[] bytes = Funzione03.Split(' ').Select(item => Convert.ToByte(item, 16)).ToArray(); 
    
                // Converto in Hexadecimal, formato invio "01-03-80-... ecc"
               // string HexMessage = string.Join("-", bytes.Select(item => item.ToString("X2"))); ma serve? XD
                #endregion 
    
                // standard per scrittura dati al seriale collegando e scollegando il buffer
                dataWriter.WriteBytes(bytes);
                await dataWriter.StoreAsync();
                dataWriter.DetachStream();
                dataWriter = null;
    
            } //scrittura asincrona sul buffer della porta COM
    
            public async Task<string> ReadAsync(uint ReadBufferLength)
            {
                Task<UInt32> loadAsyncTask;
                byte[] ByteLettura = null;
    
                // Set InputStreamOptions per completare la lettura asincrona di 1 o + byte
                dataReader.InputStreamOptions = InputStreamOptions.Partial;
    
                // creazione task per aspettare dati alla serialPort.InputStream
                loadAsyncTask = dataReader.LoadAsync(ReadBufferLength).AsTask();
    
                UInt32 bytesRead = await loadAsyncTask; //base 32bit
    
                if (bytesRead > 0) //se leggi byte, convertili in Hexadecimal
                {
                    #region: conversione in hex
                    ByteLettura = dataReader.ReadBuffer(bytesRead).ToArray();
                    string HexMessage = string.Join("-", ByteLettura.Select(item => item.ToString("X2")));
                    #endregion
                    return HexMessage;
                }
                return $""; //ritorna nulla se i byte letti sono vuoti
            }

    **cleared from non relevant code*


    • Edited by LemonPies Tuesday, November 20, 2018 3:04 PM
    Monday, November 12, 2018 8:18 AM

All replies

  • Hi LemonPies,

    Thank you for posting here.

    For your question, what is the type of your project?

    According to your error message, we could not make sure what cause the error. I try to test your. How do you send the message?

    Please provide more details.

    Best Regards,

    Wendy


    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.

    Wednesday, November 14, 2018 7:35 AM
    Moderator
  • Hi Wendy, sorry for my late reply.

    So my project can be described as: getting data from a device throught a modbus rsu 485 (the device is connected to a raspberry pi with a modbus/usb converter). 

    The message (wich is the modbus query to read registers) is sent from the serial port of RPI using DataReader like:

     dataWriter.WriteBytes(bytes);
                await dataWriter.StoreAsync();
                dataWriter.DetachStream();
                dataWriter = null;


    (I have modified the post in order to obscure my azure personal key)

    Monday, November 19, 2018 8:05 AM
  • nothing found? :(
    Friday, November 23, 2018 3:55 PM