I have spent some time experimenting with Mikael Morup's Gobetwino package.
Everything I have tried seems to work, eventually. The whole process of communicating, via Gobetwino, between the Arduino and the PC is very time sensitive. Delays in the Arduino code are often necessary to let things happen properly.
The most difficult command to get working was the T command, to get a Timestamp from the PC. I finally solved it with a delay inserted into my Arduino Sketch.
I attach the sketch below for anyone interested:
/*
Gobetwino test sketch to open an existing text file using Notepad,
get a TimeStamp, format HH:MM:SS from the PC, write the TimeStamp
to the file and then save and close the file.
Written by Neil Kenyon, 18 April 2012
Uses code originally written by Mikael Morup, which is in the
public domain.
*/
int serInLen = 25; // Max length of string returned by Gobetwino
int pId; // Id of process started by Gobetwino
char serInString[25]; // String returned by Gobetwino
char timeString[9]; // Time Stamp extracted from returned string
byte swPin = 2; // pin for start switch; pulls pin 2 LOW.
void setup()
{
pinMode(swPin, INPUT);
Serial.begin(9600);
while(digitalRead(swPin))
{
// Loop endlessly, waiting for the start switch to be pressed
}
/*
Open Notepad with an EXISTING file.
The filename and full path are defined in the Gobetwino user
command "NOTEPADFIL", which must be created within Gobetwino.
The filename and path are in the command line parameter.
IF the file DOES NOT already exist, an attempt will be made to
create it, with on-screen dialogue. You may not have time to
respond to the dialogue!! See below.
*/
Serial.println("#S|NOTEPADFIL|[]#");
// wait up to 4 seconds for reply from Gobetwino (= Process Id)
readSerialString(serInString, 4 * 1000);
// convert char Process Id to an integer
pId= atoi(serInString);
// Wait 2 seconds to make sure the file is properly open.
// Increase this delay if you want to create a NEW file by
// responding to on-screen dialogue
delay(2 * 1000);
// Write a string constant to the file.
// The 2nd parameter is a newline which will appear in the file
// after the string constant.
// This just proves the file etc works OK.
sendKeys("Hello World", '\n', pId);
//Get a Time Stamp from the PC
Serial.println("#S|T|[]#");
// Wait 1 second for PC TimeStamp to arrive in the Serial Buffer
// This delay is VITAL and makes the whole damn thing work!
delay(1 * 1000);
// wait 5 seconds (max) for an answer from Gobetwino - PC Time
readSerialString(serInString, 5 * 1000);
// There is a leading 0 character in the returned string
// We need to strip it off. 3rd parameter is actual length of
// destination string - ie does NOT including terminal NULL.
stripFirst(serInString, timeString, 8);
// write the Time Stamp to the file; 2nd parameter is a comma
// which allows for csv writing in a text file.
sendKeys(timeString, ',', pId);
// Wait 2 seconds then save and close the file.
delay(2 * 1000);
// ALT Fs is save and ALT Fx is exit; 2nd parameter is newline
// the newline is probably "belt and braces"
sendKeys("%Fs%Fx", '\n', pId);
// Wait 2 seconds for the save to finish
delay(2000);
}
void loop()
{
// Do nothing here
}
/*
Read a string from serial and store it in a char array.
You must supply the array variable - will return if timeOut
passes before the string is read, so you should check the
contents of the char array before making any assumptions.
Borrowed from Mikael Morup
*/
void readSerialString (char *strArray, long timeOut)
{
long startTime = millis();
// In Mikael's original sketch, i is NOT initialised to 0
// Nevertheless, it works! I don't understand why?
// I have initialised i to 0 for tidiness.
int i = 0;
while (!Serial.available())
{
if (millis() - startTime >= timeOut)
{
return;
}
}
while (Serial.available() && i < serInLen)
{
strArray[i] = Serial.read();
i++;
}
strArray[i] = '\0'; // Mikael doesn't do this either.
}
// Build Gobetwino SENDK command and write to Serial Output
// parameter 2 (par2) is either a newline character or a
// separator eg comma. Parameter 3 is the integer Process Id
void sendKeys(char par1[], char par2, int prId)
{
char buf[5];
Serial.print("#S|SENDK|[");
Serial.print(itoa((prId), buf, 10));
Serial.print("&");
Serial.print(par1);
Serial.print(par2);
Serial.println("]#");
}
// Copy len characters from array1 to array2, stripping off the
// first character of array1. A NULL terminator is added to the
// destination array.
void stripFirst(char *array1, char *array2, int len)
{
int i;
for(i = 0; i < len; i++)
{
array2[i] = array1[i + 1];
}
array2[i + 1] = '\0';
}
The sketch file is also attached.
Hope someone may find this useful