SerialPort library

Extending or modifying the GP system itself (advanced!)

Moderator: MSandro

Post Reply
bromagosa
Posts: 33
Joined: Sep 15th, '15, 07:11

SerialPort library

Post by bromagosa » Sep 15th, '15, 12:00

Hi!

I see a there is a SerialPort category, which I can access from REPL:

Code: Select all

-- Serial Port --
   openSerialPort
   closeSerialPort
   readSerialPort
   writeSerialPort
I've looked everywhere in the browser and it doesn't seem to be there though. Do I need to import it into the authoring UI somehow?

Thanks!

JohnM
Posts: 379
Joined: Sep 11th, '15, 14:42

Re: SerialPort library

Post by JohnM » Sep 15th, '15, 13:55

These are "primitives" for which there are not (yet) any blocks.

You can invoke them from the GP command prompt by finding the terminal
window and typing control-C to exit the GP UI. You can find out more about
a primitive using the "help" command:

Code: Select all

gp> help openSerialPort
Open a serial port. Return the portID or nil on failure. Arguments: portName, baudRate
Here is a little program that opens a serial port connected to an embedded
ARM board (the mbed NXP LPC1768) running eLua (embedded Lua), sends a
command, waits for the response, and prints it.

Code: Select all

to testPort cmd {
  if (isNil cmd) { cmd = '' }
  p = (openSerialPort '/dev/tty.usbmodem1412' 115200)
  sleep 1000
  writeSerialPort p (join cmd (string 13))
  sleep 1000
  print (readSerialPort p)
  closeSerialPort p
}
Here's how you'd use it to send the command 'help' to eLua:

Code: Select all

gp> testPort 'help'
help
Shell commands:
  help   - shell help
  lua    - start a Lua session
  ls     - lists files and directories
  dir    - lists files and directories
  cat    - list the contents of a file
  type   - list the contents of a file
  recv   - receive files via XMODEM
  cp     - copy files
  ver    - show version information
  mkdir  - create directories
  exit   - exit the shell
For more information use 'help <command>'.
eLua#   
nil
gp> 

bromagosa
Posts: 33
Joined: Sep 15th, '15, 07:11

Re: SerialPort library

Post by bromagosa » Sep 15th, '15, 14:46

Oh, I see!

Okay, so with SerialPort working we can already push programs to the board from GP:

Code: Select all

f = io.open('autorun.lua')
f:write('pio.pin.sethigh(pio.PB_3)')
f:close()
Nice! I can't wait to have these primitives as blocks!

bromagosa
Posts: 33
Joined: Sep 15th, '15, 07:11

Re: SerialPort library

Post by bromagosa » Sep 17th, '15, 14:00

I've just discovered "blockify" and so I could make blocks for the serial port primitives!

Here's my first blink script:
Image

And the WhiteCat board blinking in an awfully low quality GIF:
Image

JohnM
Posts: 379
Joined: Sep 11th, '15, 14:42

Re: SerialPort library

Post by JohnM » Sep 17th, '15, 18:41

This is great! I love it when people just dive in and figure out how to add what they need themselves!

In case others want to try "blockify", here's how:

1. enter developer mode
2. open a "workspace" window
3. type some valid GP code and select it
4. control-click (or right-click) on the selected text
5. select "blockify" from the menu
blockify.png
The code will be converted to blocks that will be stuck to the cursor, so you can drop it wherever you like.
newBlock.png
Note: To make a "reporter" block for an expression returns a value, just put parentheses around the expression. For example: (3 + 4).

Post Reply