I'm trying to read lines from an Arduino board with a very simple code (for the sake of showcasing the problem) on Linux.
Python code:
# arduino.py
import serial
arduino = serial.Serial('/dev/ttyACM0')
with arduino:
while True:
print(arduino.readline())
Arduino code:
// simpleWrite.ino
long ii = 0;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
Serial.println(ii);
ii++;
}
As the board auto-resets when the serial connection is opened, the first bytes are likely garbage. After a second or two everything works fine.
This is a typical output:
$ python arduino.py
b'09\r\n'
b'540\r\n'
b'541\r\n'
b'542\r\n'
b'543\r\n'
b'544\r\n'
b'545\r\n'
b'546\r\n'
b'547\r\n'
b'548\r\n'
b'549\r\n'
b'550\r\n'
b'551\r\n'
b'552\r\n'
b'553\r\n'
b'554\r\n'
b'555\r\n'
b'556\r\n'
b'557\r\n'
b'55\xfe0\r\n' # <---- Here the board restarted
b'1\r\n'
b'2\r\n'
b'3\r\n'
b'4\r\n'
b'5\r\n'
b'6\r\n'
b'7\r\n'
b'8\r\n'
b'9\r\n'
b'10\r\n'
However, I see the Arduino IDE Serial Monitor doesn't have this problem, and properly shows a delay (while restarting) and then prints all the lines starting from the first one.
Is there a way to emulate this behaviour in Python using pySerial? That is, discarding all the output before restarting and nothing more? Perhaps through some low-level functions?
I tried looking at the relevant Arduino source code, but I don't know Java and it didn't help.
Note: Of course I could sleep for, say, three seconds, discard everything and start from there, but I would probably discard some of the first lines too.
Edit: Apparently, this problem doesn't exist on Windows and the accepted solution was not necessary.
The Arduino IDE's monitor toggle's the assigned DTR pin of the port when connected. Where this toggling causes a reset on the Arduino. Noting that the DTR is toggled after the Monitor has opened the Serial port and is ready to receive data. In your case the below example should do the same.
Import serial
arduino = serial.Serial('/dev/ttyS0',
baudrate=9600,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
timeout=1,
xonxoff=0,
rtscts=0
)
# Toggle DTR to reset Arduino
arduino.setDTR(False)
sleep(1)
# toss any data already received, see
# http://pyserial.sourceforge.net/pyserial_api.html#serial.Serial.flushInput
arduino.flushInput()
arduino.setDTR(True)
with arduino:
while True:
print(arduino.readline())
I would also add the compliment to the DTR for the Arduino's with AVR's using built-in USB, such as the Leonoardo, Esplora and alike. The setup() should have the following while, to wait for the USB to be opened by the Host.
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
}
It will have no effect for FTDI's based UNO's and such.
I realize this is an old question, but hopefully this can be useful to somebody else out there with the same problem.
I had an issue where if I used any baudrates other than 9600, the serial connection in python would just receive gibberish all the time, even if Serial.begin(...) is properly set on the arduino and matches the value used in the python code.
I read online that the bootloader or watchdog may take a second to load (when the board is power-cycled) and it may send stuff over serial at some specific baudrate (for chip programming possibly). I'm guessing that this is what messes up the serial communication in python.
Here's the piece of code that gives me reliable results:
import serial
from time import sleep
arduino = serial.Serial('/dev/ttyACM0') # dummy connection to receive all the watchdog gibberish (unplug + replug) and properly reset the arduino
with arduino: # the reset part is actually optional but the sleep is nice to have either way.
arduino.setDTR(False)
sleep(1)
arduino.flushInput()
arduino.setDTR(True)
# reopen the serial, but this time with proper baudrate. This is the correct and working connection.
arduino = serial.Serial('/dev/ttyACM0',baudrate=57600)
with arduino:
while True:
print(arduino.readline())
The code used on the arduino side for testing is as simple as this:
void setup() {
Serial.begin(57600);
Serial.println("setup");
}
void loop() {
Serial.println("hello");
delay(200);
}
Please follow this link for a reliable PC-Arduino USB serial communication using python.
Python code simply sends a short message to the Arduino and prints the reply it receives.
// This is very similar to Example 3 - Receive with start- and end-markers
// in Serial Input Basics http://forum.arduino.cc/index.php?topic=396450.0
const byte numChars = 64;
char receivedChars[numChars];
boolean newData = false;
byte ledPin = 13; // the onboard LED
//===============
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
Serial.println("<Arduino is ready>");
}
//===============
void loop() {
recvWithStartEndMarkers();
replyToPython();
}
//===============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//===============
void replyToPython() {
if (newData == true) {
Serial.print("<This just in ... ");
Serial.print(receivedChars);
Serial.print(" ");
Serial.print(millis());
Serial.print('>');
// change the state of the LED everytime a reply is sent
digitalWrite(ledPin, ! digitalRead(ledPin));
newData = false;
}
}
//===============
Python Code
import serial
import time
startMarker = '<'
endMarker = '>'
dataStarted = False
dataBuf = ""
messageComplete = False
#========================
#========================
# the functions
def setupSerial(baudRate, serialPortName):
global serialPort
serialPort = serial.Serial(port= serialPortName, baudrate = baudRate, timeout=0, rtscts=True)
print("Serial port " + serialPortName + " opened Baudrate " + str(baudRate))
waitForArduino()
#========================
def sendToArduino(stringToSend):
# this adds the start- and end-markers before sending
global startMarker, endMarker, serialPort
stringWithMarkers = (startMarker)
stringWithMarkers += stringToSend
stringWithMarkers += (endMarker)
serialPort.write(stringWithMarkers.encode('utf-8')) # encode needed for Python3
#==================
def recvLikeArduino():
global startMarker, endMarker, serialPort, dataStarted, dataBuf, messageComplete
if serialPort.inWaiting() > 0 and messageComplete == False:
x = serialPort.read().decode("utf-8") # decode needed for Python3
if dataStarted == True:
if x != endMarker:
dataBuf = dataBuf + x
else:
dataStarted = False
messageComplete = True
elif x == startMarker:
dataBuf = ''
dataStarted = True
if (messageComplete == True):
messageComplete = False
return dataBuf
else:
return "XXX"
#==================
def waitForArduino():
# wait until the Arduino sends 'Arduino is ready' - allows time for Arduino reset
# it also ensures that any bytes left over from a previous message are discarded
print("Waiting for Arduino to reset")
msg = ""
while msg.find("Arduino is ready") == -1:
msg = recvLikeArduino()
if not (msg == 'XXX'):
print(msg)
#====================
#====================
# the program
setupSerial(115200, "/dev/ttyACM0")
count = 0
prevTime = time.time()
while True:
# check for a reply
arduinoReply = recvLikeArduino()
if not (arduinoReply == 'XXX'):
print ("Time %s Reply %s" %(time.time(), arduinoReply))
# send a message at intervals
if time.time() - prevTime > 1.0:
sendToArduino("this is a test " + str(count))
prevTime = time.time()
count += 1
you need to set your var , try:
unsigned long ii = 0;
but pay attention that this is a 32 bit var and when it is full ,cause overflow and reboot.
for me work.
As suggested by #Kobi K add a minimal delay time, for load real data at 9600 boud each char has a duration of 2 ms,
void loop() {
Serial.println(ii);
delay(20);
ii++;
}
And in python you need to declare a Pyserial like this:
arduino=serial.Serial('/dev/ttyACM0',9600,timeout=0.0001)
hope this help you
Related
I am using Python 3.7.5 with the latest version of serial library. I am trying to make an RFID authentication device via python and arduino. User has to scan their ID in the RFID connected to the arduino and the arduino must send the UID to the python software. In my laptop, a thread listening for serial is running. It checks the UID and it will send 'O' if it is allowed and 'X' if it is not. In the arduino, the program then waits if there is data sent through the serial then checks the input if it is 'O' or not. If the RX is 'O' then the LED must light green, otherwise red.
My problem is that when I first scan the CORRECT uid, it goes green, no problem. If I scan another CORRECT uid it goes green again, no problem. If I scan an INCORRECT uid, it goes green, but in my python code it SHOULD BE RED. Then if I scan a CORRECT uid, it goes RED whereas is should be GREEN. I tried adding delays to both arduino and python to wait for the previous input to clear and also tried flushing after transmission with no luck.
tl;dr
The arduino is outputting results ONE uid scan late and I don't know what else to do.
Python:
# Reads data from the serial monitor without interrupting the main thread
from serial import Serial
import time
from threading import Thread
class SerialListener:
def __init__(self, baudrate=9600, timeout=1):
try:
self.ser = Serial('/dev/ttyACM0', baudrate, timeout=timeout)
except:
self.ser = Serial('/dev/ttyACM1', baudrate, timeout=timeout)
self.stopped = False
self.paused = False
self.stream = ''
time.sleep(1) # Wait for serial buffer to reset
def start(self):
Thread(target=self.update, args=()).start()
return self
def update(self):
if not self.paused:
while True:
if self.stopped:
self.ser.close()
print("Serial Thread Stopped")
print("Serial Port Closed")
break
try:
self.stream = self.ser.readline().decode('utf-8')
except:
self.stream = self.ser.readline().decode('ascii')
self.stream = self.stream.rstrip()
def stop(self):
self.stopped = True
def pause(self):
self.paused = True
def flush(self):
self.ser.flush()
def readDistance(self):
try:
return float(self.stream)
except:
return -1 # Returns -1 if there is an error in reading
def readRFID(self):
return self.stream
def write(self, msg):
self.ser.write(msg.encode())
if __name__ == "__main__": # FOR DEBUGGING ONLY
uno = SerialListener().start()
uno.flush()
print("Serial Started")
uid = ''
while True:
uid = uno.readRFID()
if uid is not '':
uno.flush()
time.sleep(0.1)
if uid == "5BEE9F0D":
uno.write('O')
print("SHOULD BE GREEN")
else:
uno.write('X')
print("SHOULD BE RED")
print(uid)
uno.stop()
Arduino:
#include <MFRC522.h>
#define GREEN_LED 6
#define RED_LED 7
#define BUZZER 8
MFRC522 rfid(10, 9);
unsigned long timer = 0;
bool readStatus = false;
void setup() {
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
for(int i = 0; i < 10; i++)
Serial.write('\n');
delay(5);
digitalWrite(RED_LED, HIGH);
}
void loop() {
while(!readStatus){
if(rfid.PICC_IsNewCardPresent()){
if(rfid.PICC_ReadCardSerial()){
byte uid[rfid.uid.size];
if((millis() - timer) > 1000){
for(int i = 0; i < rfid.uid.size; i++)
uid[i] = rfid.uid.uidByte[i];
for(int i = 0; i < sizeof(uid); i++){
if(uid[i] < 0x10)
Serial.print('0');
Serial.print(uid[i], HEX);
}
Serial.println();
readStatus = true;
timer = millis();
}
Serial.flush();
}
}
}
if(readStatus){
while(!Serial.available());
char rx = 'X';
while(Serial.available()){
rx = Serial.read();
}
if(rx == 'O'){
digitalWrite(GREEN_LED, HIGH);
digitalWrite(RED_LED, LOW);
tone(BUZZER, 2500);
delay(100);
noTone(BUZZER);
readStatus = false;
}
else{
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, LOW);
tone(BUZZER, 1000);
delay(50);
noTone(BUZZER);
delay(30);
tone(BUZZER, 1000);
delay(50);
noTone(BUZZER);
digitalWrite(RED_LED, HIGH);
readStatus = false;
}
}
}
Output:
Serial Started
SHOULD BE RED
05520320 // it is red
SHOULD BE RED
05520320 // it is red
SHOULD BE GREEN
5BEE9F0D // it is red
SHOULD BE GREEN
5BEE9F0D // it is green
SHOULD BE RED
05520320 // it is green
Okay I solved the problem by modifying the timeout in pyserial and adding a 1 second delay between reading.
The problem was that the pyserial writes to the serial more than once because of how I set the condition.
while True:
uid = uno.readRFID()
if uid is not '':
uno.flush()
time.sleep(0.1)
if uid == "5BEE9F0D":
uno.write('O')
print("SHOULD BE GREEN")
else:
uno.write('X')
print("SHOULD BE RED")
print(uid)
Since the timeout I set was 1, PySerial will continue to read the UID until timeout. This will cause the uid to be not equal to '' for a second and will send data to the serial multiple times unnecessarily.
In the Arduino, it only reads one byte of character from the buffer and after reading it, removes what it read from the buffer. However, python didn't send only 1 character to the serial, but more than once. That is why the Arduino outputs incorrectly as it reads the character from the PREVIOUS buffer instead of the new character that the python sent to the serial
To solve this problem, I made the timeout lesser so that the input of the serial will clear after timeout has reached. Do not set the timeout value too low or else the data might not be read.
def __init__(self, baudrate=9600, timeout=0.5):
Secondly, I added a delay between reading from serial on the main thread
uid = ''
while True:
while uid is '':
uid = uno.readRFID()
time.sleep(0.1)
if uid == "5BEE9F0D":
uno.write('O')
else:
uno.write('X')
print(uid)
time.sleep(1)
uid = ''
The code in the Arduino works so I didn't change that, the only problem was within the python code itself.
I have arduino uno with simple firmware which provides simple API over serial port:
Command "read" returns the current state
Command "on" sets the state to "on"
Command "off" sets the state to "off"
Now I want to implement a client for this device.
If I use Arduino IDE serial monitor, this API works as expected.
If I use python with pySerial library, API works.
But whenever I try to read data from the serial port using golang and go-serial, my read calls hangs (but works fine with /dev/pts/X created by socat, for example)
Python client
import serial
s = serial.Serial("/dev/ttyACM0")
s.write("read\n")
resp = []
char = None
while char != "\r":
char = s.read()
resp.append(char)
print "".join(resp)
Go client (hangs on Read call forever):
package main
import "fmt"
import "github.com/jacobsa/go-serial/serial"
func check(err error) {
if err != nil {
panic(err.Error())
}
}
func main() {
options := serial.OpenOptions{
PortName: "/dev/ttyACM0",
BaudRate: 19200,
DataBits: 8,
StopBits: 1,
MinimumReadSize: 4,
}
port, err := serial.Open(options)
check(err)
n, err := port.Write([]byte("read\n"))
check(err)
fmt.Println("Written", n)
buf := make([]byte, 100)
n, err = port.Read(buf)
check(err)
fmt.Println("Readen", n)
fmt.Println(string(buf))
}
Firmware code:
String inputString = ""; // a String to hold incoming data
boolean stringComplete = false; // whether the string is complete
String state = "off";
void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
pinMode(13, OUTPUT);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
blink();
if (inputString == "on\n") {
state = "on";
} else if (inputString == "off\n") {
state = "off";
} else if (inputString == "read\n") {
Serial.println(state );
}
// clear the string:
inputString = "";
stringComplete = false;
}
}
void blink() {
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
Python code
You have set the baud rate for the Go lang function to 19200, but in the arduino you have used 9600.
In the python code, the baud rate is not set, so it takes the default of 9600.
Just set the right baud rate in your go lang program, and it should work.
I've some problem writing serial bytes from a python code to arduino.
The python code had to write to serial port a number that the arduino receive.
Python3 code:
import serial
import time
ser = serial.Serial ('/dev/ttyACM0',)
ser.baudrate = 115200
ser.write(str(3).encode()) #or (b'3')
ser.write(str('\n').encode())
Arduino code:
void setup(){
Serial.begin (115200); //Comunicazione seriale 115200 bit
servomotore.attach(3);
pinMode(2,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
digitalWrite(2,HIGH);
digitalWrite(12,HIGH);
servomotore.write(180);
}
/*Il loop comprende due funzioni; sensori e Mappa, attivate ogni 15 gradi di movimento del servomotore,
sensori rileva le distanze, Mappa invia i valori al seriale, ogni ciclo del radar produce 24 valori in centimetri*/
void loop() {
char buffer[] = {' ',' '};
if (Serial.available() > 0) {
Serial.readBytesUntil('n', buffer, 2);
int incremento = atoi(buffer);
If I run this code I can't see output, no error or print, I need to exit with ctrl+c. Arduino doesn't receive nothing.
The Arduino code is longer, this is the only part that I can't understand in this moment, it's only a part of a most complex project
I cannot reproduce your problem. Here is how I tested it. On the Arduino Uno setup the following program:
void setup()
{
Serial.begin(115200);
Serial.println(F("Serial test"));
}
char buffer[80] = { 0 };
void loop()
{
if (Serial.available() > 0)
{
Serial.readBytesUntil('\n', buffer, sizeof(buffer));
Serial.print(F("read: ["));
Serial.print(buffer);
Serial.println("]");
memset(buffer, 0, sizeof(buffer));
}
}
The following python3 script successfully reads and writes to this Arduino firmware:
#!/usr/bin/env python3
import sys
from serial import Serial
def main():
ser = Serial('/dev/ttyACM0',)
ser.baudrate = 115200
print(ser.readline())
ser.write(str(3).encode())
ser.write(str('\n').encode())
print(ser.readline())
return 0
if __name__ == '__main__':
sys.exit(main())
I get the following output once the device is programmed and plugged in:
~ $ python3 so-check-serial.py
b'Serial test\r\n'
b'read: [3]\r\n'
Alternative interrupt based serial input
I'd like to add that I would not normally write a serial handler like this in Arduino. The following uses the serial interrupt to buffer the input and set a flag when the line is read completely. In this example we can wait or do other things until a whole line has been receivied (ie: keep blinking at the right times):
#include <Arduino.h>
volatile String buffer;
volatile bool inputComplete = false;
void serialEvent()
{
while (Serial.available())
{
char c = (char)Serial.read();
if (c == '\n')
inputComplete = true;
else
buffer += c;
}
}
void setup()
{
Serial.begin(115200);
Serial.println(F("Serial test"));
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
void loop()
{
if (inputComplete) {
inputComplete = false;
Serial.println(buffer.c_str());
buffer = "";
}
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(500);
}
My (Python) publisher:
import zmq
import time
context = zmq.Context()
socket = context.socket(zmq.PUB)
connectStr = "tcp://*:%d" % 5563
socket.bind(connectStr)
messageNum = 0
while True:
++messageNum
message = "Testing %d"%messageNum
print("Sending.. '%s'"%message)
socket.send_string(message)
time.sleep(1)
messageNum += 1
My (C++) subscriber (running in GTest):
TEST(ZeroMqPubSubTest, SubscribeGetsData)
{
// Set up the subscriber we'll use to receive the message.
zmq::context_t context;
zmq::socket_t subscriber(context, ZMQ_SUB);
// Connect to the publisher
subscriber.connect("tcp://127.0.0.1:5563");
subscriber.setsockopt(ZMQ_SUBSCRIBE, ""); // Set the filter blank so we receive everything
zmq::message_t response(0);
EXPECT_TRUE(subscriber.recv(&response));
}
I start up the publisher then start up the subscriber. The latter never returns though.
If I run a Python subscriber doing (I thought) exactly the same thing..
import zmq
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect ("tcp://127.0.0.1:5563")
socket.setsockopt_string(zmq.SUBSCRIBE, "")
print ("Waiting for data...")
while True:
message = socket.recv()
print ("Got some data:",message)
..it works fine:
Waiting for data...
Got some data: b'Testing 8'
Got some data: b'Testing 9'
There are two overloads of setsockopt defined in zmq.hpp:
template<typename T> void setsockopt(int option_, T const& optval)
{
setsockopt(option_, &optval, sizeof(T) );
}
inline void setsockopt (int option_, const void *optval_, size_t optvallen_)
{
int rc = zmq_setsockopt (ptr, option_, optval_, optvallen_);
if (rc != 0)
throw error_t ();
}
By providing only two arguments you implicity used the first overload, which assumes a value length of sizeof(T). This resolves to one, because "" is a zero-terminated character array. To pass in an empty string you need to use the second overload and specify a length of 0:
subscriber.setsockopt(ZMQ_SUBSCRIBE, "", 0);
Alternatively, use a zero size data type:
char none[0];
subscriber.setsockopt(ZMQ_SUBSCRIBE, none);
I would like to set up a serial communication between a Python daemon and an Arduino.
At first, the Python daemon sets up a serial connection that will last for the whole lifetime of the daemon. Through this connection, I would like to send data to the Arduino and receive back data in the acks variable every time the Python daemon receives commands.
The problem is that while the first time the communication goes well, nothing is sent through serial afterwards. If I make the a new connection for every request it works, but it makes the program very slow, which I'd like to avoid.
edit: the real issue is when send a correct string to the arduio evrything goes well but when i send a wrong one the serial port block and it will never reconize corrct strings again( the problem is in the arduino code)
Python code:
import serial
import time
import sys
from socket import *
import threading
import thread
def handler(clientsock,addr):
while 1:
#arduino.flush()
data = clientsock.recv(BUFSIZ)
if not data:
break
print data
print data
#time.sleep(3)
arduino.write(data)
#time.sleep(3)
ack = arduino.readline(1)
arduino.flush()
clientsock.send(ack+"\n")
clientsock.close()
if __name__=='__main__':
HOST = '0.0.0.0'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
arduino = serial.Serial('/dev/ttyACM0',9600,timeout=6)
serversock = socket(AF_INET, SOCK_STREAM)
serversock.bind(ADDR)
serversock.listen(2)
while 1:
print 'waiting for connection...'
clientsock, addr = serversock.accept()
print '...connected from:', addr
thread.start_new_thread(handler, (clientsock, addr))
Arduino code:
int relayPinCH1 = 7; // pin de commande du relais 1
char inData[20]; // Allocate some space for the string
char inChar=-1; // Where to store the character read
byte index = 0; // Index into array; where to store the character
void setup()
{
pinMode(relayPinCH1, OUTPUT);
Serial.begin(9600);
}
char Comp(char* This) {
while (Serial.available() > 0) // Don't read unless
// there you know there is data
{
if(index < 19) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = '\0'; // Null terminate the string
}
}
Serial.flush();
if (strcmp(inData,This) == 0) {
for (int i=0;i<19;i++) {
inData[i]=0;
}
index=0;
return(0);
}
else {
return(1);
}
}
void loop()
{
//Serial.println("Hello Pi");
if (Comp("l11\n")==0)
{
Serial.flush();
digitalWrite(relayPinCH1, HIGH);
Serial.println("y");
}
if (Comp("l10\n")==0)
{
Serial.flush();
digitalWrite(relayPinCH1, LOW);
Serial.println("n");
}
delay(1000);
}
In your Arduino code, your logic is kind of funky - so, I'm not sure, but are you clearing index to 0 before you start the loop again? It looks like once index == 19, it may or may not get reset to 0 depending upon later logic. If you enter Comp() a second time and index >= 19 then you'll never read the serial port again.
I think #Zeus is entirely right (and hence I upvoted that answer), but there are also other problems. To reiterate what #Zeus is saying:
index is only reset to 0 if the comparison succeeds. So your buffer is full, the string you are looking for isn't there, and index never goes back to 0 again.
Once index reaches 19, no more reading is done. As a result, whatever is in inData stays in inData and all the future comparisons will fail, meaning index will never get reset to 0.
There are a number of other problems in the code, but the main issue is that the design is very fragile, and prone to exactly the sort of error you are experiencing. For instance if the newlinews your Python script is sending are CR+LF for newlines, but you are expecting CR only, you'll have the same sort of failure you have now: first time communications work, but never again.
I would suggest reorganizing your code like this:
Your function for reading serial port reads a line from a serial port and returns that to the caller (without the newlines), regardless of the content of the communications.
The caller compares the line received from the serial port with the list of known commands and executes them accordingly.
This might look rougly as follows
char strCommand[0xFF];
int idxCommandChar;
// Read a command from serial, returning the command size
// This function BLOCKS, i.e., doesn't return until a command is available
int readSerialCommand() {
// We reset the index to zero on every read: the command is overwritten every time
idxCommandChar = 0;
// Read serial characters and store them in strCommand
// until we get a newline
int in = Serial.read();
while (in!='\n') {
strCommand[idxCommandChar++] = in;
in = Serial.read();
}
// Add the string terminator
strCommand[idxCommandChar++] = '\0';
// Return command size
return idxCommandChar;
}
// Get command from serial, and process it.
void processCommand() {
readSerialCommand();
if (strcmp(strCommand, "CMD1")==0) {
// do something
} else if (strcmp(strCommand, "CMD2")==0) {
// do something else
} else {
// Unknown command
Serial.println("Unknown command");
}
}
void loop() {
processCommand();
delay(1000);
}
This code blocks on serial, i.e. doesn't return until a newline is detected. You could easily modify the code to be non-blocking, perhaps like this:
/* Read serial characters, if available and store them in strCommand
until we get a newline
Returns 0 if no command is available */
int readSerialCommand() {
idxCommandChar = 0;
while (Serial.available()) {
int in = Serial.read();
while (in!='\n') {
strCommand[idxCommandChar++] = in;
in = Serial.read();
}
strCommand[idxCommandChar++] = '\0';
return idxCommandChar;
}
return 0;
}
// Get command from serial (if available), and process it.
void processCommand() {
if (readSerialCommand()) {
....
In either case you might loose serial characters while you are waiting, so you may want to rethink that strategy.