文章

Send & Receive with one handle with NTCAN

Example code

snd_rcv_with_one_handle.c: 此源文件适用于Linux环境,调用Linux下的pthread生成两个thread,一个负责连续向总线发送CAN CC数据,一个负责接收并显示所有11-bit ID的CAN消息(此处虽然Interaction处于打开状态,但是在同一个process内的thread_snd()发送的消息是不会被自己接收到的)。此example code用来演示两个thread共享同一个handle的情形,因此具有相同的baud rate。发送thread结束后会修改全局变量letsquit 的值,接收thread一旦察觉此值被修改,也就一起退出thread,继而退出整个程序。

/* ############################################################### */
    pthread_t tid_snd;
    pthread_t tid_rcv;
    // create thread cc
    if (pthread_create(&tid_snd, NULL, thread_snd, &handle) != 0) {
        perror("cc pthread_create error");
        return (-1);
    }
	// create thread fd
    if (pthread_create(&tid_rcv, NULL, thread_rcv, &handle) != 0) {
        perror("fd pthread_create error");
        return (-1);
    }    
    // block main() until thread_rcv return
    if (pthread_join(tid_rcv, NULL)!=0){
        perror("pthread_join fail");
    }