Beijer Electronics (formerly QSI Corporation)
http://www.qsiforums.com/

Issues with .NET CF Serial Communication
http://www.qsiforums.com/viewtopic.php?f=8&t=3924
Page 1 of 1

Author:  NotSoTragicHero [ Mon Mar 15, 2010 10:26 am ]
Post subject:  Issues with .NET CF Serial Communication

We've had more trouble than it's worth with serial communication in the compact framework for CE. We've tried the base SerialPort class provided, the OpenNETCF classes, and we've even tried creating our own wrapper class using the native dlls.

The code is being deployed to a TREQ-M4, which is connected, via RS232, to an Enfora MT-Gu. When I connect the Enfora unit directly to my PC, I can connect and make successful calls to it using HyperTerminal as well as with our wrapper class. But when I convert the Kernel32 calls to CoreDLL calls, the wrapper class fails, as does the CF SerialPort and OpenNETCF classes.

I've decided to start from scratch as I've been racking my brain too much and hacking away at too many things for too long now. Below is a quick example class I've written to communicate with the device.

The DataReceived event is never fired, which is fine, but even polling the port doesn't work, as the BytesToRead value is always 0. Has anyone else had this problem using serial communication with the TREQs?

Code:
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Collections;
using System.Threading;

namespace SerialComTest
{
    public partial class form_SerialCom : Form
    {
        private SerialPort port;

        private String displayString;

        public form_SerialCom()
        {
            InitializeComponent();
            button_Send.Click += new EventHandler(button_Send_Click);

            port = new SerialPort("COM1", 115200, Parity.None, 8, StopBits.One);
            port.Open();
            port.NewLine = Environment.NewLine;
            port.RtsEnable = true;
            port.DtrEnable = false;
            port.Handshake = Handshake.RequestToSend;
            port.ReceivedBytesThreshold = 1;
            port.WriteTimeout = 500;
            port.ReadTimeout = 500;
            port.DiscardInBuffer();
            port.DiscardOutBuffer();

            port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            port.ErrorReceived += new SerialErrorReceivedEventHandler(port_ErrorReceived);

            if (port.IsOpen)
            {
                textBox_Rec.Text += "Connected to " + port.PortName + Environment.NewLine;
            }
            Thread thread = new Thread(delegate()
            {
                while (true)
                {
                    try
                    {
                        if (port.BytesToRead > 0)
                        {
                            displayString += port.ReadExisting();
                            textBox_Rec.Text += displayString + Environment.NewLine;
                        }
                    }
                    catch (TimeoutException) { }
                }
            });
            thread.Start();
        }

        void port_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
        {
            throw new NotImplementedException();
        }

        void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            displayString = port.ReadLine();

            try
            {
                this.Invoke(new EventHandler(SetText));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void SetText(object o, EventArgs e)
        {
            textBox_Rec.Text += displayString + Environment.NewLine;
            displayString = string.Empty;
        }

        void button_Send_Click(object sender, EventArgs e)
        {
            try
            {
                port.WriteLine(textBox_Send.Text);
            }
            catch (TimeoutException)
            {
                textBox_Rec.Text += ("The write operation timed out." + Environment.NewLine);
            }
            if (port.BytesToRead > 0)
            {
                displayString = port.ReadExisting();
                textBox_Rec.Text += displayString;
            }

            textBox_Send.Text = "";
        }
    }
}

Author:  Ron L. [ Mon Mar 15, 2010 11:07 am ]
Post subject:  Re: Issues with .NET CF Serial Communication

It sounds like you could have an issue with the serial cable. Are you able to send & receive data using the included program "MinTerm"? You can find this under the /hardisk/bin directory.

You may want to simplify your C# application somewhat for testing. A combination of using the DataRecieved event with ReadExisting.

I'm out of office at the moment, but I'll see if I can get you some sample code if needed.

Author:  NotSoTragicHero [ Mon Mar 15, 2010 11:25 am ]
Post subject:  Re: Issues with .NET CF Serial Communication

Ron L. wrote:
It sounds like you could have an issue with the serial cable. Are you able to send & receive data using the included program "MinTerm"? You can find this under the /hardisk/bin directory.

You may want to simplify your C# application somewhat for testing. A combination of using the DataRecieved event with ReadExisting.

I'm out of office at the moment, but I'll see if I can get you some sample code if needed.


The cables should be fine. We've used the same cables / adapters in direct communication with the Enfora / PCs.

Hmm, using MinTerm seems to have ruled out a few of our speculations, as MinTerm also fails. The Port Open is checked "red", and I'm assuming this should be "green"? The Clear-To-Send is also checked "red".

Author:  Ron L. [ Mon Mar 15, 2010 11:48 am ]
Post subject:  Re: Issues with .NET CF Serial Communication

Right now, I would continue testing with the known working software MinTerm, until you can send some data back & forth. Your description of how MinTerm is working sounds like it's working correctly.

One challenge that you could be having is that COM1 does not have DTR/DTS lines and your modem may need those lines. See page 89 in the TREQ-M4 manual for it's pin-out.
http://www.qsicorp.com/pdf/qsi_treq-m4_user_manual.pdf

I had a similar problem with another terminal that did not have DTR/DTS lines and there was a work around in the cable as described in this application note:
http://www.qsicorp.com/download/index.p ... minals_ppp

But before I would start looking at the DTR/DTS lines, I would double check the RTS/CTS/RX and TX lines looking at the documentation of the pin-out of the both the TREQ-M4 and the Modem.

Author:  danieln [ Mon Mar 15, 2010 11:54 am ]
Post subject:  Re: Issues with .NET CF Serial Communication

The Port Open icon should be "red" when connected, and grey when not connected.

As Ron mentions, it is a good idea to double check the signals you have wired up between the devices. I've seen similar communication issues in the past if certain lines are left floating on the cable itself.

Author:  NotSoTragicHero [ Mon Mar 15, 2010 12:20 pm ]
Post subject:  Re: Issues with .NET CF Serial Communication

The Enfora has 3 active pins.

2 - Data In
3 - Data Out
5 - Ground

This is all the documentation I could find in regards to this. Any insight? I'm not very well versed in serial communication, as this is the first project we've taken on that requires it. :-/

Author:  Ron L. [ Mon Mar 15, 2010 12:33 pm ]
Post subject:  Re: Issues with .NET CF Serial Communication

From the pinout of the Enfora your giving me, it should be a straight through cable from a TREQ-M4 BOB. Are you using a TREQ-M4 BOB that comes with the developers kit?

What is the model of Enfora modem your using?

Does it respond to "AT\r\n"?

Author:  NotSoTragicHero [ Mon Mar 15, 2010 12:48 pm ]
Post subject:  Re: Issues with .NET CF Serial Communication

Ron L. wrote:
From the pinout of the Enfora your giving me, it should be a straight through cable from a TREQ-M4 BOB. Are you using a TREQ-M4 BOB that comes with the developers kit?

What is the model of Enfora modem your using?

Does it respond to "AT\r\n"?


Yes, it's the dev kit BOB. The Enfora is the MT-Gu.

When I send the command, or any command, it seems, to me, that there is a write timeoutexception being thrown. The entry textbox freezes up for a few seconds, and then comes back to life, with nothing received.

Author:  danieln [ Mon Mar 15, 2010 1:11 pm ]
Post subject:  Re: Issues with .NET CF Serial Communication

"When I send the command, or any command, it seems, to me, that there is a write timeoutexception being thrown. The entry textbox freezes up for a few seconds, and then comes back to life, with nothing received."

This behavior in Minterm is identical to issues I've seen in the past. How exactly is the DB9 serial cable wired to the modem? You need to verify that RX,TX,GND are wired to their corresponding pinouts of the TREQ-M4 device. Also, do not leave the other signals floating (they should be grounded), as phantom voltages on those other lines can cause erratic communication behavior.

Author:  NotSoTragicHero [ Mon Mar 15, 2010 1:32 pm ]
Post subject:  Re: Issues with .NET CF Serial Communication

danieln wrote:
"When I send the command, or any command, it seems, to me, that there is a write timeoutexception being thrown. The entry textbox freezes up for a few seconds, and then comes back to life, with nothing received."

This behavior in Minterm is identical to issues I've seen in the past. How exactly is the DB9 serial cable wired to the modem? You need to verify that RX,TX,GND are wired to their corresponding pinouts of the TREQ-M4 device. Also, do not leave the other signals floating (they should be grounded), as phantom voltages on those other lines can cause erratic communication behavior.


It's a pre-built wire harness that came with the modem.

It converts the data connection from a proprietary molex to separate serial and power connections. That serial cable goes from the modem, to a male/male adapter, to the BOB.

Author:  danieln [ Mon Mar 15, 2010 2:51 pm ]
Post subject:  Re: Issues with .NET CF Serial Communication

While the pre-built cable can work fine with a PC serial port, it is still possible that it can run into trouble with the TREQ-M4, depending on how it is wired. This usually because the PC serial port has extra logic behind it to "account" for situations where lines may be left floating.

Just last month a customer bought one of these MTG-u modems and was having an identical problem. He sent the unit to myself, and after inspecting it the Serial GND line was left floating. Whether this was a cable he made himself or whether is was a pre-built cable it doesn't really matter. After grounding this wire, the connection worked fine. Your situation might not be exactly the same, but it is similar enough that I would recommend opening up that cable to see what is actually wired up to what.

If you need help or you have any questions about the documentation let me know. I'm fairly confident we can fix this, it is just going to require a more detailed look at the connections.

Author:  NotSoTragicHero [ Tue Mar 16, 2010 5:39 am ]
Post subject:  Re: Issues with .NET CF Serial Communication

Here is the cable on the Enfora end.

Image

It looks as it is wired just fine...

Manual re-wiring is not really an option for us, as we will eventually have to deploy a minimum of 80 units. :|

QSI can create custom BOBs, correct? If so, and if this is the issue, this may be the route we have to take.

We've tried a number of different serial cables, all to no avail.

Author:  danieln [ Tue Mar 16, 2010 8:27 am ]
Post subject:  Re: Issues with .NET CF Serial Communication

If Manual re-wiring is not an option, additional steps may have to be taken.

Go ahead and submit a Technical Support Request here:
http://www.qsicorp.com/support/contact/tech_support.php

Please include your email, and I will respond to it shortly giving an overview of the options available.

Author:  NotSoTragicHero [ Tue Mar 16, 2010 1:00 pm ]
Post subject:  Re: Issues with .NET CF Serial Communication

Sent. Thanks for all the help so far.

Page 1 of 1 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/