0
votes

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

1
have you tried changing the serial port speed on both the arduino and the raspberry pi? for example 19200 or 115200 arduino.cc/en/Serial/beginEntrabiter
yes I tried to change BaudRate for both devices .. it didn't give any remarkable difference .. 9600 is enough to handle "Hello Pi" , '5'Amro Samy
I found a good hint arduino receive only single string usig Serial.read commandAmro Samy

1 Answers

1
votes

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?