I've been playing around with serial comms on my Pi/Picaxe dev rig.
Using Gordon's wiringPi library makes it easy to open the gpio serial comms port on the Pi and write code with Gambas.
So this post involves writing a simple Picaxe program, and then using wiringPi and Gambas on the Pi to communicate with the Picaxe.
Much of the configuration stuff is described in my previous posts which you can link to from here:-
I am using a PiB2 running Raspbian Jessie, and found I needed the very latest versions/updates for wiringPi and Jessie.
I'm running Gambas v3.5.4 which can be installed straight from the Jessie repository. Dont forget you will need to run Gambas as root, which you can do from a terminal with:-
gksu gambas3
The Picaxe I'm using is an 08M2, but in principle you could use other models/variants.
Picaxe test program
Initially I wrote a simple Picaxe program to send data to the Pi, which looked like this:-
'Serial comms check
'===================
'Send a message at 3s intervals
'+++++++++++++++++++++++++++++++++++
b0 = 1
main:
SerOut 0,N4800,("Hi Pi! ",#b0,13) '13 = CR, 10=LF
inc b0
pause 3000
goto main
Once compiled and downloaded, I used minicom to check that the Pi was receiving the message. So in a terminal:-
sudo minicom -D /dev/ttyAMA0 -b 4800
...which should result in an output like this:-
Welcome to minicom 2.7
OPTIONS: I18n
Compiled on Jan 12 2014, 05:42:53.
Port /dev/ttyAMA0, 09:18:17
Press CTRL-A Z for help on special keys
Hi Pi! 18
...where the "Hi Pi!" number increments every 3 seconds.
If all is OK, its time to write a Gambas program for the Pi.
Using wiringPi in Gambas
After wiringPi has been downloaded, un-packed and built (as described in my previous posts) we need to add declarations to a new Gambas project, which I've called: Picaxe2Pi
' Gambas class file
Library "/usr/local/lib/libwiringPi" Public Extern wiringPiSetup() As Integer
Public Extern serialOpen(strPort As String, intBaud As Integer) As Integer
Public Extern serialClose(intFileDescriptor As Integer)
Public Extern serialGetchar(intFD As Integer) As Integer
...and declare a few global variables & constants:-
Public iFD As Integer
Const Pi_PORT As String = "/dev/ttyAMA0"
Const BAUD_RATE As Integer = 4800
Read data from Picaxe
I added to the form a couple of buttons, a label, a textarea control, and a timer. So when it runs it looks like this:-
the running Gambas test program |
I wont include all the detail, but when the program opens we need to run wiringPi setup:-
Public Sub Form_Open()
If (wiringPiSetup() = -1)
lblStatus.Text = "Failed to setup wiringPi. This program must be run as root"
Else
lblStatus.Text = "WiringPi initialised OK"
Endif
...then we need to open the serial port:-
Public Sub btnOpenSerial_Click()
iFD = serialOpen(Pi_PORT, BAUD_RATE)
If iFD < 0 Then
Me.Text = "Cannot open " & Pi_PORT
Else
Me.Text = Pi_PORT & " : " & CStr(iFD)
txtaSerial.Clear
tmrRead.Start
Endif
Catch
lblStatus.Text = "Open Serial port: " & Error.Text
End
...and then the timer runs and gets input from the port:-
Public Sub tmrRead_Timer()
Dim intTemp As Integer
If iFD > 0 Then
intTemp = serialGetchar(iFD)
If intTemp > 0 And intTemp < 256 Then 'to keep it simple!
txtaSerial.Text = txtaSerial.Text & Chr(intTemp)
Endif
Endif
End
I set the timer interval to 100ms, but I haven't done any checks to see if the system can actually maintain this rate...its only a test program!
And finally I can close the serial port like this:-
Public Sub btnCloseSerial_Click()
tmrRead.Stop
serialClose(iFD)
End
Well, its a start...more to follow!
No comments:
Post a Comment