Home/Support/Support Forum/Serial Comms trouble in .NET
Welcome to Digi Forum, where you can ask questions and receive answers from other members of the community.

Serial Comms trouble in .NET

0 votes
Hi all,

I'm having a nightmare of a time trying to talk to an XBee in API mode over serial interface using .NET.

I've got two XBees, a coordinator on COM5 and an end point device with the address 5001 on COM6. If I use the X-CTU packet builder and send this down COM5 in HEX:
7e 00 14 01 01 50 01 00 08 00 04 42 6f 62 20 54 68 65 20 58 42 65 65 c8
then I get a response on COM5 and this back over COM6:
7e 00 1a 80 00 13 a2 00 40 8d 53 c9 3c 00 08 00 04 42 6f 62 20 54 68 65 20 58 42 65 65 c1
which is great.

When I use this code in .NET:
SerialPort port = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One); port.Open(); byte[] buffer = { 0x7e, 0x00, 0x14, 0x01, 0x01, 0x50, 0x01, 0x00, 0x08, 0x00, 0x04, 0x42, 0x6f, 0x62, 0x20, 0x54, 0x68, 0x65, 0x20, 0x58, 0x42, 0x65, 0x65, 0xc8 }; port.Write(buffer, 0, buffer.Length); port.Close();
I don't get anything back at all. I've run both through a serial port monitor and both seem to be sending byte-for-byte identical data. I think I'm going mad here, has anybody got any ideas?


asked May 3, 2013 in USB Serial by TheGribble New to the Community (4 points)

Please log in or register to answer this question.

1 Answer

0 votes
This program will send data from COM1 to COM2. Before running this program make sure you are setting the port with loopback mode.

COM1 vs COM2
pin 2(COM1) connected to 3(COM2)
pin 3(COM1) connected to 2(COM2)
pin 5(COM1) connected to 5(COM2)

pin 2 = RX
pin 3 = TX
pin 5 = GND
using System; using System.Windows.Forms; using System.IO.Ports; using System.Threading; using System.Diagnostics; using System.Text; namespace WindowsFormsApplication1 { public partial class Form1 : Form { const int TX_DELAY = 1000; // data byte[] buffer = { 0x7e, 0x00, 0x14, 0x01, 0x01, 0x50, 0x01, 0x00, 0x08, 0x00, 0x04, 0x42, 0x6f, 0x62, 0x20, 0x54, 0x68, 0x65, 0x20, 0x58, 0x42, 0x65, 0x65, 0xc8 }; // transmitter port SerialPort port1 = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); // receiver port SerialPort port2 = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One); byte[] rec_buffer = new byte[4096]; int data_length = 0; public Form1() { InitializeComponent(); port2.DataReceived += new SerialDataReceivedEventHandler(port2_DataReceived); } void port2_DataReceived(object sender, SerialDataReceivedEventArgs e) { // receive data from COM2 while (port2.BytesToRead > 0) { // save data to rec_buffer rec_buffer[data_length] = Convert.ToByte(port2.ReadByte()); // increment data length data_length++; // avoid error when data length more than rec_buffer length if (data_length > rec_buffer.Length) { data_length = 0; } } StringBuilder sb = new StringBuilder(); sb.Clear(); for (int i = 0; i < rec_buffer.Length; i++) { if (rec_buffer[i] != 0) { sb.Append(rec_buffer[i] + " "); } } Debug.Print(sb.ToString()); } private void Form1_Load(object sender, EventArgs e) { // close COM1 if opened if (port1.IsOpen) { port1.Close(); } // COM1 opened in here port1.Open(); // close COM2 if opened if (port2.IsOpen) { port2.Close(); } // COM2 opened in here port2.Open(); // write data from COM1 to COM2 port1.Write(buffer, 0, buffer.Length); // wait data sent Thread.Sleep(TX_DELAY); } } }
answered May 6, 2013 by edha Community Contributor (71 points)
edited May 5, 2013 by edha
Bizarre - exactly the same result. Using a null modem cable this goes through fine, printing the result to the output exactly as you'd expect.
Trapping the traffic on a serial port monitor it looks exactly right. Take the null modem cable out and put the XBees in - nothing gets through.

Now the really weird bit - if I monitor the traffic in Serial Port Monitor I can see the traffic leaving but not returning. Copy the hex values being sent down and send them manually from a terminal program and they arrive at the other COM port via the XBees just fine.

I'm beginning to think I'm going mad here!
Try to send the data from the X-CTU and look at the data being sent from the X-CTU through the above program. To do that, you have to close with comment the  / / port1.Open ()  in the Form1_Load.

There may be an additional frame of the X-CTU packet builder sent, you can see that in the Debug.Print(sb.ToString())  in port2_DataReceived. Look at result in Immediate window of .net (Click view menu -> other windows -> command window and then typo immed on command window, immediate window will appear on there)

Remember, Debug.Print can not display control characters in immediate window, only ascii that will appear, you should be adding a breakpoint on the Debug.Print(sb.ToString())  to see the data sent from X-CTU.
Solved it in the end, but still don't know why the problem was happening in the first place. Tried your code (Great idea, btw) and the data sent was exactly the same in either case. Switched over to using 64-bit addressing instead of 16-bit, and now suddenly it's working. No idea why and No time to find out sadly - guess I'll never know. :)

Thanks for all your help, anyway!