Darryl Ingram

Darryl Ingram

  • NA
  • 28
  • 575

C# Serial Communication -- Adafruit Metro M4 and Grand Central M4

Aug 10 2020 6:03 PM
Background:  VS2017 & Windows 10.  Some time ago I put together a C# data acquisition program to collect data from different experiments using different MCUs. 
The app collected CSV text strings and stored the data in a text file for later data evaluation.   The process worked well.
I recently started working with the Metro & Grand Central M4 processor, wanting to use them for additional data acquistion data collections. 
I started by writing simple CSV text string and capturing them with VS2017 monitor screen and Arduino 1.8 monitor screens.  Every thing worked well.

Next, I fired up my old program and -- It failed. 

I started termite and it didn't capture any data.

I started  PUTTY and I captured data.

I stared a console monitor from a 3rd development program -- it did not capture data.

I stared a Hercules.  It didn't capture data.

I started a 4'th development program monitor -- it did not capture data.


This is my code that opens the port

         srPort = new SerialPort();
         srPort.PortName = portName;
         srPort.BaudRate = baud;
         srPort.DtrEnable = false;
         srPort.Handshake = System.IO.Ports.Handshake.None;
         srPort.RtsEnable = false;
         srPort.StopBits = System.IO.Ports.StopBits.One;
         try
         {
            srPort.Open();
            if (srPort.IsOpen)
            {
               this.srPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.srPort_DataReceived);
               //this.srPort.ErrorReceived += new System.IO.Ports.SerialErrorReceivedEventHandler(this.srPort_ErrorReceived);
               srPort.DtrEnable = true;
               Thread.Sleep(20);
               srPort.DtrEnable = false;
               MessageBox.Show("Port: " + portName + " Open \r\n");
            }
         }
         catch (Exception ex)
         {
            MessageBox.Show("Opening Port:  " + portName + " Failed \n\n" + ex.Message);
            srPort = null;
         }
         return srPort;


This is the event handler for receiving the code
      private void srPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
      {
         int numbytes = srPort.BytesToRead;
         if (numbytes > 0)
         {
            byte[] bytes = new byte[numbytes];
            int knt = srPort.Read(bytes, 0, numbytes);
            this.ScanStreamForValidMsg(bytes);
         }
      }
     


My program reports that the port is opened properly but never receives data. I placed a breakpoint in the handler and it is never hit.
 
Also...the program hangs when attempting to send a character but so does Arduino & VS2017 programs.
 
I don't know if this is a board problem or a code problem.
 
So...What am I missing???

Any Suggestions???