Hy everybody!
i changed a sample program from a library to send my data to arduino.
it reads data from a file, and send it to arduino via usb.
i made this program first in python with pyserial, and it did the same as cpp does.
after 165th loop, it slows down.
the program runs 5 times /sec, each time it sends 6 characters, after 165th loop, it sends 1 char/sec, so its 30x slower. i think that the problem is with the serial communication, i hope somebody can help me.
the program does not have problems with the structure, it does its job perfectly until 165. loop.
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 tried it without file reading but with an array data predeclaration:
incomingData="01234";
it did the same.
it does its function perfectly until it 165th loop, after that, it slow down sending the data.
it sends only 1 character of the 6 from the array per second, and i have to restart the program to make it work again, for 165 loop.
here is the whole sender code:
#include <stdio.h>
#include <tchar.h>
#include "SerialClass.h"
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Serial* SP = new Serial("\\\\.\\COM35");
if (SP->IsConnected())
Sleep(1);
char incomingData[6] = "";
//printf("%s\n",incomingData);
int dataLength = 6;
int readResult = 0;
int szam=0;
while(SP->IsConnected())
{
fstream file;
file.open("c:/Python27/beki.txt");
for(int i=0;i<6;i++)
{
file >> incomingData[i];
}
file.close();
file.clear();
SP->WriteData(incomingData,dataLength);
szam++;
Sleep(50);
}
return 0;
}
im writing data, the cause of "incomingdata" array, is that i changed an example program and didn't changed the name, but its data is to send.
here is the python version, via pyserial:
import serial
import time
s=serial.Serial("COM35", 9600)
while True:
f=open("beki.txt", "r")
a=f.readline()
s.write(a)
time.sleep(0.5)
f.close()
I was under the impression that your Python code sends 6 bytes but your Arduino code only reads one byte.
Did you look at the examples in Serial Input Basics - especially examples 2 and 3. They read in all the data before acting on any of it.
And, for fairly obvious reasons, I have no idea where you removed the 2 Serial.print()s from. As your Python code was not reading the data coming from the Arduino it is possible that the PC input buffer overflowed.