.NET Components for Mobility

Can't read serial port

Last post 04-25-2008 12:24 PM by alanjmcf. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 04-21-2008 9:27 AM

    • Avalon
    • Top 500 Contributor
    • Joined on 04-21-2008
    • Posts 3

    Can't read serial port

    Hello,

    I wrote a simple application in C# to try and set up a bluetooth connection and set up a serial port service. I want to find devices around me and ask for their GPS information, this should be available trough their com ports after GPGGA in the string that i'd retreive from the com ports.

    When i run this application it will tell me that it cannot make a SerialPort object (No ioport opened: ... IO.IOException ). I tried alot and read trough forums but i cant get this fixed, some devices ask if we want to activate serial communication with the device, we say yes but it wont open an IO port. Sometimes it DOES manage to open an IO Port but when it returns the info we just receive an empty string. Lets just say that what im doing right now does not work.

    If anyone could help me on this error, it would help me, and probably some other people trying to do serial communication as well.

    I tried the following:

    using System;

    using System.Linq;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using InTheHand.IO.Ports;

    using InTheHand.Net;

    using InTheHand.Net.Bluetooth;

    using InTheHand.Net.Forms;

    using InTheHand.Net.Sockets;

    using System.Windows.Forms;

    using System.IO.Ports;

    namespace SmartDeviceProject3

    {

    public partial class Form1 : Form

    {

    string labeltext;

    BluetoothDeviceInfo[ devices;

    BluetoothSerialPort btSerialPort;

    BluetoothClient btClient = new BluetoothClient();

    public Form1() {

    InitializeComponent();

    labeltext =
    "";

    //List all bluetooth devices

    setLabel("Searching Devices");

    devices = btClient.DiscoverDevices();

    setLabel("Found " + devices.Length + " devices");

    foreach (BluetoothDeviceInfo dev in devices) {

    setLabel("Connecting Device: " + dev.DeviceName);

    BluetoothEndPoint ep = new BluetoothEndPoint(dev.DeviceAddress, BluetoothService.SerialPort);

    //Set a standard pin when devices require authentication

    if (!dev.Authenticated) {InTheHand.Net.Bluetooth.BluetoothSecurity.SetPin(dev.DeviceAddress, "1234");

    }

    //Check whether the socket is available, close if unavailable

    if (btClient.Connected) {

    btClient.Close();

    }

    //Try to connect

    try{

    btClient.Connect(ep);

    }
    catch (Exception e) { setLabel("No connect() possible: " + e);

    }

    //Try to find an available com port on this system and create it

    int portNumber = 0;

    while (btSerialPort == null) {

    try {btSerialPort = BluetoothSerialPort.CreateClient("COM", portNumber, ep);

    }

    catch {

    portNumber++;

    }

    }

    //Create a virtual SerialPort over the bluetooth serial port

    setLabel("BTPort Opened: " + btSerialPort.PortName);

    SerialPort ioport = new SerialPort(btSerialPort.PortName);

    try {

    ioport.Open();

    }

    catch (Exception e) {setLabel("No ioport opened: " + e);

    }

    //When the ioport is open, try to read al existing data and print it

    if (ioport.IsOpen) {

    setLabel("SerialPort Opened with: " + dev.DeviceName);

    //Read all data and split it then store it into an array

    string ioinfo = ioport.ReadExisting();

    setLabel("info= " + ioinfo);

    string[ strArr = ioinfo.Split('$');

    //Iterate the array and look for GPS info (GPGGA in the string)

    foreach (string str in strArr) {

    string[ lArr = str.Split(',');

    if (lArr[0] == "GPGGA") {

    setLabel("Found GPS info!!");

    return;

    }

    }

    //Close ports

    ioport.Close();

    btClient.Close();

    }

    btSerialPort.Close();

    }

    }

    //Print a new line in the label

    private void setLabel(string lText)

    {

    labeltext = labeltext +
    "\n" + lText;

    label1.Text = labeltext;

    label1.Invalidate();

    }

    }

    }

  • 04-21-2008 1:32 PM In reply to

    Re: Can't read serial port

    In brief, I would switch over to using BluetoothClient alone instead.  Remove all the code to do with COM ports and just communicate using the BluetoothClient socket i.e. using

    NetworkStream peer = cli.GetStream();
    peer.Write(...)
    peer.Read(...)

    You'll find that way very much simpler and very much more robust.  Its also easier to tell if the device has gone out of range/turned off etc.

     

    Also, with the code as it stands, I'd expect that having the BluetoothClient connection open would prevent a COM port connection from working at all: Bluetooth (sadly) only allows one connection to a particular service from each peer, so one would block the other.

    Anyway we're depreciating the BluetoothSerialPort class as the undellying native API is depends on is very unreliable -- as you've found :-( -- it doesn't work at all on many device types, and when it does work in general it is unreliable in practice.

    We've got another method of creating Bluetooth virtual serial ports, but using serial ports is only to be recommended if a separate application needs to access the serial port.  BluetoothClient is better in all other cases.

    Alan J. McFarlane
    http://www.alanjmcf.me.uk/
    Please follow-up in the newsgroup for the benefit of all.
    Have I helped? Consider visiting my Amazon wishlist, see my homepage.
  • 04-25-2008 5:10 AM In reply to

    • Avalon
    • Top 500 Contributor
    • Joined on 04-21-2008
    • Posts 3

    Re: Can't read serial port

    OK, ill dive into that.

    I dont have any experience using streams but ive got much people around me to help me out. Ill figure something out.

    Thanks for your help!

  • 04-25-2008 6:30 AM In reply to

    • Avalon
    • Top 500 Contributor
    • Joined on 04-21-2008
    • Posts 3

    Re: Can't read serial port

    Hello,

    I tried your solution, and i got to this in my code.

    Once i set up a connection and paired succesfully i do this:

     

    if (btClient.Connected)

    {

    peer = btClient.GetStream();

    peer.Read(bufr, 1, 5);

    setLabel("Stream:" + bufr[0].ToString());

    peer.Close();

    btClient.Close();

    }

     

    setLabel is a function to print something on a label for debugging. This piece of code results in having a long long loading time and then shuts down, when i take the peer.read(.... out it wont load this long and make succesfull connections.

    I also tried a StreamRead and a StringBuilder to make a string out of my stream, same results.

    Any suggestions on what ive been doing wrong?

    Again thanks for your help!

  • 04-25-2008 12:24 PM In reply to

    Re: Can't read serial port

    So, as soon as you connect you expect the device to send you back (upto) five bytes of data.  Correct?

    Are you saying that when you run that code that your application blocks at the Read call?  If so, then the device hasn't sent anything...  If it has sent any number of bytes then Read would return.  (Note that Read will return if at least one byte has been read, you always must check the return value of Read to see whether you got all the bytes you wanted).

    I'd probably start by playing with the SdpBrowser sample app.  When connected, it has a background thread that writes all received characters to the screen.  See if it dumps anything in your case.

     

    You don't need to send some command to the device to make is send some data?

    Finally if the Read could take a 'long' time to complete you need to put it in a background thread so that the UI can continue to refresh, however we'll deal with that later...

    Alan J. McFarlane
    http://www.alanjmcf.me.uk/
    Please follow-up in the newsgroup for the benefit of all.
    Have I helped? Consider visiting my Amazon wishlist, see my homepage.
Page 1 of 1 (5 items)
Copyright © 2001-2008 In The Hand Ltd. All rights reserved. Terms of Use and Privacy Policy.