Main Site Documentation

Errors when opening two serial ports. Exception System.Exception - CLR_E_WRONG_TYPE (5) ####


#1

I wanted to use two serial ports on my Fez.Spider . However I get the following errors in the debug output window if I open both ports. All is well if I open only one port at a time.
I need both open.

Exception System.Exception - CLR_E_WRONG_TYPE (5)

#### Message: 
#### System.Text.UTF8Encoding::GetChars [IP: 0000] ####
#### Gadgeteer.Interfaces.Serial::ReadLineProcess [IP: 003e] ####

A first chance exception of type ‘System.Exception’ occurred in mscorlib.dll
Exception parsing serial line
#### Exception System.Exception - CLR_E_WRONG_TYPE (5) ####
#### Message:
#### System.Text.UTF8Encoding::GetChars [IP: 0000] ####
#### Gadgeteer.Interfaces.Serial::ReadLineProcess [IP: 003e] ####
A first chance exception of type ‘System.Exception’ occurred in mscorlib.dll
Exception parsing serial line
#### Exception System.Exception - CLR_E_WRONG_TYPE (5) ####
#### Message:
#### System.Text.UTF8Encoding::GetChars [IP: 0000] ####
#### Gadgeteer.Interfaces.Serial::ReadLineProcess [IP: 003e] ####

The errors come as if in a loop. If I do not stop it I will keep receiving them. I have nothing connected to the serial ports and I do not receive events I set up.

//Basic code I am using

#region Serial1
// Using Socket 4 (IKUX) to GHI prototype board. Serial connection 1 to WiFly serial pins.
// Pin 1 3.3VDC, Pin 2 5VDC, Pin 3 GPIO (Interrupt), Pin 4 TX, Pin 5 RX, Pin 6 GPIO, Pin 7,8,9 NC, Pin 10 GND
serial1 = new GT.Interfaces.Serial(GT.Socket.GetSocket(4, false, null, null),
9600, // Using default for device. I have no need for high speed.
GT.Interfaces.Serial.SerialParity.None,
GT.Interfaces.Serial.SerialStopBits.One,
8,
GT.Interfaces.Serial.HardwareFlowControl.NotRequired, null);

        //Use either
        //serial1.DataReceived += new GT.Interfaces.Serial.DataReceivedEventHandler(Serialserver1_DataReceived);
        serial1.LineReceived += new GT.Interfaces.Serial.LineReceivedEventHandler(Serialserver1_LineReceived);
        
        serial1.AutoReadLineEnabled = true;
        serial1.LineReceivedEventDelimiter = "\r\n";
        serial1.ReadTimeout = 1000;
        serial1.Open();

#endregion Serial1

#region Serial2
// Using Socket 9 (IKUX) to GHI prototype board. Serial connection 2 to WiFly serial pins.
// Pin 1 3.3VDC, Pin 2 5VDC, Pin 3 GPIO (Interrupt), Pin 4 TX, Pin 5 RX, Pin 6 GPIO, Pin 7,8,9 NC, Pin 10 GND
serial2 = new GT.Interfaces.Serial(GT.Socket.GetSocket(9, false, null, null),
9600, // Using default for device. I have no need for high speed.
GT.Interfaces.Serial.SerialParity.None,
GT.Interfaces.Serial.SerialStopBits.One,
8,
GT.Interfaces.Serial.HardwareFlowControl.NotRequired, null);

        //Use either
        //serial2.DataReceived += new GT.Interfaces.Serial.DataReceivedEventHandler(Serialserver2_DataReceived);
        serial2.LineReceived += new GT.Interfaces.Serial.LineReceivedEventHandler(Serialserver2_LineReceived);
        
        serial2.AutoReadLineEnabled = true;
        serial2.LineReceivedEventDelimiter = "\r\n";
        serial2.ReadTimeout = 1000;
        serial2.Open();

#endregion Serial2

if (serial1 != null && serial1.IsOpen)
{
txtSerial1 = new Text(baseFontB, “Serial port 1 open.”);
serial1.Write(“Serial port 1 open.”);
canvasSerialStatus.Children.Add(txtSerial1);
Canvas.SetTop(txtSerial1, 2);
Canvas.SetLeft(txtSerial1, 1);
}

        if (serial2 != null && serial2.IsOpen)
        {
            txtSerial2 = new Text(baseFontB, "Serial port 2 open.");
            serial2.Write("Serial port 2 open.");
            canvasSerialStatus.Children.Add(txtSerial2);
            Canvas.SetTop(txtSerial2, 2);
            Canvas.SetLeft(txtSerial2, 170);
        }

//Events
// Socket 4 Pin 5 RX
private void Serialserver1_LineReceived(Object sender, String data)
{
serial1.Write("[Start 1]" + data);
serial2.Write("[Start 2]" + data);
if (WiFlytop >= 220)
{
WiFlytop = topofDisplay;
}
dispatchtimer.Stop(); //Stop time update while reading the serial port
PrintWiFlyDataToDisplay(data);
dispatchtimer.Start();
serial1.Write("[Stop 1]");
serial2.Write("[Stop 2]");
}
//

//Socket 9 Pin 5 RX,
private void Serialserver2_LineReceived(Object sender, String data)
{
serial2.Write("[Start 2]" + data);
if (WiFlytop >= 220)
{
WiFlytop = topofDisplay;
}
// dispatchtimer.Stop(); //Stop time update while reading the serial port
// PrintWiFlyDataToDisplay(data);
// dispatchtimer.Start();
serial2.Write("[Stop 2]");
}

Looking at a watch on serial1 and serial2 seems normal. Both ports work as long as only one is open.

What gives?


#2

if you have nothing connected to serial ports you could be receiving garbage and failing when the line parser tries to process the junk.


#3

@ willgeorge - Please use code tags in your posts