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
Best Answer
The API help says,
... 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 theWriteLine
? 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 invokedWriteLine
?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).