Main Site Documentation

Problem connecting Arduino and Panda II via serial


#1

hi, I’ve a bare atmega chip on a breadboard and an panda II…both conected via serial with 2 cables, pin 0 on arduino to pin 1 on panda and pin 1 on arduino to pin 0 on panda.

what i want is to send for example a command from the arduino to the panda and get a response back. my problem is that most of the time i send something to the panda using the serial monitor from arduino IDE i get this error

Exception System.Exception - CLR_E_WRONG_TYPE (3)

#### Message: 
#### System.Text.UTF8Encoding::GetChars [IP: 0000] ####
#### FEZ_Panda_II_Application1.Program::UART_DataReceived [IP: 002d] ####
#### System.IO.Ports.SerialPort::DataEventHandler [IP: 0012] ####

A first chance exception of type ‘System.Exception’ occurred in mscorlib.dll
An unhandled exception of type ‘System.Exception’ occurred in mscorlib.dll

The code on the panda is very simple :

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

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

using GHIElectronics.NETMF.FEZ;


namespace FEZ_Panda_II_Application1
{
    public class Program
    {
        //Comunicacion
        public static SerialPort UART = new SerialPort("COM1", 115200);


        public static void Main()
        {
            // Blink board LED

            Debug.EnableGCMessages(false);

            bool ledState = false;
            
            OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, ledState);

            //Inicia Comunicacion
            UART.Open();
            UART.DataReceived += new SerialDataReceivedEventHandler(UART_DataReceived);
            
            while (true)
            {
                // Sleep for 500 milliseconds
                Thread.Sleep(500);

                // toggle LED state
                ledState = !ledState;
                led.Write(ledState);
            }
        }

        static void UART_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            
            
            if (e.EventType == SerialData.Chars)
            {
                
                 byte[] readbuffer = new byte[UART.BytesToRead];

                UART.Read(readbuffer, 0,readbuffer.Length);

          string RX_Data = new string(Encoding.UTF8.GetChars(readbuffer));

                Debug.Print(RX_Data);

                Debug.GC(true);

                switch (RX_Data)
                {
                    case "FE":
                        break;
                    case "T1":
                        break;
                    case "T2":

                        break;

                }
                
            }
             
        }

    }
}

what is wrong?


#2

You can’t assume that all your string data will arrive in one pass through the serial DataRecieved event.

Why don’t you diagnose this by setting a breakpoint in the recieved event handler and checking your CHAR[] and STRING values. I suspect you’re just not handling a situation properly…

but I still think you’re going to need to deal with your data coming in differently, as the current approach you take won’t give you what you want.


#3

the command i’m passing are at most 13 bytes long.

anyway can you give me some example of a better way?

and you think that the error is because of the size of the data?


#4

DataRecieved means there’s at minimum one byte of data ready to be handled. To continue with this methodology, you need to take it out of the hardware queue in the handler and put it in your own queue and then exit the handler. Somewhere else you’ll need to be looking at your queue and when you have a full command, parse it and act on it.

OR: you can change your methodology and not read in a queue like this.

Do I think the error is because of the data length? Well, no I don’t. The error clearly says it’s a data type mismatch. Thats why I say you need to debug this more with breakpoints. You haven’t told us what string or characters are in your buffer when you get the error, or even what line the error occurred on.

But the error doesn’t stop me from knowing that your algorithm is going to be your next problem.


#5

Do not forget to connect GND between each-other.


#6

hi, i have the same problem that omartinez, i don´t know, if you already have the solution?
I did the connection of GND between each-other and the continuous error
Please, tell me if they have the answer


#7

hi Fur_739, welcome to the forum! Probably best to start your own thread and to show us your wiring, and also show us your code and fully explain what the behaviour you’re seeing is.


#8

Don’t forget the input stream bytes must be encoded as UTF-8. If you try to convert bytes which are not UTF-8 you will get an exception.


#9

thanks for your quick response, I show my code, sorry but Im new in Fez Panda and .Net code


#10

@ fur_739 - What is your question about the latest code?


#11

after that communication begins, send an error, my question is, why?


#12

a screen capture of your code isn’t very helpful.

What error?

What do you expect to occur based on your code?

What do you ACTUALLY see - describe the program flow.


#13

What is the format of the data that is being received?