1

I have device that can connect to my laptop via blue-tooth as COM5. The device has a Pulse sensor. I want to draw data coming from sensor to graph. However when i connected to COM5 the serialport_Datarecieved event is not triggered. I tried device using matlab. It takes and draws data but i cant get data in c#. I checked the connection status of device and it is ok. I tried to change DtrEnabled and RtsEnapled properties but not worked.

    private void Form1_Load(object sender, EventArgs e)
    {

        cmbPortList.Items.AddRange(SerialPort.GetPortNames());
        cmbPortList.Sorted = true;
        cmbPortList.SelectedIndex = 0;


        this.serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);

    }

    private void btnOpenPort_Click(object sender, EventArgs e)
    {
        try
        {
            serialPort1.PortName = cmbPortList.Text;
            serialPort1.BaudRate = 9600;
            serialPort1.DataBits = 8;
            serialPort1.ReadTimeout = 500;
            serialPort1.WriteTimeout = 500;
            serialPort1.Handshake = Handshake.None;

            if (!serialPort1.IsOpen)
            {
                btnRun.Enabled = true;
                serialPort1.Open();
            }

        }
        catch (Exception ex)
        {
            serialPort1.Close();
        }
    }
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        while (serialPort1.BytesToRead > 0)
        {
            Thread.Sleep(50);
            byte[] buffer = new byte[serialPort1.BytesToRead];
            serialPort1.Read(buffer, 0, buffer.Length);
        } 

    }

I cant read any data in buffer. There is led is flashing while device is not connected with via blue-tooth. So i am absolutely sure i connected to device. Is problem about Bluetooth or code? Should i use another library to communicate blue tooth device? I have read links below. SerialPort fires DataReceived event after close SerialPort not receiving any data

Community
  • 1
  • 1
Blast
  • 955
  • 1
  • 17
  • 40

2 Answers2

0

This may have less to do with the SerialPort and more to do with the way that Winforms threads are interacting with the serial port's background worker threads. See the solution to this for more info.

Thabo
  • 329
  • 3
  • 6
  • In timer (100ms) i used serialport.Write("Blast"); That triggers the serialport_DataRecieved event in each tick and i can read data that coming from Pulse sensor. However when i use serialport.Write("AbcDeFGgHzxY"); that is not trigger the event. I really can't understand the logic. Do we have to write an initial data to port everytime? If yes that is not good because i need absolutely correct data. Maybe i should use RecievedBytesTreshold. I am really confused.. – Blast Aug 30 '13 at 08:58
  • Weird, definitely something I haven't seen, but then again, I'm not doing bluetooth with my serial port. You can try this [.NET serial port host library](https://bitbucket.org/thabofletcher/serialhost) (from the Package Manger Console: Install-Package AgingMonarch) and see if it gives you a different behavior. This is the [usage](https://bitbucket.org/thabofletcher/serialhost/src/48dab545ec51bd05ed10bf06ae9b4729d221970c/Console/Program.cs?at=default) – Thabo Sep 01 '13 at 06:10
  • 1
    I think the designer of the circuit requests data with 's'. It must be about its protocol or hex code. I have found that code in matlab sample of circuit % Request Data fprintf(s,'s'); That's why i can read data when i use serialport.Write("Blast"); Also i tried all letters. Only 's' triggers the event. – Blast Sep 02 '13 at 09:19
0

I think the designer of the circuit requests data from device with 's'. It must be about its protocol or hex code. I have found that code in matlab sample of circuit % Request Data fprintf(s,'s'); That's why i can read data when i use serialport.Write("Blast"); Also i tried all letters. Only 's' char triggers the event.

Blast
  • 955
  • 1
  • 17
  • 40