issue with Bluetooth serial communication

J

Jay

Hello All,

I notice I have a problem with an application I am writing where a
form starts up and opens a Bluetooth serial communication (COM8 on my
Verizon 6700 device).

I open the port on form.Activate(), and close the port on
form.DeActivate(). The communication works great if I manually open
and close the form or if the form is DeActivated from another form
coming up in front of my form (example is if a appointment
notification comes up from outlook). The problem I am seeing is if the
pocket PC goes into suspend mode and I turn it back on by pushing the
power button. After the device suspends, the OnDataReceived event
doesn't raise ( I thought it would reactivate when the form came back
to front). I also saw an IO exception for mscorlib on the debug
output. it does not work even if I exit the application, I have to
soft reset the device for it to work correctly. Do I need to somehow
catch a different event like a system suspend event?..

thanks in advance.


here is my code


--------------------------------------------------------

private void PortClose()
{
if (serialPort1 != null && serialPort1.IsOpen)
serialPort1.Close();
}





private void OpenPort()
{
try
{


//label1.Text = string.Format("baudrate:{0}, portname:
{1}, parity:{2} ", serialPort1.BaudRate, serialPort1.PortName,
serialPort1.Parity);
if (serialPort1 == null)
{
serialPort1 = new
System.IO.Ports.SerialPort("COM8", 38400, System.IO.Ports.Parity.None,
8, System.IO.Ports.StopBits.One);
serialPort1.ReceivedBytesThreshold = 82;
}

if (!serialPort1.IsOpen)
{
serialPort1.Open();
}
}
catch (Exception x)
{
System.Diagnostics.Debug.WriteLine(x.Message +
x.StackTrace);
}
}



private void OnDataReceived(object sender,
System.IO.Ports.SerialDataReceivedEventArgs e)
{
if (DateTime.Now < _eventTime)
return;

string mrc_data = serialPort1.ReadExisting();

}


private void frmSetup_Deactivate(object sender, EventArgs e)
{
this.PortClose();
}

private void frmSetup_Activated(object sender, EventArgs e)
{
this.OpenPort();
}


private void frmSetup_Load(object sender, EventArgs e)
{


this.OpenPort(); // open comm port here and
form.Activated, it will not create new if it exists.
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top