# Serial Comms trouble in .NET

**URL:** <https://forums.digi.com/t/serial-comms-trouble-in-net/11447>\
**Category:** Edgeport\
**Tags:** serial, net, xbee\
**Created:** [May 3, 2013, 2:30am UTC](https://forums.digi.com/t/serial-comms-trouble-in-net/11447 "2013-05-03T02:30:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheGribble](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.digi.com/thegribble/32/189_2.png) [@TheGribble](https://forums.digi.com/u/TheGribble)\
**Post date:** [May 3, 2013, 2:30am UTC](https://forums.digi.com/t/serial-comms-trouble-in-net/11447/1 "2013-05-03T02:30:13Z")

</div>

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:

```auto
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:

```auto
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:

```auto
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?

Thanks,

Ben.

---

<div class="post-metadata">

**Author:** ![edha](https://avatars.discourse-cdn.com/v4/letter/e/ecccb3/32.png) [@edha](https://forums.digi.com/u/edha)\
**Post date:** [May 6, 2013, 12:14am UTC](https://forums.digi.com/t/serial-comms-trouble-in-net/11447/2 "2013-05-06T00:14:02Z")

</div>

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

```auto
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 &gt; 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 &gt; rec_buffer.Length) {
                        data_length = 0;
                    }
            }

            StringBuilder sb = new StringBuilder();
            sb.Clear();
            for (int i = 0; i &lt; 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);
        }

    }
}

```

---

<div class="post-metadata">

**Author:** ![TheGribble](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.digi.com/thegribble/32/189_2.png) [@TheGribble](https://forums.digi.com/u/TheGribble)\
**Post date:** [May 8, 2013, 6:01am UTC](https://forums.digi.com/t/serial-comms-trouble-in-net/11447/3 "2013-05-08T06:01:02Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![edha](https://avatars.discourse-cdn.com/v4/letter/e/ecccb3/32.png) [@edha](https://forums.digi.com/u/edha)\
**Post date:** [May 8, 2013, 7:50am UTC](https://forums.digi.com/t/serial-comms-trouble-in-net/11447/4 "2013-05-08T07:50:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TheGribble](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.digi.com/thegribble/32/189_2.png) [@TheGribble](https://forums.digi.com/u/TheGribble)\
**Post date:** [May 8, 2013, 10:37am UTC](https://forums.digi.com/t/serial-comms-trouble-in-net/11447/5 "2013-05-08T10:37:28Z")

</div>

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!
