Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bluetooth 使用线程的连续蓝牙C程序_Bluetooth_Pthreads_Continuous - Fatal编程技术网

Bluetooth 使用线程的连续蓝牙C程序

Bluetooth 使用线程的连续蓝牙C程序,bluetooth,pthreads,continuous,Bluetooth,Pthreads,Continuous,我正试图通过蓝牙发送我的测量数据(温度、湿度和压力),它必须是连续的,所以我使用线程,但它对我来说太复杂了 以下是蓝牙客户端代码: #include "defines.h" void L2CAP_client(char *b_addr, float *temp, float *humi, float *pressure) { struct sockaddr_l2 addr = { 0 }; struct Measurement_Data

我正试图通过蓝牙发送我的测量数据(温度、湿度和压力),它必须是连续的,所以我使用线程,但它对我来说太复杂了

以下是蓝牙客户端代码:

    #include "defines.h"

    void L2CAP_client(char *b_addr, float *temp, float *humi, float *pressure)
    {
        struct sockaddr_l2 addr = { 0 };

        struct Measurement_Data *Thread_Data = malloc(sizeof(struct Measurement_Data));
        int s, status;
        char dest[18];

        pthread_t Measurement_data_thread = 0;
        int  iret = 0;
        pthread_t Thread_id;

        strncpy(dest, b_addr, 18);

        // allocate a socket
        s = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
        Thread_Data->s_ptr = s;

        // set the connection parameters (who to connect to)
        addr.l2_family = AF_BLUETOOTH;
        addr.l2_psm = htobs(0x1001);
        str2ba( dest, &addr.l2_bdaddr );

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

        Thread_Data->temp = *temp;
        Thread_Data->humi = *humi;
        Thread_Data->pressure = *pressure;

        // send a message
        if( status == 0 ) {

            iret = pthread_create(&Measurement_data_thread, NULL, &Thread_Function,    (void*) &Thread_Data);
        if(iret != 0){
        perror("Thread creation failed\n");
       }
    Thread_id = pthread_self();
    printf("ID of Temp_thread is: %u\n", (unsigned int)Thread_id);

}

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

    printf("Thread 1 returns: %d\n",iret);

    pthread_join(Measurement_data_thread, NULL);


    close(s);
 }

void *Thread_Function(void* Measurement_Data)
{
    int s = 0;
    struct Measurement_Data *Thread_Data = (struct Measurement_Data*) Measurement_Data;

    s = Thread_Data->s_ptr;
    send(s, &Thread_Data, sizeof(Thread_Data), 0);
    free(Thread_Data);

    //return 0;
}
它只发送了零,我也收到了这个错误:*检测到glibc*/anturi\u luku:munmap\u chunk():无效指针:0xbef48738***


我需要使用互斥锁来锁定数据,对吗

Valgrind在这种情况下是一个非常有用的工具。你能用Valgrind吗?你能分享一下瓦尔格林报告吗

确保使用“db attach”选项,因为Valgrind将在显示的每个错误后暂停并打印该行。尝试使用以下命令,将a.out替换为文件名


$valgrind--tool=memcheck--db attach=yes./a.out

以下是来自客户端的valgrind报告:

==2157== Memcheck, a memory error detector
==2157== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==2157== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==2157== Command: ./anturi_luku
==2157== 
Temperature: 22.7C
Humidity: 61.7%
Temperature: 22.7C
Humidity: 61.7%
pressure= 0, 0, 0
Pressure: 0.0 Pa
ID of Temp_thread is: 67241968
Thread 1 returns: 0
==2157== Thread 2:
==2157== Invalid free() / delete / delete[] / realloc()
==2157==    at 0x48348B8: free (vg_replace_malloc.c:427)
==2157==    by 0x8C0B: Thread_Function (L2CAP_client.c:89)
==2157==    by 0x485FBFB: start_thread (pthread_create.c:306)
==2157==    by 0x4962967: ??? (clone.S:116)
==2157==  Address 0xbde575e8 is on thread 1's stack
==2157== 
==2157== 
==2157== ---- Attach to debugger ? --- [Return/N/n/Y/y/C/c] ---- n
==2157== 
==2157== HEAP SUMMARY:
==2157==     in use at exit: 16 bytes in 1 blocks
==2157==   total heap usage: 2 allocs, 2 frees, 152 bytes allocated
==2157== 
==2157== LEAK SUMMARY:
==2157==    definitely lost: 16 bytes in 1 blocks
==2157==    indirectly lost: 0 bytes in 0 blocks
==2157==      possibly lost: 0 bytes in 0 blocks
==2157==    still reachable: 0 bytes in 0 blocks
==2157==         suppressed: 0 bytes in 0 blocks
==2157== Rerun with --leak-check=full to see details of leaked memory
==2157== 
==2157== For counts of detected and suppressed errors, rerun with: -v
==2157== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 17 from 6)

很高兴您能够通过ARM以C语言获得蓝牙L2CAP客户端的Valgrind报告

如您所见,当第一个错误发生时,将询问您以下问题

----是否附加到调试器?--[返回/否/是/是/抄送/抄送]----

按Ret或N Ret或N Ret会导致Valgrind不会为此错误启动调试器

按下Y Ret或Y Ret可使Valgrind在此时启动程序的调试器

从日志中可以看出,使用了“n”。你能试着用“Y”吗


此外,从日志来看,它似乎传达了无效释放已完成的信息。与之内联的是,似乎对未分配的内存调用了free()。检查它。

现在我得到的错误信息消失了。这是pthread_创建函数的第四个参数。我删除了符号和字符。现在函数如下所示:pthread_create(&Measurement_data_thread,NULL,&thread_函数,(void*)thread_data)

但它仍然没有发送或接收任何数据。当我使用printf检查发送端thread_函数上要发送的数据值时,它们是正常的。但在展示结束时,它有时只打印零,有时什么也不打印


我现在需要互斥锁来锁定数据吗?

感谢您的快速回复!因为valgrind显然不支持arm架构,所以有没有其他方法来处理这种情况。我得到了这个错误:valgrind:无法启动平台“arm linux”的工具“memcheck”:没有这样的文件或目录–edit:Raspberry Pi似乎也有valgrind。这很奇怪,因为我分配了两次内存,而且从我的代码中可以看到,我释放了两次内存。那么,我是在分配还是释放错误?
==2157== Memcheck, a memory error detector
==2157== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==2157== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==2157== Command: ./anturi_luku
==2157== 
Temperature: 22.7C
Humidity: 61.7%
Temperature: 22.7C
Humidity: 61.7%
pressure= 0, 0, 0
Pressure: 0.0 Pa
ID of Temp_thread is: 67241968
Thread 1 returns: 0
==2157== Thread 2:
==2157== Invalid free() / delete / delete[] / realloc()
==2157==    at 0x48348B8: free (vg_replace_malloc.c:427)
==2157==    by 0x8C0B: Thread_Function (L2CAP_client.c:89)
==2157==    by 0x485FBFB: start_thread (pthread_create.c:306)
==2157==    by 0x4962967: ??? (clone.S:116)
==2157==  Address 0xbde575e8 is on thread 1's stack
==2157== 
==2157== 
==2157== ---- Attach to debugger ? --- [Return/N/n/Y/y/C/c] ---- n
==2157== 
==2157== HEAP SUMMARY:
==2157==     in use at exit: 16 bytes in 1 blocks
==2157==   total heap usage: 2 allocs, 2 frees, 152 bytes allocated
==2157== 
==2157== LEAK SUMMARY:
==2157==    definitely lost: 16 bytes in 1 blocks
==2157==    indirectly lost: 0 bytes in 0 blocks
==2157==      possibly lost: 0 bytes in 0 blocks
==2157==    still reachable: 0 bytes in 0 blocks
==2157==         suppressed: 0 bytes in 0 blocks
==2157== Rerun with --leak-check=full to see details of leaked memory
==2157== 
==2157== For counts of detected and suppressed errors, rerun with: -v
==2157== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 17 from 6)