简体   繁体   English

串行通讯问题

[英]Serial communication issue

I'm making a project which works with an Arduino via USB. 我正在制作一个通过USB与Arduino兼容的项目。 I'm using nearly the same code which was in the library I am using. 我正在使用几乎与正在使用的库中相同的代码。

The code reads a .txt file, and sends this data to the Arduino in an infinite loop. 该代码读取一个.txt文件,并在无限循环中将此数据发送到Arduino。 The problem is that after the 165th loop, it really slows down. 问题在于,在第165次循环之后,它确实放慢了速度。 It takes the connection 6 seconds to send 6 characters. 连接需要6秒钟才能发送6个字符。 Always at 165. Here is the code: 始终为165。代码如下:

while(SP->IsConnected()) {
  //see if the usb connection is on.
  fstream file;
  file.open("c:/Python27/beki.txt");
  for(int i=0;i<6;i++) {
    file >> incomingData[i];
    cout << incomingData[i];
  }
  file.close();
  cout <<szam << "\n";
  SP->WriteData(incomingData,dataLength);
  szam++; //counting, thats why i know its always slows at 166.
  Sleep(200);
}

I figured out that it's not the file, and it does not eat much memory, 335k. 我发现它不是文件,并且不会占用太多内存,即335k。 And it sends good data, so until the 165th loop, it does its work perfectly. 并且它发送良好的数据,因此直到第165次循环之前,它都能完美地工作。 The file is written by another infinite loop, and after this 165th loop, the code sends old data's characters. 该文件由另一个无限循环写入,在第165次循环之后,代码发送旧数据的字符。

It did the same in Python. 在Python中也是如此。 I don't know if there's a limit or something. 我不知道有没有限制。 Please help. 请帮忙。

I'm using this library . 我正在使用这个图书馆

You need to bring the following outside of your while loop: 您需要将以下内容带到while循环之外:

fstream file;
file.open("c:/Python27/beki.txt");

and

file.close();

As it is now, you are probably sending the same bytes after opening the file each time. 现在,每次打开文件后,您可能发送相同的字节。

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

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