Dear all,
I’m writing since I’m getting a little bit confused…
What would like to do is the following communication between Core © and Arduino (A):
© --> (A) “:CT1!”
(A) --> © “:ANSWER1!”
Where :CT1! is a command (a string of 5 char) that starts with “:” and ends with “!”. Answers are variable length and always start and end in the same way.
I previously connected my Arduino to a RPi and via python script I was sending commands to Arduino and parsing the answer.
This was the original code for the Arduino:
String inputString = ""; // string for incoming command
boolean stringComplete = false; // flag command complete
void setup() {
Serial.begin(9600);
Serial.println("Start");
inputString.reserve(200);
}
void serialEvent() {
while (Serial.available())
{
char inChar = (char)Serial.read();
if (inChar == ':') // Command begins
{
inputString = "";
}
inputString += inChar;
if (inChar == '!') // Command ends
{
stringComplete = true;
}
}
}
void loop() {
if (stringComplete)
{
if ((inputString.length()==5) && (inputString.startsWith(":C")))
{
switch (inputString.charAt(2))
{
case 'P':
Serial.println(":P:Arduino is live!");
break;
case 'T':
Serial.println(ProduceAnswer(inputString.charAt(3)));
break;
}
}
// clear
inputString = "";
stringComplete = false;
}
}
Now reading this http://community.spark.io/t/spark-3-3v-to-arduino-5v-serial-communication/3548
I did connected as follows my arduino 3.3v to the Core:
(A) (C)
GND <--> GND
3.3Vin <--> 3.3Vout
D6 <--> TX
D7 <--> RX
For Arduino the code has been adapted:
#include <SoftwareSerial.h>
SoftwareSerial swSerial(6,7); // 6 RX, 7 TX
void setup() {
swSerial.begin(9600);
[...]
}
[...]
// Serial changed with swSerial
But now the serialEvent() function is no more available. I tried to create a swSerialEvent() and call it at the beginning of the loop() but it seems not to work.
The spark code:
String inputString = "";
boolean stringComplete = false;
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
Serial.println("SPARK: Start");
}
void loop() {
if (Serial1.available() > 0)
{
Serial1.write(":CT0!"); // Send the command
Serial.println("SPARK: sending CT0!");
}
// Read the answer
while (Serial1.available())
{
char inChar = (char)Serial1.read();
Serial.print("SPARK: receive single char: ");
Serial.print(inChar);
if (inChar == ':')
{
inputString = "";
}
inputString += inChar;
if (inChar == '!')
{
stringComplete = true;
}
}
if (stringComplete)
{
Serial.print("SPARK: received: ");
Serial.println(inputString);
}
else
{
Serial.println("SPARK: waiting");
}
// clear the string:
inputString = "";
stringComplete = false;
delay(1000);
}
But it actually never mets the “if (Serial1.available() > 0)” …
Can anyone help me clarify?
Is there an alternative to serialEvent() function for software serial? Exists this for Core?
If I take off the FTDI connector from the arduino, I can use the original serial pins 0 RX and 1 TX… but how can I make the Core to wait to have the whole answer string back?
Thank you for your support.
dk