Reputation: 82517
I have to do a communication over a serial port. I am trying to do it now to allow Bluetooth communication via 2 devices and am getting nowhere.
I have a Application on the devices (called Bluetooth Explorer) that allows me to do serial communication and it will be sent over Bluetooth, using the Stonestreet One stack in case you are wondering :(
In the settings, I can create a "Service" that has a COM Port defined.
So I then try to run the following code (I call Write on one device and Read on another device). BTExplorer launches a pairing app when serialPort.Open()
is executed. In that I select what "service" I want to use (Serial Port 1).
But the serialPort.ReadLine()
hangs and never returns. (I mean REALLY hangs. I have to warm boot the device to kill my app. End process/kill process does not work.)
Here is the code for 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 get this working?
I am using Windows Mobile 5 with MC70 Devices. The Bluetooth Stack is Stonestreet One (can't change that sadly). Developing in C# Compact Framework .NET 3.5
Upvotes: 2
Views: 6111
Reputation: 56123
The API help says,
By default, the ReadLine method will block until a line is received. If this behavior is undesirable, set the ReadTimeout property to any non-zero value to force the ReadLine method to throw a TimeoutException if a line is not available on the port.
... so, do that if you want to avoid it hanging.
Anyway:
SerialPort
properties (e.g. BaudRate
etc.) before you call the Open method?ReadLine
or the WriteLine
?
Instead of doing open/write/close and open/read/close, how about doing open/open/read/write/close/close instead?BytesToRead
property return after the sender has invoked WriteLine
?If their SDK does have a sample program, then I suggest you use it (unaltered) to verify your test setup (e.g. to verify that your devices are connecting properly), before you alter the sample program and/or before you test your own software (using the same devices/test setup which you've already tested with their sample software).
Upvotes: 5