Windows Mobile - Serial Port
I need to communicate over a serial port. I am trying to do this now to allow bluetooth communication across 2 devices and am not going anywhere.
I have an app on devices (called Bluetooth Explorer) that allows me to do serial communication, and it will be sent over Bluetooth using the Stonestreet One stack, if you're interested :(
In the settings I can create a "Service" in which the COM port is installed.
So I then try to run the following code (I call Write on one device and read on another device). BTExplorer launches the pairing app when executed serialPort.Open()
. In this I choose which "service" I want to use (serial port 1).
But it serialPort.ReadLine()
freezes and never returns. (I mean it REALLY hangs. I need to warm up the device boot to kill my application. The process termination / kill doesn't work.)
Here's the code for your reference:
public void WriteSerial()
{
SerialPort serialPort = new SerialPort("COM4");
serialPort.Open();
serialPort.WriteLine("Hello To The Other Side");
serialPort.Close();
}
public void ReadSerial()
{
SerialPort serialPort = new SerialPort("COM4");
serialPort.Open();
string output = serialPort.ReadLine();
serialPort.Close();
MessageBox.Show(output);
}
private void btnWrite_Click(object sender, EventArgs e)
{
WriteSerial();
}
private void btnRead_Click(object sender, EventArgs e)
{
ReadSerial();
}
How to do it?
I am using Windows Mobile 5 with MC70 devices. Bluetooth stack - Stonestreet One (can't sadly change that). Development in C # Compact Framework .NET 3.5
source to share
By default, the ReadLine method will continue until a string is received. If this behavior is undesirable, set the ReadTimeout Property to any nonzero value to cause the ReadLine method to throw a TimeoutException if the line is not available on the port.
... so, do this if you want it not to hang.
Anyway:
- How do you know two devices / ports are connected to each other (via bluetooth)?
- Are you setting properties
SerialPort
(eg,BaudRate
etc.) before calling the Open method? - Is there any difference you call first,
ReadLine
orWriteLine
? Instead of opening / writing / closing and opening / reading / closing, how about opening / opening / reading / writing / closing / closing instead? - What does the receiver property return
BytesToRead
after the sender has sentWriteLine
? - Have you looked at the sample serial port code that exists (according to the Stonestreet One SDK FAQ )
If their SDK has a sample program, I suggest that you use it (unchanged) to verify your test setup (for example, to make sure your devices are connected properly), before changing the sample program and / or before testing (using those same devices / test setup you have already tested with your software).
source to share