Main Site Documentation

Very Odd SerialPort Behavior when Consecutive Characters are Repeated


#1

I’ve got a Panda II with the latest firmware (4.1.7.0) running this simple program:


using System;
using System.Threading;
using System.IO.Ports;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.FEZ;

namespace FEZ_Panda_II_Application1
{
    public class Program
    {
        public static void Main()
        {
            SerialPort sp = new SerialPort("COM1", 115200, Parity.None, 8, StopBits.One);

            sp.Open();
            sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);

            Thread.Sleep(Timeout.Infinite);           
        }

        static void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort mySerialPort = (SerialPort)sender;

            int numBytes = mySerialPort.BytesToRead;

            byte[] received = new byte[numBytes];

            mySerialPort.Read(received, 0, numBytes);

            Debug.Print("Num Bytes Received: " + numBytes);
            Debug.Print("Byte[0] Received (Decimal): " + received[0].ToString());
        }

    }
}

As you can see, the program simply prints out the number of bytes received in the serial buffer as well as the value of the first byte received (as a decimal). Now, testing this program using HyperTerminal, if I type 1 character at a time and enter the same character each time, when I get to the 3rd character, that character always seems to be messed up.

For example, let’s say I type a capital “M” each time (this is equal to decimal 77 or hexadecimal 4D in UTF8 encoding), my program outputs:

Num Bytes Received: 1
Byte[0] Received (Decimal): 77
Num Bytes Received: 1
Byte[0] Received (Decimal): 77
Num Bytes Received: 1
Byte[0] Received (Decimal): 205

As you can see, on the third attempt, the value suddenly changes to 205, even though I’m simply typing the same character each time.

Does anyone know what’s going on? And can anyone else replicate this? (Provided you have a RS-232 to TTL level-shifter available.)

----- UPDATE -----

If I use TeraTerm instead of HyperTerminal, this odd behavior goes away, so the fault most likely lies in the terminal software.


#2

Is it possible that configuration of the ports is mismatched?


#3

I’ve verified that the COM port settings match on the Panda and on the PC, so it’s probably not the culprit.

Thankfully, TeraTerm works just fine. HyperTerminal must just be doing something weird.