Hello there,
I'm trying to write in C#, a Blue tooth/serial port application for a Windows CE 5.0 PDA so as to be able to write/read from an LMX9838 or LMX9820a bluetooth module. I have to admit I have become a bit stuck, and would immensely appreciate any help by anyone. I have had luck writing to a HyperTerminal window via an ABE UB20s from the PDA, but no luck writing back back from HyperTerminal. When attempting to connect with the LMX9820a bluetooth module, I am able to read out a connection and disconnect on the HyperTerminal, which amounts to a bit of gibberish, but am unable to read write to from either end. I'm curious if I am relegated to using a 2 COM ports upon the current iPAQ 110 (5:inbound / 6:outbound). Wouldn't I only need one? I've noticed testing out virtual serial ports on the PDA (eg: Mobile Terminal), they apparently use only one port and are successful from the PDA/LMX9820a interface. I've also noted some people have said using sockets is the way to go. Again, any help that might point me in the proper direction would be appreciated.
Thanks,
Jeff
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
using System.Threading;
namespace CSSP
{
public partial class Form1 : Form
{
SerialPort serialIn;
SerialPort serialOut;
Thread rcvThread;
public XmlTextReader waypoints;
private delegate void updateText(string s);
private delegate void closeDel();
private delegate void disconnectDel();
private string inboundPort = "";
private string outboundPort = "";
private string filename = "";
bool connect_or_disconnect = false;
bool closeRequested = false;
bool disconnectRequested = false;
LinkedList<dasWaypoints> linked_waypoints = new LinkedList<dasWaypoints>();
int numThreads = 0;
public Form1()
{
InitializeComponent();
lblStatus.Text = "Status: Ports not set.\r\nWaypoints not loaded.";
}
private void ReceiveData()
{
while (!closeRequested && !disconnectRequested)
{
try
{
string line = serialIn.ReadLine();
//lblStatus = line;
if (line.CompareTo("end") == 0)
{
disconnectRequested = true;
continue;
}
//txtLog1.Invoke(new updateText(UpdateText), line);
//here we have to do a switch statement
//speed,##.##
//*loc,##.##,##.##
//*sensor,#,#,#,#
//*loc~lat,lon
//sensor1-4 with a possible integer scale factor
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
if (closeRequested)
closeMe();
if (disconnectRequested)
this.Invoke(new disconnectDel(onDisconnect));
}
private void onDisconnect()
{
serialIn.Close();
serialOut.Close();
//mnuDisconnect.Enabled = false;
// mnuConnect.Enabled = false;
mnuSettings.Enabled = true;
// mnuGpx.Enabled = true;
lblStatus.Text = "Disconected.\r\nPorts not set.";
numThreads--;
}
private void closeMe()
{
numThreads--;
this.Invoke(new closeDel(this.Close));
}
private void Form1_Closing(object sender, CancelEventArgs e)
{
if (this.numThreads > 0)
{
e.Cancel = true;
}
else
{
e.Cancel = false;
if (serialIn != null && serialOut != null)
{
serialIn.Close();
serialOut.Close();
}
}
this.closeRequested = true;
}
private void checkBox2_CheckStateChanged(object sender, EventArgs e)
{
if (connect_or_disconnect == false)
{
connect_or_disconnect = true;
txtLog2.Enabled = true;
txtLog3.Enabled = true;
txtLog4.Enabled = true;
txtLog5.Enabled = true;
txtLog6.Enabled = true;
checkBox1.Enabled = true;
Logging.Enabled = true;
serialIn = new SerialPort(inboundPort);
serialOut = new SerialPort(outboundPort);
serialIn.ReadTimeout = 1000;
serialOut.ReadTimeout = 1000;
disconnectRequested = false;
try
{
if (!serialIn.IsOpen)
{
lblStatus.Text = "Input port closed. Opening input port...";
serialIn.Open();
}
if (!serialOut.IsOpen)
{
lblStatus.Text = "Output port closed. Opening output port...";
serialOut.Open();
}
lblStatus.Text = "Ports opened. Starting the listener thread...";
rcvThread = new Thread(new ThreadStart(ReceiveData));
numThreads++;
rcvThread.Start();
lblStatus.Text = "Listener thread started.";
// btnSend.Enabled = true;
lblStatus.Text = "Connected.\r\nInbound:" + inboundPort + "\r\nOutbound:" + outboundPort;
MessageBox.Show("Connected.");
//Here we must get the current GPS coordinates from the car to ascertain if an additional waypoint must be added
//if the car is not already @ point 1 +/- a few (To be determined) fractions of a degree lat/lon
//if at point 1, then add all points otherwise only points 2+ (must use pt 1 for angle calculation)
serialOut.WriteLine("connected\r");
serialOut.WriteLine("gps\r");
//here we should wait for the response
//must figure out what the format will be
//Will the formatting take place upon the car or pda
serialOut.WriteLine("compass\r");
//here we should wait for the response
//must figure out what the format will be
//Will the formatting take place upon the car or pda
mnuSettings.Enabled = false;
//XXXXXXXXXXX
StreamReader xmlfile = File.OpenText(filename);
waypoints = new XmlTextReader(xmlfile); //load in the selected file into the xml parser
while (waypoints.Read())
{
waypoints.MoveToElement();
if (waypoints.Name.ToLower().CompareTo("trkpt") == 0 && !(waypoints.Name.ToLower().CompareTo("\trkpt") == 0))
{
string latVal = "";
string lonVal = "";
// double Ang = 0;
waypoints.MoveToFirstAttribute();
for (int i = 0; i < waypoints.AttributeCount; i++)
{
if (waypoints.Name.ToLower().CompareTo("lat") == 0) latVal = waypoints.Value;
if (waypoints.Name.ToLower().CompareTo("lon") == 0) lonVal = waypoints.Value;
waypoints.MoveToNextAttribute();
}
if (latVal.Length > 1 && lonVal.Length > 1)
{
// waypoint_count++;
dasWaypoints temp = new dasWaypoints();
temp.latitude = double.Parse(latVal);
temp.longitude = double.Parse(lonVal);
temp.angle = 1;
linked_waypoints.AddFirst(temp);
// double olat = double.Parse(listView1.Items[listView1.Items.Count - 1].SubItems[0].Text);
// double olon = double.Parse(listView1.Items[listView1.Items.Count - 1].SubItems[1].Text);
// Ang = Math.Atan2((lon - olon), (lat - olat)) * 180 / 3.1415926535;
// MessageBox.Show("Ang: " + Ang);
}
}
}
// MessageBox.Show("count: " + linked_waypoints.Count);
//XXXXXXX
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
lblStatus.Text = "error.\r\nPorts not set.";
// mnuConnect.Enabled = false;
}
}else{
if (checkBox1.Checked == true)
{
serialOut.WriteLine("stop\r");
}
connect_or_disconnect = false;
txtLog1.Enabled = false;
txtLog2.Enabled = false;
txtLog3.Enabled = false;
txtLog4.Enabled = false;
txtLog5.Enabled = false;
txtLog6.Enabled = false;
Logging.Checked = false;
Logging.Enabled = false;
checkBox1.Checked = false;
btnSpeedDown.Enabled = false;
btnSpeedUp.Enabled = false;
btnDirUp.Enabled = false;
btnDirDown.Enabled = false;
btnDirLeft.Enabled = false;
btnDirRight.Enabled = false;
checkBox1.Enabled = false;
try
{
disconnectRequested = true;
serialOut.WriteLine("disconnected\r");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}