element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Personal Blogs
  • Members
  • More
Personal Blogs
Legacy Personal Blogs Fun with Gobetwino
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: neilk
  • Date Created: 18 Apr 2012 2:26 PM Date Created
  • Views 488 views
  • Likes 2 likes
  • Comments 2 comments
  • gobetwino
  • arduino_projects
Related
Recommended

Fun with Gobetwino

neilk
neilk
18 Apr 2012

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

Attachments:
open_notepad_write_timestamp.ino.zip
  • Sign in to reply
  • neilk
    neilk over 11 years ago

    Thanks Nico; my pleasure.

     

    Sadly. I think I'm in the same boat as you for the greenhouse control.........I keep finding interesting little coding problems which take hours to solve!

     

    Maybe the heating control system will be finished for the coming winter...........

     

    All the best

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • ntewinkel
    ntewinkel over 11 years ago

    Excellent stuff, Neil!! Thanks for that!

    Thanks for posting the code too image

    I'm glad you were able to figure out how to read the values back from Gobetwino, that's a tricky one.

    I'm going to give your code a try here first chance I get... hopefully after work tonight.

     

    How's the greenhouse doing? By the time I finally get all the hardware/software ready I'll likely be using this setup for determining daytime highs rather than nighttime lows image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube