Serial Link from Cerberus to Spider - Need some help

Hi,
I have been strugling to get some understanding of serial communication.
I got my Cerberus and Spider linked up by connecting pin 4-5, 5-4 (cross-wise) and GND-GND.
Declaring the UART port works fine, I can SEND some bytes from the spider and the UART_DataReceived routine fires on the Cerberus when i press the SEND button on the Spider.

My problem is that i cant find any simple examples of how to parse the data to readable text wich i can eg. display on my LCD.

Can anybody who has a moment to spare help me or point me to a simple code structure that will SEND a piece of text (“Hello World”)
and a piece code that will recieve the data and put it in a STRING variable that i can display.
Thanks

This is my code on the spider :

 public partial class Program
    {
        static Font font2 = Resources.GetFont(Resources.FontResources.NinaB);
        // This method is run when the mainboard is powered up or reset.   
        GT.Interfaces.Serial serial = new GT.Interfaces.Serial(GT.Socket.GetSocket(9, false, null, null), 300,
        GT.Interfaces.Serial.SerialParity.None, GT.Interfaces.Serial.SerialStopBits.One, 8,
        GT.Interfaces.Serial.HardwareFlowControl.NotRequired, null);
        static int Counter = 1;

        void ProgramStarted()
        {
            button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
            display_T35.SimpleGraphics.Clear();
            display_T35.SimpleGraphics.DisplayText(" Program Ready ", font2, GT.Color.Green, 20, 20);
        }

        void button_ButtonPressed(Button sender, Button.ButtonState state)
        {
            display_T35.SimpleGraphics.Clear();
            display_T35.SimpleGraphics.DisplayText("sending byte" , font2, GT.Color.Green, 20, 20);
            serial.Open();
           
            {          
                {
                    // create a string
                    string counter_string = "Counter is  " + Counter + "\r\n";
                    display_T35.SimpleGraphics.DisplayText(counter_string, font2, GT.Color.Green, 20, 40);
                    byte[] buffer = Encoding.UTF8.GetBytes(counter_string);
                    // send the bytes on the serial port
                    serial.Write(buffer, 0, buffer.Length);
                    //wait...
                    Thread.Sleep(10);
                    Counter++;
                }
            }
        }
    }

And on the Cerberus I have tried dosens of code examples…
like


  public partial class Program
    {
        GT.Interfaces.Serial serial = new GT.Interfaces.Serial(GT.Socket.GetSocket(6, false, null, null), 300,
        GT.Interfaces.Serial.SerialParity.None, GT.Interfaces.Serial.SerialStopBits.One, 8,
        GT.Interfaces.Serial.HardwareFlowControl.NotRequired, null);
        private static byte[] buffer= new byte[0];
        private string msg;
       
        void ProgramStarted()
        {
            display_HD44780.PrintString("Cerberus OK");
            serial.Open();
            serial.DataReceived += new GT.Interfaces.Serial.DataReceivedEventHandler(serial_DataReceived);
        }

        void serial_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
        {
            display_HD44780.Clear();
            display_HD44780.PrintString("Data Received..");
            
            int Cnt = (serial.BytesToRead);  
            Debug.Print("RX :" + Cnt) ;      // This prints  the number of sent bytes OK
            for (int i = 1; i < Cnt; i++)
            {
            // ------------read one byte from the buffer

             serial.Read(buffer,0,1);
             // byte B = (byte)serial.ReadByte();
             // I tried all kinds of variations of serial.Read and serial.ReadByte.....

             // ------------Add the read carachter to the msg string
             //msg = msg + buffer.ToString; doesnt work

             char[] car = UTF8Encoding.UTF8.GetChars(buffer);
             msg =  msg  +  car ;
                //comes up empty too....
             }
             display_HD44780.PrintString(msg);
           }
         } 
      }

When the receive event fires and you read the data, you may not necessarily have all data as the data is still coming on the way.

Try a thread that loops and read incoming data.

Check this project:

It has everything you need. Very good code for reference.

1 Like

Now that is going to come in handy for a project I have in mind :slight_smile: thanks for pointing this out Architect.

You are welcome!

@ Gus

That was fast :smiley:

http://www.tinyclr.com/forum/topic?id=8118

This is a useful project that should be on codeshare not wiki so here it is moved http://www.tinyclr.com/codeshare/entry/517