Sorry for the long title but trying to hit keywords. I’m working on a project I have started and stopped multiple times, using the original Fez Panda. It is a project that has two pieces. One is serial communications, and the other will be logging what is captured during the communications to a memory card.
I’m still at the serial communications piece. I am starting small with a very simple program as I find an architecture to work with. I have one sample working when I have the Com1 port hooked up in loop back so that the data sent out is the same that comes in. I can run it all the way up from 300baud to 19200 and it works. But as soon as I move the code to a second Panda, and hook both panda’s together, I run into problems.
The biggest problem is that I’m getting garbage on the serial line and I’m not sure how to handle it. Is it something I need to handle, or is it a result of something I am doing wrong? The two are connect to each other by two jumper wires. One set plugged into Panda 2, and the other set clipped to Panda 1 (with tape).
Panda 1 has a LCD Keypad Shield on it and I’m using it’s buttons to initiate the message and the code for that is below:
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.IO.Ports;
using GHIElectronics.NETMF.FEZ;
namespace pandaScreen
{
public class mainProg
{
readonly static SerialPort _serialPort = new SerialPort("COM1", 300) { ReadTimeout = 0 };
static int readcount = 0;
readonly static OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, false);
static string[] recv;
static bool HandledKeyPress = true;
public static void Main()
{
byte[] yx_data = System.Text.UTF8Encoding.UTF8.GetBytes ("Y ");
byte[] qx_data = System.Text.UTF8Encoding.UTF8.GetBytes ("Q ");
byte[] yx_data2 = System.Text.UTF8Encoding.UTF8.GetBytes("Message|Y - Pressed");
byte[] qx_data2 = System.Text.UTF8Encoding.UTF8.GetBytes("Message|Q - Pressed");
FEZ_Shields.KeypadLCD.Initialize();
FEZ_Shields.KeypadLCD.TurnBacklightOn();
FEZ_Shields.KeypadLCD.CursorHome();
FEZ_Shields.KeypadLCD.Print("Initialized");
_serialPort.Open();
_serialPort.Flush();
while (true)
{
switch (FEZ_Shields.KeypadLCD.GetKey())
{
case FEZ_Shields.KeypadLCD.Keys.Left:
if (HandledKeyPress)
{
HandledKeyPress = false;
_serialPort.Write(qx_data, 0, qx_data.Length);
FEZ_Shields.KeypadLCD.Clear();
FEZ_Shields.KeypadLCD.SetCursor(0, 0);
FEZ_Shields.KeypadLCD.Print("Pressed Left Q");
Debug.Print("Left 'Q' Pressed:");
}
break;
case FEZ_Shields.KeypadLCD.Keys.Right:
if(HandledKeyPress)
{
HandledKeyPress = false;
_serialPort.Write(yx_data, 0, yx_data.Length);
FEZ_Shields.KeypadLCD.Clear();
FEZ_Shields.KeypadLCD.SetCursor(0, 0);
FEZ_Shields.KeypadLCD.Print("Pressed Left Y");
Debug.Print("Right 'Y' Pressed:");
}
break;
case FEZ_Shields.KeypadLCD.Keys.Up:
HandledKeyPress = true;
break;
default:
break;
}
Thread.Sleep(100);
ReadSerial();
if (recv != null)
{
if (recv[0].IndexOf("Y") >= 0)
{
_serialPort.Write(yx_data2, 0, yx_data2.Length);
recv = null;
Debug.Print("Wrote Y Response");
led.Write(false);
}
else
{
if (recv[0].IndexOf("Q") >= 0)
{
_serialPort.Write(qx_data2, 0, qx_data2.Length);
recv = null;
Debug.Print("Wrote Q Response");
led.Write(false);
}
else
{
if (recv[0] == "Message")
{
FEZ_Shields.KeypadLCD.Clear();
FEZ_Shields.KeypadLCD.SetCursor(1, 1);
FEZ_Shields.KeypadLCD.Print(recv[1]);
HandledKeyPress = true;
}
}
}
}
}
}
static void ReadSerial()
{
readcount = 0;
byte[] buffer = new byte[300];
if (_serialPort.BytesToRead >= 19)
{
readcount = _serialPort.Read(buffer, 0, buffer.Length);
//_serialPort.DiscardInBuffer();
if (readcount > 0)
{
recv = new string(System.Text.UTF8Encoding.UTF8.GetChars(buffer)).Split('\n')[0].Split('|');
if (recv.Length > 1)
{
led.Write(true);
//Debug.Print("Received a response");
//'good value
}
}
}
else
{
if (recv != null)
{
recv = null;
//Debug.Print("Set recv Null");
}
}
}
}
}
Panda 2 is a plain old panda with no keypad so it’s code is slightly modified by based on the Panda 1 code and looks like this:
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using System.IO.Ports;
namespace pandaSerial
{
public class Program
{
readonly static SerialPort _serialPort = new SerialPort("COM1", 300) { ReadTimeout = 0 };
static int readcount = 0;
readonly static OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, false);
static string[] recv;
public static void Main()
{
byte[] yx_data2 = System.Text.UTF8Encoding.UTF8.GetBytes("Message|Y - Pressed");
byte[] qx_data2 = System.Text.UTF8Encoding.UTF8.GetBytes("Message|Q - Pressed");
_serialPort.Open();
_serialPort.Flush();
while (true)
{
Thread.Sleep(1000);
ReadSerial();
if (recv != null)
{
if (recv[0].IndexOf("Y") >= 0)
{
_serialPort.Write(yx_data2, 0, yx_data2.Length);
recv = null;
Debug.Print("Wrote Y Response");
led.Write(false);
}
else
{
if (recv[0].IndexOf("Q") >= 0)
{
_serialPort.Write(qx_data2, 0, qx_data2.Length);
recv = null;
Debug.Print("Wrote Q Response");
led.Write(false);
}
else
{
recv = null;
}
}
}
}
}
static void ReadSerial()
{
readcount = 0;
byte[] buffer = new byte[300];
if (_serialPort.BytesToRead >= 19)
{
readcount = _serialPort.Read(buffer, 0, buffer.Length);
//_serialPort.DiscardInBuffer();
if (readcount > 0)
{
recv = new string(System.Text.UTF8Encoding.UTF8.GetChars(buffer)).Split('\n')[0].Split('|');
if (recv.Length > 1)
{
led.Write(true);
//Debug.Print("Received a response");
//'good value
}
}
}
else
{
if (recv != null)
{
recv = null;
//Debug.Print("Set recv Null");
}
}
}
}
}