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 mobile data terminal running Windows CE, which is connected, via RS232, to a GPS/GSM unit. When I connect this GSM 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.
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;
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("COM2", 115200, Parity.None, 8, StopBits.One);
port.NewLine = "\r\n";
port.RtsEnable = true;
port.DtrEnable = true;
port.ReceivedBytesThreshold = 1;
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.ErrorReceived += new SerialErrorReceivedEventHandler(port_ErrorReceived);
port.Open();
if (port.IsOpen)
{
textBox_Rec.Text += "Connected to " + port.PortName + Environment.NewLine;
}
}
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)
{
port.Write(textBox_Send.Text + port.NewLine);
textBox_Send.Text = "";
}
}
}
When I send an AT command to the device, the DataReceived event never fires. I've also tried setting up a tick to try and capture some return data, yet I still get nothing. I can rule out the data sending as the problem, as I can send a message, via SMS, that will cause the device to return data, yet still, nothing happens.
Any help would be appreciated. Thanks!
-Brandon
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.