Main Site Documentation

SerialDataReceivedEventHandler Exception Error


#1

Below is firmware info - I’ve checked both sides and they match.
I’m getting an exception error when checking amount of data received on the com port in the serial event code.
Below is my code.
The code runs in the debugger. It flashes the LED and sends data out the com port to teraterm which is connected to the com port. When I send data into the com port, the serial event fires, but I get an exception error when checking how many bytes are in the com port buffer -
int Bytes2Read = UART.BytesToRead;

This code works on .net framework and .net compactframework. Am I doing something wrong?
Any help is greatly appreciated.


using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using System.Threading;
using System.IO.Ports;
using System.Text;
using BuffMgr;//needed to build a scan or swipe


namespace FEZ_Mini_Application1
{
    public class Program
    {
        
        public static int read_count = 0;
        public static byte[] rx_data = new byte[10];
        public static byte[] tx_data;
        public static SerialPort UART = null;

        // Blink board LED
        public static bool ledState = true;
        public static OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, ledState);


        public static void Main()
        {
            int counter = 0;

            SerialPort UART = new SerialPort("COM1", 115200);
            UART.Open();
            UART.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);


             while (true)
                        {
                            // create a string
                            string counter_string = "Count: " + counter.ToString() +
                                                    "\r\n";
                            // convert the string to bytes
                            byte[] buffer = Encoding.UTF8.GetBytes(counter_string);
                            // send the bytes on the serial port
                            UART.Write(buffer, 0, buffer.Length);
                            // increment the counter;
                            counter++;
                            //wait...
                            Thread.Sleep(100);
                            // toggle LED state
                            ledState = !ledState;
                            led.Write(ledState);
                        }
        }
        
        static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            // Visual indication of data received.
            led.Write(false);
            // Read the number of bytes to read from the serialport
            int Bytes2Read = UART.BytesToRead;
            // Create local array to put data from serialport in.
            byte[] bytearray = new byte[Bytes2Read];

            // Read the bytes from the serial port
            UART.Read(bytearray, 0, Bytes2Read);
        }
    }
}

Using FEZ Mini· FEZ Mini V 4.1.6.0
SDK Version 1.0.15
ClrInfo.targetFrameworkVersion: 4.1.2821.0
SolutionReleaseInfo.solutionVersion: 4.1.6.0
SolutionReleaseInfo.solutionVendorInfo: GHI Electronics, LLC
SoftwareVersion.BuildDate: Jun 24 2011


#2

[quote]I’m getting an exception error when checking amount of data received on the com port in the serial event code.
Below is my code.[/quote]

What exception?
What line?

Welcome to the community


#3

[quote]This code works on .net framework and .net compactframework. Am I doing something wrong?
[/quote]
This really works?

You are getting a NullException.

You have defined UART twice, once as static class member and once within the Main() method.

You created the SerialPort object within the Main() method, but are referrencing the class member, which was never instaniated, within the DataReceived event.

Within Main() change:

SerialPort UART = new SerialPort("COM1", 115200);

To:

UART = new SerialPort("COM1", 115200);


#4

Good catch mike :slight_smile:


#5

Mike / GHI
Thanks for fast response. The code sort of works in the sense that it detects the incoming serial interupt and and goes to the event handler. The code stops working when it generates a null exception while asking for number of bytes in the UART. Now that you point out how I goofed up creating the UART, it clear why the event handler can’t determine number of bytes.

I made suggested changes and no surprise, it works :slight_smile:
Thanks for helping out


#6

:smiley: Welcome to the community,