Im using a Fez Domino connected to a RN42 on COM1 and a XBee on COM4(REMAPPED) but serial data receive event works only on the last declared port.
There is a special consideration to use multiple com ports.
Please show your code.
namespace AlarmaV1
{
public delegate void DataReceivedEventHandler(string data);
public class SerialDevice
{
static SerialPort com;
static Thread receiveThread;
public event DataReceivedEventHandler DataReceived;
void RemapCOM4()
{
if (com.PortName != "COM4")
return;
if (!com.IsOpen)
throw new Exception("Only use COM4 and make sure it is open");
Register PINSEL9 = new Register(0xE002C024);
PINSEL9.Write(0);// COM4 is now disconnected from P4.28 and P4.29
Register PINSEL1 = new Register(0xE002C004);
PINSEL1.SetBits(0xf << 18);// COM4 is now connected to An3 and An4
}
public SerialDevice(string port, int baudRate)
{
com = new SerialPort(port,baudRate,Parity.None,8,StopBits.One);
com.BaudRate = baudRate;
com.DataReceived += new SerialDataReceivedEventHandler(com_DataReceived);
}
public void Open()
{
try
{
com.Open();
RemapCOM4();
receiveThread.Start();
}
catch (Exception ex)
{
Debug.Print("Exception: " + ex.Message);
}
}
public void Close()
{
try
{
receiveThread.Abort();
com.DiscardInBuffer();
com.DiscardOutBuffer();
com.Close();
}
catch (Exception ex)
{
Debug.Print("Exception: " + ex.Message);
}
}
void com_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
int r = 0;
string msg = "";
r = com.BytesToRead;
if (r <= 0)
return;
byte[] bytes = new byte[r];
com.Read(bytes, 0, r);
msg = new string(System.Text.Encoding.UTF8.GetChars(bytes));
onDataReceived(msg);
}
catch (Exception ex)
{
Debug.Print("Exception: " + ex.Message);
}
}
void onDataReceived(string data)
{
if (DataReceived != null)
DataReceived(data);
}
public void Send(string data)
{
if(com.IsOpen)
com.Write(System.Text.Encoding.UTF8.GetBytes(data), 0, data.Length);
}
}
}
namespace AlarmaV1
{
public class Program
{
static string EOM = new string(System.Text.UTF8Encoding.UTF8.GetChars(new byte[]{0x13}));
static SerialDevice RN42;
static SerialDevice XBee;
static bool ledState = false;
static OutputPort led;
static Thread LEDBlinker;
public static void Setup()
{
led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, ledState);
RN42 = new SerialDevice("COM1", 115200);
RN42.DataReceived += new DataReceivedEventHandler(RN42_DataReceived);
RN42.Open();
XBee = new SerialDevice("COM4", 9600);
XBee.DataReceived += new DataReceivedEventHandler(XBee_DataReceived);
XBee.Open();
XBee.Send("+++");
Thread.Sleep(50);
XBee.Send("ATDN REMOTEMKI" + EOM);
Thread.Sleep(20);
XBee.Send("ATCN" + EOM);
Thread.Sleep(50);
LEDBlinker = new Thread(blink);
LEDBlinker.Start();
}
static void XBee_DataReceived(string data)
{
Debug.Print("XBee: "+data);
XBee.Send("Read: " + data.Length);
}
static void blink()
{
while (true)
{
Thread.Sleep(500);
// toggle LED state
ledState = !ledState;
led.Write(ledState);
}
}
static void RN42_DataReceived(string data)
{
Debug.Print("RN42: "+data);
RN42.Send("Read: " + data.Length);
//XBee.Send("BT Read: " + data.Length);
}
public static void Loop()
{
//Debug.Print("Loop");
}
public static void Main()
{
try
{
Setup();
while (true)
{
Loop();
}
}
catch (Exception ex)
{
Debug.Print("Something wwent wrong, exception: " + ex.Message);
}
}
}
}
I posted the code, I think its very easy, but the problem is that the XBee instance is properly working, and RN42 isnt.
Only work if I use a single instace of my class.
Any help is apreciated.
try registering for serial event after open() not before.
and in main put a thread.sleep() in loop
I tried your suggestions, but no work, also im using Visual Studio 2010 Express C# and when I start debugging a VS exception constantly appears, but I ignore it (it appears on all my projects, its really annoying)
Any other ideas? :S
Thanks in advance
What exception is that?