简体   繁体   English

Arduino和C ++串行通信同步

[英]Arduino and C++ Serial communication synchronization

I use the SerialClass.h and Serial.cpp in this link: http://playground.arduino.cc/Interfacing/CPPWindows 我在以下链接中使用SerialClass.h和Serial.cpp: http ://playground.arduino.cc/Interface/CPPWindows

My main.cpp: 我的main.cpp:

#include <stdio.h>
#include <tchar.h>
#include "SerialClass.h"    // Library described above
#include <string>

// application reads from the specified serial port and reports the collected data
int main(int argc, _TCHAR* argv[])
{

    printf("Welcome to the serial test app!\n\n");

    Serial* SP = new Serial("COM4");    // adjust as needed

    if (SP->IsConnected())
        printf("We're connected\n");

    char incomingData[256] = "hello";
    int dataLength = 255;
    int readResult = 0;

    while(SP->IsConnected())
    {
        readResult = SP->ReadData(incomingData,dataLength);
        incomingData[readResult] = 0;

        if(readResult != 0){
            printf("%s",incomingData);
             printf("---> %d\n",readResult);
        }
        Sleep(500);
    }
    return 0;
}

My arduino code: 我的arduino代码:

int mySize = 5;
char incomingData[256] = "hello";

void setup (){
  Serial.begin(9600); // Seri haberleşmeyi kullanacağımızı bildirdik
  pinMode(LedPin, OUTPUT); //LedPini çıkış olarak tanımlıyoruz.
}

void loop (){

    incomingData[mySize] = 't';
    ++mySize;

    Serial.write(incomingData);

    delay(500);
}

Arduino writes character array and C++ reads it. Arduino编写字符数组,而C ++读取它。 The issue is sometimes cpp is missing the data. 问题是有时cpp缺少数据。 My output: 我的输出:

输出

My First question is what can I do for this? 我的第一个问题是我该怎么做? How to do synchronization between Arduino and C++? 如何在Arduino和C ++之间进行同步? C++ should wait, until arduino to finish writing. C ++应该等待,直到arduino完成编写。 I think I should use lock system or something like that. 我想我应该使用锁系统或类似的东西。

And other question. 还有其他问题。 I want to my Arduino and C++ program are constantly communicating. 我想让我的Arduino和C ++程序不断交流。 I want to make that: "Arduino writes" After "C++ reads" after "C++ writes" after "Arduino reads" after again "Arduino writes". 我想做的是:“ Arduino写”在“ C ++读”之后,在“ C ++写”之后,在“ Arduino读”之后又是“ Arduino写”。 So, I don't use sleep and delay. 因此,我不使用睡眠和延迟。 My Second question is how can I do for this synchronization? 我的第二个问题是如何进行同步? I think answer is same like the answer of the first question. 我认为答案与第一个问题的答案相同。

The C++ class you are using doesn't implements own internal buffers, it relies on hardware buffer and OS driver buffer. 您使用的C ++类没有实现自己的内部缓冲区,它依赖于硬件缓冲区和OS驱动程序缓冲区。 OS driver buffer could be increased (Device Manager -> Ports -> driver properties -> Port Settings) 可以增加OS驱动程序缓冲区(设备管理器->端口->驱动程序属性->端口设置)

在此处输入图片说明

There is Sleep(500) delay in your receiving code. 您的接收代码中存在Sleep(500)延迟。 Now imagine that during such a 500ms delay UART hardware and software driver buffers are filled. 现在想象一下,在500ms的延迟时间内,UART硬件和软件驱动器缓冲区已满。 But your code is 'sleeping' and didn't read buffered data. 但是您的代码“正在休眠”,并且没有读取缓冲的数据。 Any data received during this period will be discarded. 在此期间收到的任何数据都将被丢弃。 Since Windows is not real-time OS, from time to time your windows process didn't get enough time-slice (because there are many other processes) and during such extended inactivity data could be lost. 由于Windows不是实时操作系统,因此Windows进程有时会没有足够的时间切片(因为还有许多其他进程),并且在这种长时间的不活动状态下,数据可能会丢失。 So remove that Sleep(500) . 因此,删除该Sleep(500)

To ensure reliable communication, receiving part must buffer the data as soon as it detects new data (usually in separate thread, could have higher priority). 为了确保可靠的通信,接收部分必须在检测到新数据后立即缓冲数据(通常在单独的线程中,可能具有更高的优先级)。 Main processing logic shall work with that buffered data. 主处理逻辑应与该缓冲数据一起工作。

Also you shall implement some kind of protocol, with at least following 2: 另外,您还应实施某种协议,至少应遵循以下2项:

  • message format (starting, ending, size etc) 消息格式(开始,结束,大小等)
  • message integrity (is data received without corruption, could be simple checksum) 消息完整性(接收到的数据没有损坏,可以是简单的校验和)

Also some kind of transmission control would be nice (timeouts, reply / acknowledge if any). 同样,某种传输控制也会很好(超时,回复/确认(如果有))。

UPD: in Arduino code Serial.write(incomingData); UPD:用Arduino代码Serial.write(incomingData); make sure that incomingData is properly zero-terminated. 确保传入数据正确以零结尾。 And add upper bound check for mySize... 并为mySize添加上限检查...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM