Bluetooth Datenübertragung schnell

Gast #4104990
Lesenswert?

Hallo zusammen,

Ziel
ich möchte mit meinem Handy via Bluetooth sehr schnell Daten zu meinem 
PC senden, das heißt etwas alle 5-10 ms 40Byte.

Bisher
Die Kommunikationsstrecke ist über Bluetooth realisiert. Am PC habe ich 
ein USB-UART-Bluetooth-Dongle mit einer Baudrate von 920600.

Derzeit sende ich nur ein Byte, welches ich mit jedem senden 
ikrementiere,
also 0,1,2,3,4,5,...,254,255,0,1,2,3,...

Die Daten verifiziere ich am PC, indem ich eifach schaue, ob das neue 
Byte um 1 größer ist als das vorherige Byte.

Das Problem
Beträgt die Sendefrequenz >= 1/40ms entstehen keine Fehler!! GUT
Beträgt die Sendefrequenz < 1/40ms entstehen Fehler (ca.1 mal pro 4 sec)

woran kann das liegen?

Anbei der relevante code (Android)

1
private Handler handler = new Handler();
2

3
protected void onCreate(Bundle savedInstanceState)
4
{
5
handler.postDelayed(runnable, 100);
6
}
7
long temp=0, Fehler;
8
    byte test=0;
9
    private Runnable runnable = new Runnable() {
10
        @Override
11
        public void run() {
12
            if(false == _singleton.KILL_APP)
13
            {
14
                if (null != mConnectedThread)
15
                {
16

17
                    testCounter[0] = (byte) (testCounter[0] + 1 & (byte) 0xFF);
18
                    mConnectedThread.write((testCounter));    // Send string via Bluetooth
19

20
                    if ((byte) ((testCounter[0] - 1) & 0xFF) == test)
21
                    {
22

23
                    }
24
                    else
25
                    {
26
                        Log.i("MyView", "Fehler=" + (Fehler++));
27
                    }
28
                    test = testCounter[0];
29

30
                   //Log.i("MyView", "Time Between Clicks=" + (temp - System.currentTimeMillis()));
31
                }
32
                else
33
                {
34

35
                }
36
      /* and here comes the "trick" */
37
                handler.postDelayed(this, global.eConnection.TimeDiff);
38
                temp = System.currentTimeMillis();
39
            }
40
        }
41
    };

Anbei der relevante code (C#)
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Linq;
7
using System.Text;
8
using System.Windows.Forms;
9
using System.IO.Ports;
10
using System.Diagnostics;
11

12
namespace bluetoothTest
13
{
14
    public partial class Form1 : Form
15
    {
16
        byte inputData;
17
        byte intZahl_neu, intZahl_alt;
18

19
        int intZahl_fehler;
20

21
        Stopwatch watch = new Stopwatch();
22
        
23
        public Form1()
24
        {
25
            InitializeComponent();
26
        }
27

28
        private void Form1_Load(object sender, EventArgs e)
29
        {
30
            serialPort1.PortName = "COM13";
31
            serialPort1.BaudRate = 920600;
32

33
            serialPort1.Parity = Parity.None;
34
            serialPort1.DataBits = 8;
35
            serialPort1.StopBits = StopBits.One;
36
            serialPort1.Handshake = Handshake.None;
37
            //serialPort1.RtsEnable=true;
38
            serialPort1.DtrEnable = true;
39

40
            serialPort1.Open();
41
        //    serialPort1.Write("Testnachricht");
42
        //    serialPort1.Close();
43
        }
44

45
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
46
        {
47
            watch.Reset();
48
            // watch.Start();
49
            //MessageBox.Show(serialPort1.ReadExisting());
50
            inputData = (byte)serialPort1.ReadByte(); //oder serialPort1.ReadLine 
51
            this.BeginInvoke(new EventHandler(DoUpdate));
52
        }
53
        private void DoUpdate(object s, EventArgs e)
54
        {
55

56
            intZahl_neu = inputData;
57

58
            if ((byte) ((intZahl_neu - 1) & 0xFF) == intZahl_alt)
59
            {
60

61
            }
62
            else
63
            {
64
                label1.Text = Convert.ToString(intZahl_fehler++);
65
            }
66
            intZahl_alt = intZahl_neu;
67

68
            richTextBox1.SelectionStart = richTextBox1.Text.Length; //Set the current caret position at the end
69
            richTextBox1.ScrollToCaret(); //Now scroll it automatically
70

71
            while (richTextBox1.Lines.Length > 10)
72
            {
73
                richTextBox1.SelectionStart = 0;
74
                richTextBox1.SelectionLength = richTextBox1.Text.IndexOf("\n", 0) + 1;
75
                richTextBox1.SelectedText = "";
76
            }
77
            //Hier die Funktion einfügen deren Zeit gemessen werden soll
78

79
            watch.Stop();
80
            Console.WriteLine("Time spent: " + watch.Elapsed.Milliseconds);
81
            richTextBox1.Text += inputData + "\r\n";
82
        }
83

84
      
85
    }
86
}

Viele Grüße,
Dieter
Gast #4105011
Lesenswert?

"The DataReceived event is not guaranteed to be raised for every byte 
received. Use the BytesToRead property to determine how much data is 
left to be read in the buffer." (msdn)
...da immer nur ein Byte auslesen, wäre da nicht schlau.

Ich würde außerdem ein
1
if (e.EventType == SerialData.Eof)
2
{
3
   return; // Eof ignorieren
4
}
...in die DataReceived dort als erstes reinpacken.
0x1A ist aus irgendwelchen Gründen als Eof-Char eingestellt und 
verursacht ein DataReceived mit SerialData.Eof - 0x1A ist aber nochmal 
bei DataReceived mit SerialData.Chars dabei ;D
Gast #4105025
Lesenswert?

Kleine Anmerkung noch am Rande:
"mit einer Baudrate von 920600"
Bei reiner Bluetooth-Kommunikation wird die Baudrate übrigens in der 
Regel völlig ignoriert und die maximal mögliche verwendet ;D

Aus der SPP-Spezifikation:
"DevA may inform DevB of RS232 port settings with the Remote Port 
Negotiation Command, directly before DLC establishment [...]
The information conveyed in the remote port negotiation procedure is 
expected to be useful only in type II devices (with physical serial 
port) according to section 1.2 in RFCOMM [4], or if data pacing is done 
at an emulated serial port interface for any reason. RFCOMM as such will 
not artificially limit the throughput based on baud rate settings, see 
RFCOMM [4] chapter 2."

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren