Raspsberry Pi serial prj

Linux specific questions.
dasyar
Posts: 343
Joined: Dec 04, 2008 15:31

Raspsberry Pi serial prj

Postby dasyar » Oct 13, 2014 14:38

As noted in another thread, I am working with an RPi and my desktop PC. I have installed FBC on my RPi successfully and I am now starting a new project with the RPi.

I think first some baby steps, I have started the program below which I intend it to be command driven, so for instance if I type in a 'quit' command on the remote terminal, the program below will then end. In my other program, FBterm.bas, it was basically a char driven program, type in a char, and it gets displayed on the remote terminal.

So the first concept I am having difficulty with is comparing and responding to a string of characters which will be terminated with a CR. I know that in the C language they have a string compare command, which really simplified things, but it looks like in FB you have to capture the whole string, a char at a time, and then somehow compare it to the command string? Not sure as to how this would be done, and result in an expected manner. So, if somebody could give me a hint, the compare strings command perhaps, it would greatly appreciated. Or maybe there is a different way to do this then what I have shown below?

Thanks in advance


Code: Select all

'XBredun.bas

dim as string Key,buffer

Open Com "/dev/ttyUSB0:9600,n,8,1,cs0,ds0,cd0,rs" As #1
sleep 1000,1

if Err <> 0 then
   print "Error opening port";
   sleep 2000,1
   cls
   goto Cend
End if

print "Test program."
print #1, " XBredun program"

'Main serial receive and respond routine

Do
   Key = inkey$
   if Key = chr$(27) then ' Local Esc key
      exit do
   end if
   While Loc(1) > 0
      buffer = input$(loc(1),#1)
      'print #1, buffer;  ' debug, check if echo back
      if buffer = chr$(27) then ' Esc on remote terminal
         goto Cend
      end if
      if buffer = "quit" then
         goto Cend
      end if
      
   Wend
Sleep 1,0
Loop

Cend:
print #1, "Stopping XBredun";
Close

End
dasyar
Posts: 343
Joined: Dec 04, 2008 15:31

Re: Raspsberry Pi serial prj

Postby dasyar » Oct 14, 2014 17:16

So, I played around with the code below, and I think I have what I want, in a crude code fashion of course. I can now type in a command, and it seems to respond as expected. When I first started coding I did not realize(forgot) that there was a CR being appended to buffer1, so once I removed that CR, it seems to be working as expected.

Now the next step that I would like to do is somehow address the RPi GPIO, that way with a 'high' command, in my program below, I could turn on an LED that would be attached to the GPIO. Is there anybody doing any coding accessing the RPi GPIO directly with FB? I know that there are some Python and C libs for accessing the GPIO, does that mean that I would have to use Python or C as a middle ground to get to the GPIO?

I am also starting to get the feeling that there are not very many people here that are working with the Raspberry Pi, hopefully I can stir some interest, maybe not.

Code: Select all

'XBredun.bas

dim as string Key,buffer,buffer1

Open Com "/dev/ttyUSB0:9600,n,8,1,cs0,ds0,cd0,rs" As #1
sleep 1000,1

if Err <> 0 then
   print "Error opening port";
   sleep 2000,1
   cls
   goto Cend
End if

print "Test program."
print #1, " XBredun program"

'Main serial receive and respond routine

Do
   Key = inkey$
   if Key = chr$(27) then ' Local Esc key
      exit do
   end if
   While Loc(1) > 0
      buffer = input$(loc(1),#1)
      'print #1, buffer;  ' debug, check if echo back
      buffer1 = buffer1 + buffer  ' Add in keystroke
      if buffer = chr$(27) then ' Esc on remote terminal
         goto Cend
      end if
      if buffer = chr$(13) then  ' Check if CR
         print buffer1;
         buffer1 = rtrim(buffer1, chr$(13))  ' Strip off CR
         if buffer1 = "quit" then
            goto Cend
         end if
         if buffer1 = "help" then
            print #1, "Menu - help, quit, "
         end if
         buffer1 = ""
      end if
      
   Wend
Sleep 1,0
Loop

Cend:
print #1, "Stopping XBredun"
Close

End
TJF
Posts: 3384
Joined: Dec 06, 2009 22:27
Location: N47°, E15°

Re: Raspsberry Pi serial prj

Postby TJF » Oct 14, 2014 18:49

dasyar wrote:Now the next step that I would like to do is somehow address the RPi GPIO, that way with a 'high' command, in my program below, I could turn on an LED that would be attached to the GPIO. Is there anybody doing any coding accessing the RPi GPIO directly with FB? I know that there are some Python and C libs for accessing the GPIO, does that mean that I would have to use Python or C as a middle ground to get to the GPIO?

My current project is libpruio, which deals with GPIO (and ADC) stuff on the Beaglebone [Black] platforms. It uses the PRU coprocessors to control the hardware, which aren't present on RPi CPU.

Since RPi is running under LINUX it should be similar to BBB. You can either

  • conrrol GPIO by using the system file system (= sysfs, slow but easy), or
  • by direct access to the hardware registers (= mapped memory, fast but extravagantly)
I guess the best start is to translate a header for the available C library you mentioned.
dasyar
Posts: 343
Joined: Dec 04, 2008 15:31

Re: Raspsberry Pi serial prj

Postby dasyar » Oct 14, 2014 19:35

Below is a snippet fro a bash file that uses the RPi GPIO, I am wondering if using the Shell command could accomplish something similar? Maybe something like 'Shell " red gpio mode out gpio write red", or some kind similar structure. According to the FB manual, 'Shell' gets you into the system, and it looks like gpio is a segment of a command, just maybe a first stab at this could be done in this manner. Any thoughts?

Code: Select all

#
#   Gordon Henderson, June 2012

# Our lamps:
#   (These are wiringPi pin numbers)

     red=0
  yellow=1
   green=2
  redMan=3
greenMan=4

# The input button

button=8


# setup:
#   Program the GPIO correctly and initialise the lamps
#######################################################################

setup ()
{
  echo Setup
  for i in $red $yellow $green $redMan $greenMan ; do gpio mode  $i out ; done
  for i in $red $yellow $green $redMan $greenMan ; do gpio write $i   0 ; done
  gpio write $green  1
  gpio write $redMan 1
  gpio mode  $button in
}
dasyar
Posts: 343
Joined: Dec 04, 2008 15:31

Re: Raspsberry Pi serial prj

Postby dasyar » Oct 15, 2014 12:00

I did some playing around with trying to flash an LED on/off. In order for the snippet that I listed in the previous post to work, you have to install wiringPi for the RPi, a program that was written by Gordon Henderson. Once I did that, set up an LED on a breadboard that was connected to the RPi via a cable, I ran the below listed bash script to test it out. And the LED turned on, paused for two seconds, and then turned off, as expected.

Now the next thing I will have to do is replicate this procedure with an FB program, for a short test run. It looks to me that the wiringPi program, in the short example, it is using commands like 'gpio mode' and 'gpio write'. I will have to figure out, in the FB program, what would be the best way to run those commands to do the flash LED procedure.

When I downloaded wiringPi, from a git site, I had to do a 'make' procedure. I noticed that what was occurring was a bunch of C files were being compiled and placed into the wiringPi directory. I will have to check out that directory and see what other commands are available. Hopefully I will now be able to create a functional flash LED program written in FB.

Code: Select all

#!/bin/bash

red=6

setup
{
echo Setup
for i in $red ; do gpio mode $i out; done
for i in $red ; do gpio write $i 0; done
}
onled ()
{
gpio write $red 1
}
offled ()
{
gpio write $red 0
}
resttime ()
{
sleep 2
}

setup
onled
resttime
offled
dasyar
Posts: 343
Joined: Dec 04, 2008 15:31

Re: Raspsberry Pi serial prj

Postby dasyar » Oct 15, 2014 13:48

Below is a test run of tled6.bas, and yes it worked as expected. So at this point, using wiringPi, and using those commands within an FB is a possible direction to go if you want to use an FB program to access the gpio pins on the Raspberry Pi. I am sure that there will be some limitations and disappointments, but for the time being this gives a quick and dirty access to the RPi gpio.

I am still hoping to stumble across some FB code for dealing with a Sensirion sht11 module, this will be a more complicated accomplishment.

I am wondering if there is a more efficient way of doing the FB program below, in terms of maybe something other than 'Shell' command being used? I guess maybe one of you gurus could comment on that (thank you, thank you).

Code: Select all

'tled6.bas

' Setup the pin(s)
shell "gpio mode 6 out" ' Setup gpio pin 6
shell "gpio write 6 0"  ' Turn off gpio pin 6

' Run the program
shell "gpio write 6 1"  ' Turn on gpio pin 6
shell "sleep 2"  ' Sleep for two seconds
shell "gpio write 6 0"  ' Turn off gpio pin 6

End
dasyar
Posts: 343
Joined: Dec 04, 2008 15:31

Re: Raspsberry Pi serial prj

Postby dasyar » Oct 15, 2014 16:27

Below is a program that looks more like an FB program. The shorty example works as expected where it flashes the LED every half second continuously until you hit the 'Esc' to end the program.

Code: Select all

'tled6a.bas

dim as string Key

declare sub setup()
declare sub high()
declare sub low()


setup()

do
        Key = Inkey$
        if Key = chr$(27) then
                exit do
        end if
        high()
        sleep 500,0
        low()
        sleep 500,0

loop

End

sub setup()
        shell "gpio mode 6 out" ' Setup gpio pin 6
        shell "gpio write 6 0"  ' Turn off gpio pin 6
end sub

sub high()
        shell "gpio write 6 1"
end sub

sub low()
        shell "gpio write 6 0"
end sub
D.J.Peters
Posts: 7659
Joined: May 28, 2005 3:28

Re: Raspsberry Pi serial prj

Postby D.J.Peters » Oct 15, 2014 21:01

You need this fine C lib http://wiringpi.com/

Joshy

here are a simple FreeBASIC test: (not tested I'm on the PC atm.)
[code file=test01.bas]#include once "wirringpi.bi"

if wiringPiSetup()=-1 then
print "error: can't setup GPIO !"
beep : sleep : end
end if

print "blinking LED on pin 1"
pinMode(0, PIN_OUTPUT)
while inkey=""
digitalWrite (0,PIN_HIGH) : sleep 500
digitalWrite (0,PIN_LOW ) : sleep 500
wend[/code]Here are the inlcude file: "wirringpi.bi" (not tested I'm on the PC atm.)
[code file=wirringpi.bi]#ifndef __WIRINGPI_BI__
#define __WIRINGPI_BI__

/'
* wiringPi:
* Arduino compatable (ish) Wiring library for the Raspberry Pi
* Copyright (c) 2012 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
'/

' Handy defines

' Deprecated
#define NUM_PINS 17
#define WPI_MODE_PINS 0
#define WPI_MODE_GPIO 1
#define WPI_MODE_GPIO_SYS 2
#define WPI_MODE_PIFACE 3
#define WPI_MODE_UNINITIALISED -1

' Pin modes
#define PIN_INPUT 0
#define PIN_OUTPUT 1
#define PIN_PWM_OUTPUT 2
#define PIN_GPIO_CLOCK 3
#define PIN_LOW 0
#define PIN_HIGH 1

' Pull up/down/none
#define PUD_OFF 0
#define PUD_DOWN 1
#define PUD_UP 2

' PWM
#define PWM_MODE_MS 0
#define PWM_MODE_BAL 1

' Interrupt levels
#define INT_EDGE_SETUP 0
#define INT_EDGE_FALLING 1
#define INT_EDGE_RISING 2
#define INT_EDGE_BOTH 3

' Threads
type THREAD_T as sub cdecl (byval dummy as any ptr)

' Function prototypes
' c++ wrappers thanks to a comment by Nick Lott
' (and others on the Raspberry Pi forums)

extern "c" lib "wiringPi"

' Basic wiringPi functions
declare function wiringPiSetup () as integer
declare function wiringPiSetupSys () as integer
declare function wiringPiSetupGpio () as integer
declare function wiringPiSetupPiFace () as integer

declare function piBoardRev() as integer
declare function wpiPinToGpio (byval wpiPin as integer) as integer
declare function wiringPiSetupPiFaceForGpioProg () as integer ' Don't use this - for gpio program only
declare sub pinMode (byval pin as integer, byval mode as integer)
declare function getAlt (byval pin as integer) as integer
declare sub pullUpDnControl (byval pin as integer, byval pud as integer)
declare sub digitalWrite (byval pin as integer, byval value as integer)
declare sub digitalWriteByte (byval value as integer)
declare sub gpioClockSet (byval pin as integer, byval freq as integer)
declare sub pwmWrite (byval pin as integer, byval value as integer)
declare sub setPadDrive (byval group as integer, byval value as integer)
declare function digitalRead (byval pin as integer) as integer
declare sub pwmSetMode (byval mode as integer)
declare sub pwmSetRange (byval range as uinteger)
declare sub pwmSetClock (byval divisor as integer)

' Interrupts
declare function waitForInterrupt (byval pin as integer, byval mS as integer) as integer
declare function wiringPiISR (byval pin as integer, byval mode as integer, byval tsr as sub ()) as integer

' Threads
declare function piThreadCreate (byval fn as THREAD_T) as integer
declare sub piLock (byval key as integer)
declare sub piUnlock (byval key as integer)

' Schedulling priority
declare function piHiPri (byval pri as integer) as integer

' Extras from arduino land
declare sub delay (byval howLong as uinteger)
declare sub delayMicroseconds (byval howLong as uinteger)
declare function millis () as uinteger
declare function micros () as uinteger

end extern

#endif ' __WIRINGPI_BI__[/code]
dasyar
Posts: 343
Joined: Dec 04, 2008 15:31

Re: Raspsberry Pi serial prj

Postby dasyar » Oct 16, 2014 10:24

Thanks D.J.Peters, that sure gets rid of using the 'Shell' command. I have not tried this yet, but it looks like, I sure hope so, that it should work. This makes things a little more manageable, thanks again. I hope this will get more people interested in trying out the RPi using freeBasic of course.
dasyar
Posts: 343
Joined: Dec 04, 2008 15:31

Re: Raspsberry Pi serial prj

Postby dasyar » Oct 16, 2014 11:19

I just tried out the code, the new wirringpi.bi file, and it worked as expected. I guess now I should place the wirring.bi in the FBC directory where all the other .bi files are kept?

As a recap, you have to install the wiringPi program, in the D.J.Peters post, he has a link, and then you can proceed from there. Looking at the wirringpi.bi, that kind of looks like the wiringPi.h file that is used for wiringPi.c, is that correct?

Yesterday I went to the Raspberry Pi forum and did a search for freebasic, only one item showed up that linked back to the freebasic basic forum, you would think that their would be more interest in using freebasic on the Raspberry Pi forum.
dasyar
Posts: 343
Joined: Dec 04, 2008 15:31

Re: Raspsberry Pi serial prj

Postby dasyar » Oct 16, 2014 13:10

// bcm2835.c
// C and C++ support for Broadcom BCM 2835 as used in Raspberry Pi
// http://elinux.org/RPi_Low-level_peripherals
// http://www.raspberrypi.org/wp-content/u ... herals.pdf
//
// Author: Mike McCauley
// Copyright (C) 2011-2013 Mike McCauley
// $Id: bcm2835.c,v 1.14 2013/12/06 22:24:52 mikem Exp mikem $

The above is from the bcm2835 program that I had set up to access the sht11 module back some time ago. I also have the example C program that was included to do a test run of the sht11 module. Since the bcm2835 program needs to be loaded to RPi, can a .bi file be created to get access to this program? I noticed that with this program you an also setup the pins, write to the pins, ..., etc. I wonder how this compares to wiringPi program, better, worse...?
dasyar
Posts: 343
Joined: Dec 04, 2008 15:31

Re: Raspsberry Pi serial prj

Postby dasyar » Oct 17, 2014 11:28

I created a quick and dirty little prelim sheet that should print out in an 80 char wide format. I like to have some things on paper so I can jot notes or some other kind of scribbling. Not sure if this will grow in size or not.

*******************************************************************************
wiringPi
Gordon Henderson, June 2012

When using freeBasic remember to use #include once "wirringpi.bi"
This is a typical freeBasic example program for blinking an LED. In the example
below it uses pin 6.

#include once "wirringpi.bi"

if wiringPiSetup()=-1 then
print "error: can't setup GPIO !"
beep : sleep : end
end if

print "blinking LED on pin 6"
pinMode(6, PIN_OUTPUT)
while inkey=""
digitalWrite (6,PIN_HIGH) : sleep 500
digitalWrite (6,PIN_LOW ) : sleep 500
wend

****************
Core functions
****************

•void pinMode (int pin, int mode) ;

This sets the mode of a pin to either INPUT, OUTPUT, PWM_OUTPUT or GPIO_CLOCK.
Note that only wiringPi pin 1 (BCM_GPIO 18) supports PWM output and only
wiringPi pin 7 (BCM_GPIO 4) supports CLOCK output modes.

This function has no effect when in Sys mode. If you need to change the pin
mode, then you can do it with the gpio program in a script before you start
your program.

freeBasic - example
pin mode
pinMode(0, PIN_OUTPUT)

•void pullUpDnControl (int pin, int pud) ;

This sets the pull-up or pull-down resistor mode on the given pin, which should
be set as an input. Unlike the Arduino, the BCM2835 has both pull-up an down
internal resistors. The parameter pud should be; PUD_OFF, (no pull up/down),
PUD_DOWN (pull to ground) or PUD_UP (pull to 3.3v) The internal pull up/down
resistors have a value of approximately 50KO on the Raspberry Pi.

This function has no effect on the Raspberry Pi’s GPIO pins when in Sys mode.
If you need to activate a pull-up/pull-down, then you can do it with the gpio
program in a script before you start your program.

•void digitalWrite (int pin, int value) ;

Writes the value HIGH or LOW (1 or 0) to the given pin which must have been
previously set as an output.

WiringPi treats any non-zero number as HIGH, however 0 is the only
representation of LOW.

freeBasic - example
pin mode
digitalWrite (0,PIN_HIGH)
digitalWrite (0,PIN_LOW )

•void pwmWrite (int pin, int value) ;

Writes the value to the PWM register for the given pin. The Raspberry Pi has
one on-board PWM pin, pin 1 (BMC_GPIO 18, Phys 12) and the range is 0-1024.
Other PWM devices may have other PWM ranges.

This function is not able to control the Pi’s on-board PWM when in Sys mode.

•int digitalRead (int pin) ;

This function returns the value read at the given pin. It will be HIGH or
LOW (1 or 0) depending on the logic level at the pin.

•analogRead (int pin) ;

This returns the value read on the supplied analog input pin. You will need to
register additional analog modules to enable this function for devices such as
the Gertboard, quick2Wire analog board, etc.

•analogWrite (int pin, int value) ;

This writes the given value to the supplied analog pin. You will need to
register additional analog modules to enable this function for devices such as
the Gertboard.

****************
Setup functions
****************
The differences between the setup functions are as follows:

•wiringPiSetup (void) ;

This initialises wiringPi and assumes that the calling program is going to be
using the wiringPi pin numbering scheme. This is a simplified numbering scheme
which provides a mapping from virtual pin numbers 0 through 16 to the real
underlying Broadcom GPIO pin numbers. See the pins page for a table which maps
the wiringPi pin number to the Broadcom GPIO pin number to the physical location
on the edge connector.

This function needs to be called with root privileges.

freeBasic - example
if wiringPiSetup()=-1 then
print "error: can't setup GPIO !"
beep : sleep : end
end if

•wiringPiSetupGpio (void) ;

This is identical to above, however it allows the calling programs to use the
Broadcom GPIO pin numbers directly with no re-mapping.

As above, this function needs to be called with root privileges, and note that
some pins are different from revision 1 to revision 2 boards.

•wiringPiSetupPhys (void) ;

Identical to above, however it allows the calling programs to use the physical
pin numbers on the P1 connector only.

As above, this function needs to be called with root priviliges.

•wiringPiSetupSys (void) ;

This initialises wiringPi but uses the /sys/class/gpio interface rather than
accessing the hardware directly. This can be called as a non-root user provided
the GPIO pins have been exported before-hand using the gpio program. Pin
numbering in this mode is the native Broadcom GPIO numbers – the same as
wiringPiSetupGpio() above, so be aware of the differences between Rev 1 and
Rev 2 boards.

Note: In this mode you can only use the pins which have been exported via the
/sys/class/gpio interface before you run your program. You can do this in a
separate shell-script, or by using the system() function from inside your
program to call the gpio program.

Also note that some functions have no effect when using this mode as they’re not
currently possible to action unless called with root privileges. (although you
can use system() to call gpio to set/change modes if needed)
*******************************************************************************
dasyar
Posts: 343
Joined: Dec 04, 2008 15:31

Re: Raspsberry Pi serial prj

Postby dasyar » Oct 18, 2014 12:53

Now that I have updated my FBterm program, I can devote some more time to FB and Raspberry Pi. I have a couple of projects in mind. I started this thread with a program to create an FB program to make a user interactive scenario where you can control some devices attached to the RPi, local and remote, in the simplest form, an LED. So, I will continue with that and progress to, hopefully being able to access a sht11 module for getting temp and humidity readings. Hopefully the RPi will be a stand-alone unit, with access via lan and XBee, more emphasis on the XBee remote part. More to come ...
dasyar
Posts: 343
Joined: Dec 04, 2008 15:31

Re: Raspsberry Pi serial prj

Postby dasyar » Oct 19, 2014 19:44

The code below is a proof-of-concept FB program to see if I am getting things to work as expected. I have a Raspberry Pi B+ setup with WiFi and an XBee module in a room about sixty feet away from my main Windows 7 PC desktop. On my desktop I can now startup FBterm and connect to the RPi via XBee, successfully. I can also turn on/off the LED on the RPi via the XBee, successfully. So far things are working as expected, next I have to figure out how to get the sht11 module functioning, hopefully via FB code.

Any ideas as to how to improve my code would be greatly appreciated.

Code: Select all

'XBsun.bas
#include once "wirringpi.bi"

' Check to see if wiringPi is setup
if wiringPiSetup()=-1 then
   print "error: Can't setup GPIO!"
   sleep : end
end if

' Declare Subs
declare sub onled()
declare sub offled()

' Declare variables
dim as string Key,buffer,buffer1

' Open the com port, hard coded for /dev/ttyUSB0
open com "/dev/ttyUSB0:9600,n,8,1,cs0,ds0,cd0,rs" as #1
sleep 1000,1
' Check if port is available
if Err <> 0 then
   print "error: Can't open port!"
   sleep 2000,1
   cls
   goto Cend
end if

'**********
' Main
print "Started program.";
print #1, " XBsun"

do
' Check locally for Esc key press
   Key = inkey$
   if Key = chr$(27) then
      exit do
   end if
   
   while Loc(1) > 0
      buffer = input$(loc(1),#1)
      buffer1 = buffer1 + buffer
      if buffer = chr$(27) then   ' Check for Esc key press, remote
         goto Cend
      end if
      if buffer = chr$(13) then
         print buffer1;
         buffer1 = rtrim(buffer1, chr$(13))  ' End of str CR
         if buffer1 = "quit" then  ' Check for 'quit' command
            goto Cend
         end if
         if buffer1 = "onled" then  ' Check for 'onled' command
            onled()
         end if
         if buffer1 = "offled" then  ' Check for 'offled' command
            offled()
         end if
         buffer1 = ""
      end if
   wend
sleep 1,0
loop
'**********

Cend:
close  ' Close open com port
end

' Functions and subRoutines
' Turn on LED on pin 6
sub onled()
   pinMode(6, PIN_OUTPUT)
   digitalWrite(6,PIN_HIGH)
end sub

' Turn off LED on pin 6
sub offled()
   pinMode(6, PIN_OUTPUT)
   digitalWrite(6,PIN_LOW)
end sub
dasyar
Posts: 343
Joined: Dec 04, 2008 15:31

Re: Raspsberry Pi serial prj

Postby dasyar » Oct 20, 2014 11:18

I made a couple of updates to the program, and it runs as expected.

A question for the gurus, how can I have the program select an open and available com port? Since the RPi B+ has four USB ports plus a UART(/dev/ttyAMA0), and I am not sure how the OS assigns the USB devices on start up, I would like to move away from a hard coded com port. I think it makes more sense for the program to check and see what is open and available, can FB do something like that?

Next question, I noticed when I have #include once "wirringpi.bi", I have to have wirringpi.bi file in the directory that I am working in, but when I added #include "vbcompat.bi", I did not have to have the file in my working directory?

Code: Select all

'XBsun.bas
' Version 1.0
' Oct 20, 2014
'
' UI program for Raspbery Pi using an XBee on COM port
'

#include once "wirringpi.bi"  ' wiringPi
#include "vbcompat.bi"        ' Date function

' Check to see if wiringPi is setup
if wiringPiSetup()=-1 then
   print "error: Can't setup GPIO!"
   sleep : end
end if

' Declare Subs
declare sub onled()
declare sub offled()
declare sub menu()

' Declare functions


' Declare variables
dim as string Key,buffer,buffer1
dim SystemDate as double = Now()  ' Date

' Open the com port, hard coded for /dev/ttyUSB0
open com "/dev/ttyUSB0:9600,n,8,1,cs0,ds0,cd0,rs" as #1
sleep 1000,1
' Check if port is available
if Err <> 0 then
   print "error: Can't open port!"
   sleep 2000,1
   cls
   goto Cend
end if

'**********
' Main
print "Started program.";
print #1, " XBsun"

do
' Check locally for Esc key press
   Key = inkey$
   if Key = chr$(27) then
      exit do
   end if

' Check for incoming on com port   
   while Loc(1) > 0
      buffer = input$(loc(1),#1)
      buffer1 = buffer1 + buffer
      if buffer = chr$(27) then   ' Check for Esc key press, remote
         goto Cend
      end if
      if buffer = chr$(13) then
         print buffer1;
         buffer1 = rtrim(buffer1, chr$(13))  ' End of str CR
         if buffer1 = "quit" then  ' Check for 'quit' command
            goto Cend
         'end if
         elseif buffer1 = "onled" then  ' Check for 'onled' command
            onled()
         'end if
         elseif buffer1 = "offled" then  ' Check for 'offled' command
            offled()
         'end if
         elseif buffer1 = "help" then
            menu()
         'end if
         elseif buffer1 = "date" then            
            print #1, format(SystemDate, "mm/dd/yyyy hh:mm:ss")
         else
            print #1, "Unknown Command"
         end if
         buffer1 = ""
      end if
   wend
''''''''''''''''''''
   
sleep 1,0
loop
'**********

Cend:
print #1, "Program stop"
close  ' Close open com port
end

' Functions and subRoutines
' Turn on LED on pin 6
sub onled()
   pinMode(6, PIN_OUTPUT)
   digitalWrite(6,PIN_HIGH)
end sub

' Turn off LED on pin 6
sub offled()
   pinMode(6, PIN_OUTPUT)
   digitalWrite(6,PIN_LOW)
end sub

sub menu()
   print #1, "Menu - help, quit, onled, offled, date "
   print #1, "quit stops the program"
end sub

Return to “Linux”

Who is online

Users browsing this forum: No registered users and 3 guests