通过蓝牙进行连续数据传输 [英] Continuous data transfer over bluetooth

查看:163
本文介绍了通过蓝牙进行连续数据传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成0到100之间的随机数,并通过蓝牙将它们连续不断地从Raspberry Pi(运行Linux)传输到嵌入式(x86)PC(也运行Linux).我的C代码基于以下内容:-

I would like to generate random numbers between 0 and 100 and transfer them continuously over bluetooth from a Raspberry Pi (running Linux) to an embedded (x86) PC (also running Linux). My C code is based on the following: -

客户端:-

#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>

int main(int argc, char **argv)
{
    struct sockaddr_rc addr = { 0 };
    int s, status;
    char dest[18] = "01:23:45:67:89:AB";

    // allocate a socket
    s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);

    // set the connection parameters (who to connect to)
    addr.rc_family = AF_BLUETOOTH;
    addr.rc_channel = (uint8_t) 1;
    str2ba( dest, &addr.rc_bdaddr );

    // connect to server
    status = connect(s, (struct sockaddr *)&addr, sizeof(addr));

    // send a message
    if( status == 0 ) {
        status = write(s, "hello!", 6);
    }

    if( status < 0 ) perror("uh oh");

    close(s);
    return 0;
}

服务器端:-

#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>

int main(int argc, char **argv)
{
    struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
    char buf[1024] = { 0 };
    int s, client, bytes_read;
    socklen_t opt = sizeof(rem_addr);

    // allocate socket
    s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);

    // bind socket to port 1 of the first available 
    // local bluetooth adapter
    loc_addr.rc_family = AF_BLUETOOTH;
    loc_addr.rc_bdaddr = *BDADDR_ANY;
    loc_addr.rc_channel = (uint8_t) 1;
    bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));

    // put socket into listening mode
    listen(s, 1);

    // accept one connection
    client = accept(s, (struct sockaddr *)&rem_addr, &opt);

    ba2str( &rem_addr.rc_bdaddr, buf );
    fprintf(stderr, "accepted connection from %s\n", buf);
    memset(buf, 0, sizeof(buf));

    // read data from the client
    bytes_read = read(client, buf, sizeof(buf));
    if( bytes_read > 0 ) {
        printf("received [%s]\n", buf);
    }

    // close connection
    close(client);
    close(s);
    return 0;
}

虽然这对于单个字符串很有效,但是我无法传输整数.另外,对于连续的数据流,我是否只是将服务器和客户端运行在for循环中(例如),或者有更好的方法吗?

While this works well for a single string, I am unable to transfer integers. Also, for a continuous data stream, do I just have the server and client running in a for loop (for example) or is there a better way to do it?

推荐答案

实际上,您想通过BT RFCOMM套接字发送Integer类型的数据,但是您的基本示例要求发送string. 您的解决方案就是将您的数字转换为字符串(用C语言表示的char数组).

Actually you want to send an Integer type data over BT RFCOMM socket but your base example requires string to be send. Your solution is simply convert your number to string (char array in C language).

您的代码来自 Albert S. Huang 关于BT编程的宝贵作品 出色的BT编程教程

Your code comes from Albert S. Huang invaluable works on BT programming Great BT programming tutorial

将这些更改应用于您的代码.

Apply these changes to your code.

  // send
char integer[4];                  // buffer
*((int*)integer) = 73232;         // 73232 is data which I want to send.
//send( cs, integer, 4, 0 );        // send it

    // send a message to server
    if( status == 0 ) {
        status = write(s, integer, 4);
        if (status == 4){
            printf("Send data to server done\n");
        }
    }
    else 
        if( status < 0 ){ 
            perror("send message to server Failed\n");
    }

它向服务器发送73232.

Its send 73232 to server.

char integer [4];

bytes_read = read(client, integer, 4);

/*if( bytes_read > 0 ) {
    printf("received [%s]\n\n", buf);
}*/

printf("int: %x\n", integer[0]);//10
printf("int: %x\n", integer[1]);//1e
printf("int: %x\n", integer[2]);//1
printf("int: %x\n", integer[3]);//0

服务器成功接收到您号码的每个部分. 但是,正如您所看到的(我将其写为注释),它们以LE形式存储.

And server successfully receive every chunks of your number. But as you see (I write them as comment ) they are store in LE form.

(我们可以在每个数字前添加一个0开头的数字)

(we can add a leading 0 digit to every numbers)

提示:您可以使用在线十进制到十六进制转换器. dec_to_HEX

Hint: you can use an on line decimal to hex converter. dec_to_HEX

由于 endian .

在这一点上,您已经拥有它们并且可以执行其他一些操作.

And in this point you have them and can do some other operations.

如您在示例中所述,您的数字必须由随机函数生成,并且其返回值(您的随机数)是通过BT套接字发送的.

As you said in example, your number must generated by a random function and its return value (your random) is send over BT socket.

我基于堆栈答案的解决方案

这篇关于通过蓝牙进行连续数据传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆