Hi,
I’ve bought a FEZ Panda II and a FEZ Cobra, along with the following 433Mhz RF transmitter([url]http://store.qkits.com/moreinfo.cfm/TX433.pdf[/url]) and receiver([url]http://www.futuraelettronica.net/pdf_ita/8220-RX433N.pdf[/url]).
I’ve connected the receivers digital out to the Panda II using COM1, and I’ve connected the transmitters digital in to the Cobra using COM1.
My issue is this, whenever I press the DOWN button on the Cobra, the Panda II only starts turning off the led about 15-20 seconds later. It remembers any patterns I press in, but it’s just delayed by an amount of time. I’ve done some manual testing with the transmitter and receiver, and know they’re not the parts sending the data slow. I’ve come to the conclusion it might have something to do with the comports and some sort of buffering, however, I keep reading data from the comport, and often read 0 bytes between messages from the Cobra. Also, I start receiving data instantly when I turn on the Panda II, it’s just manual input that’s delayed for some reason. I also control the Cobra’s LED, and it reacts right away.
Here’s my code:
Cobra Transmitter
namespace MindWorX.FEZCobra.TX433
{
public class Program
{
#region Constants
private const Byte SYSTEM = 0x01;
private const Byte SYSTEM_KEEP_ALIVE = 0x01;
private const Byte CONTROL = 0x02;
private const Byte CONTROL_TURN_ON = 0x01;
private const Byte CONTROL_TURN_OFF = 0x02;
#endregion Constants
private static OutputPort MainLED = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, false);
private static InputPort DownButton = new InputPort((Cpu.Pin)FEZ_Pin.Digital.ButtonDown, true, Port.ResistorMode.PullUp);
private static SerialPort TX433 = new SerialPort("COM1", 2400);
public static void Main()
{
TX433.ReadTimeout = 0;
TX433.Open();
while (true)
{
// send a test packet
TX433_SendByte(SYSTEM, SYSTEM_KEEP_ALIVE, (DownButton.Read() == false ? (Byte)0x01 : (Byte)0x02));
// write MainLED to see what we're transmitting, and confirm the Cobra isn't causing the problem
MainLED.Write(DownButton.Read());
}
}
private static void TX433_SendByte(Byte data1)
{
Byte[] packet = new Byte[] {
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, // sending look-at-me signal
0xFF, 0xFF, // sending sync signal
0x4D, 0x57, 0x58, 0x21, // sending MWX! signature
data1};
TX433.Write(packet, 0, packet.Length);
}
private static void TX433_SendByte(Byte data1, Byte data2)
{
Byte[] packet = new Byte[] {
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, // sending look-at-me signal
0xFF, 0xFF, // sending sync signal
0x4D, 0x57, 0x58, 0x21, // sending MWX! signature
data1, data2};
TX433.Write(packet, 0, packet.Length);
}
private static void TX433_SendByte(Byte data1, Byte data2, Byte data3)
{
Byte[] packet = new Byte[] {
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, // sending look-at-me signal
0xFF, 0xFF, // sending sync signal
0x4D, 0x57, 0x58, 0x21, // sending MWX! signature
data1, data2, data3};
TX433.Write(packet, 0, packet.Length);
}
}
}
Panda Receiver
namespace MindWorX.FEZPanda.RX433
{
public class Program
{
#region Constants
private const Byte SYSTEM = 0x01;
private const Byte SYSTEM_KEEP_ALIVE = 0x01;
private const Byte CONTROL = 0x02;
private const Byte CONTROL_TURN_ON = 0x01;
private const Byte CONTROL_TURN_OFF = 0x02;
#endregion Constants
private const Byte MAX_DATA = 32;
private static Byte[] Data = new Byte[0];
private static OutputPort MainLED = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, false);
private static SerialPort RX433 = new SerialPort("COM1", 2400);
public static void Main()
{
RX433.ReadTimeout = 0;
RX433.Open();
while (true)
{
// receive data
RX433_ReceiveData();
}
}
private static void RX433_ReceiveData()
{
Byte[] data = new Byte[16];
Int32 length = RX433.Read(data, 0, data.Length);
if (length == 0)
return; // no data received, no point in wasting power
Byte[] newData = new Byte[Data.Length + length];
Array.Copy(Data, newData, Data.Length);
Array.Copy(data, 0, newData, Data.Length, length);
Data = newData;
for (Int32 index = 0; index < Data.Length - 7; index++)
{
if ((Data[index + 0] == 0x4D) && // M of MWX! signature check
(Data[index + 1] == 0x57) && // W of MWX! signature check
(Data[index + 2] == 0x58) && // X of MWX! signature check
(Data[index + 3] == 0x21) && // ! of MWX! signature check
(Data[index + 4] == SYSTEM) &&
(Data[index + 5] == SYSTEM_KEEP_ALIVE))
{
Byte b = Data[index + 6]; // test byte
if (b == 0x01)
MainLED.Write(false);
else if (b == 0x02)
MainLED.Write(true);
// a keep alive signal was recieved.
// Debug.Print("Got SYSTEM_KEEP_ALIVE");
Data = new Byte[0]; // clear out data. TODO: needs improvement, this kills parts of messages.
}
}
if (Data.Length > MAX_DATA)
{
Data = new Byte[0]; // clears out the current buffer when too large
}
}
}
}
Questions regarding the code are more than welcome, and I’ll try and explain everything as much as possible.
TL;DR: Data transfer has a 15-20 seconds delay, possibly related to buffering of some kind.