Serial communication problem Python/C++

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.

library:
http://playground.arduino.cc/Interfacing/CPPWindows

Code snippets are rarely helpful.

Incomplete code is even more so.

How do you know it is "slowing down?"

I'm sure sleeping for 200 milliseconds (or is that seconds, your comments aren't clear) has nothing to do with the speed.

This Python - Arduino demo may be useful. Also Serial Input Basics which is more recent.

...R

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;
}

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Please post the Python version - I am more comfortable with that.

Are you reading or writing data?
I can’t make sense of your use of the incomingData array - should it be outgoingData ?

…R

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()

Have you tried a = "testAB" instead of reading the data from a file

Post your Arduino code.

...R

Here’s the code i used, i took out the 2 Serial.print, now it seems to work. thank you.
I didn’t know if that can cause a problem like mine.

#include <avr/pgmspace.h>
#include <LedControl.h>
LedControl mydisplay = LedControl(4, 2, 3, 1);
int incomingByte = 0;   // for incoming serial data
int i=7,j=0;
void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
        mydisplay.shutdown(0, false);  // turns on display
        mydisplay.setIntensity(0, 1); // 15 = brightest
}

void loop() {
        if (Serial.available() > 0) {
                incomingByte = Serial.read();
                if(incomingByte>98)
                {
                  i=7;
                  mydisplay.setDigit(0, 0, 24, false);
                  mydisplay.setDigit(0, 1, 24, false);
                  mydisplay.setDigit(0, 2, 24, false);
                  mydisplay.setDigit(0, 3, 24, false);
                  mydisplay.setDigit(0, 4, 24, false);
                  mydisplay.setDigit(0, 5, 24, false);
                  mydisplay.setDigit(0, 6, 24, false);
                  mydisplay.setDigit(0, 7, 24, false);
                }
                if(incomingByte==97)
                {
                  mydisplay.setIntensity(0, 1); // 15 = brightest
                }
                if(incomingByte==98)
                {
                  mydisplay.setIntensity(0, 15); // 15 = brightest
                }
                if(i==6)
                i=5;
                if(incomingByte<97){
                Serial.print("I received: ");
                Serial.println(incomingByte-48, DEC);
                 mydisplay.setDigit(0, i, incomingByte-48, false);
                 i--;
                }
        }
}

I think I am more confused than ever.

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.

...R