Amro Samy
Amro Samy

Reputation: 75

Serial Communication between Arduino and rasoberry is very slow (0.2 sec)

I have a problem when I'm trying to send/receive data across serial port on raspberry Pi 2 ... I used Arduino Mega and AUNO as well also I tried nanpy library it worked slow too

python code :

import serial
from time import sleep

ser = serial.Serial('/dev/ttyACM1', 9600)
while 1 :
    a= ser.readline()

    ser.write('5')
    print (a)

Arduino Code :

void setup()
{

    Serial.begin(9600);
    for (int i=2;i<=13;i++){
     pinMode(i,OUTPUT);
    }
}

void loop()
{
  Serial.println("Hello Pi");
 if (Serial.available()>0) {

     for(int i=2;i<=13;i++){
     analogWrite(i,255);
     Serial.print (Serial.read());
  } } }

Help please this is the last step to finalize my code for the project :'(

Thanx

Upvotes: 0

Views: 663

Answers (1)

Paul Cornelius
Paul Cornelius

Reputation: 10916

Your Python program will block on the statement ser.readline() until the serial port gets a "\n" (newline) character. It looks like your Arduino program doesn't send a newline except in the statement Serial.println. So your Python program will print something exactly as often as the function loop() is called. The code you gave never calls loop, so no one can tell how often that is. Your loop() function, by the way, makes little sense; every time it is called it writes "Hello pi" followed by a newline, and then it reads the serial port exactly 12 times and each time it writes whatever it reads. Seems a bit strange. Is that what you want?

Upvotes: 1

Related Questions