Menu

QB64 meets GCBASIC

2020-01-12
2022-02-07
  • Chris Roper

    Chris Roper - 2020-01-12

    Many of us here got started in programming back in the 80’s and 90’s with versions of Microsoft Quick BASIC. When windows came along Quick BASIC was expanded to work with event driven code and was relaunched as Visual BASIC. With Visual BASIC it was almost as easy to create Windows Apps as it was create DOS apps in Quick BASIC.

    Then along came the age of complex and bloated IDE’s, API's such as .NET and Languages such as C# as a result Windows programming became a dark art, with BASIC consigned to the bin as old and slow.

    But all is not lost. Just as the GCBASIC community have been proving to the world that a QuickBASIC inspired language can create code that rivals C compilers for efficiency on Microcontrollers, another Team has been keeping the QuickBASIC Compiler alive and up to date in the form of QB64.

    QB64

    Anyone of my era will instantly recognise this interface, as it was the precursor to the modern IDE, and feel right at home programming in QB64, only referring to the Wiki to learn of the myriad of advanced features that have been added over the years.

    Whilst earlier releases of QuickBASIC had issues with Serial communications because of changes to the Windows policies over the years QB64 seems to have overcome them.

    Getting QB64 communicating with a Microcontroler was easy using Serial Communications as this loopback example shows:

    Here is the QB64 Code:

    DIM InChar AS STRING * 1
    OPEN "COM6:19200,N,8,1,BIN,CS0,DS0" FOR RANDOM AS #1
    DO 'main loop
        k$ = INKEY$ ' Get Key press
        IF LEN(k$) = 1 THEN
            PUT #1, , k$ 'Send to Microcontroller
        END IF
        'receive data is in buffer when LOC > 0
    
        IF LOC(1) THEN
            GET #1, , InChar ' read character returned by Microcontroller
            IF ASC(InChar) >= 32 THEN PRINT InChar; ' display it on the screen
        END IF
    LOOP
    CLOSE #1
    

    And the corresponding Microcontroller code:

    #chip 16f18855,32
    #option explicit
    #Config MCLRE_ON
    '
    #startup InitPPS, 85
    
    Sub InitPPS
      'Module: EUSART
      RXPPS  = 0x0011    'RC1 < RX
      RC0PPS = 0x0010    'TX  > RC0
    End Sub
    
    #define DefaultUsartReturnValue = 0 ' ASCII NULL Chr
    #define USART_BAUD_RATE 19200
    
    Do
      HSerSend(HSerReceive)   ' Loopback
    loop
    

    Program the Microcontroller then use the device manager to detect the com: port number assigned.

    Place the com: Port number in the QB64 Open statement, (I have not written code to detect or select it yet) then RUN the code in the QB64 Window.

    A blank Console Window will open and anything typed on the keyboard will be sent to the microcontroller, then returned to the windows program for display in the Console Window.

    Here is the resultant Window Echoing what is typed:

    Having seen how easy it is to get a Microcontroler talking to a Windows Console app the obvious next goal is to talk to an actual windows app and for that we need a spell to counter the Dark Arts that Windows Programing became back in the 90’s

    One of the developers over at QB64 has released InForm A GUI engine and WYSIWYG interface designer for QB64. The name is a mouthful but InForm is a magical tool that lifts QB64 from being just a BASIC Compiler for console and DOS applications into a reincarnation of VisualBASIC.

    So armed with a modern iteration of the QuickBASIC compiler (QB64) and a VisualBASIC like design tool (InForm) creating Windows Applications that interact with GCBASIC applications on a Microcontroller (or Arduino Board) is a step closer to reality for the average BASIC programmer.

    And in order to test the interaction of InForm, QB64 and GCBASIC that is exactly what I did.

    Using InForm I created a Windows representation of the controls found on the Xpress Evaluation Board and mapped them to Controls on a Windows Form:

    RA0 <--> RA3 are Check Boxes under the users control, Clicking on one of them will toggle the corresponding LED on the Xpress Board showing that the Windows User can control the Microcontrollers ports. RA5 is also a Check Box Control but is Disabled so that the Windows user can not select it in the display, however, RA5 will appear ticked whenever the button S2 on the Xpress board is pressed showing that the Microcontroller can also control the Windows App. Finally the Status Bar control at the bottom of the window will reflect the ADC Reading of the POT R15 showing the potential for Real Time data Acquisition and display.

    The GCBASIC code is simple:

    ' ----- Configuration
    'Chip Settings.
    #chip 16f18855,32
    #option explicit
    #Config MCLRE_ON
    '
    #startup InitPPS, 85
    
    Sub InitPPS
      'Module: EUSART
      RXPPS  = 0x0011    'RC1 < RX
      RC0PPS = 0x0010    'TX  > RC0
    End Sub
    
    #define NUL 0                          ' ASCII Null Character
    #define DefaultUsartReturnValue = NUL  ' Override default of nbsp
    #define USART_BAUD_RATE 19200
    Dir PortA.0 out
    Dir PortA.1 out
    Dir PortA.2 out
    Dir PortA.3 out
    Dir PortA.5 in
    
    Dim InChar as Byte
    Dim GhostA as Byte
    Dim PotPos as Byte
    Dim PotDat as Byte
    
    GhostA = PortA
    
    Do
      InChar = HSerReceive
      ' Process Inbound controls
      Select case InChar
        Case "1"
          PortA.0 = NOT PortA.0
        Case "2"
          PortA.1 = NOT PortA.1
        Case "3"
          PortA.2 = NOT PortA.2
        Case "4"
          PortA.3 = NOT PortA.3
      end select
      ' Send Outbound Controls
      If GhostA.5 <> PortA.5 then
        GhostA = PortA
        InChar = "5"
        HSerSend InChar
      end if
      ' Send new Analog Data if Pot Position has changed
      wait 25 ms : PotDat = ReadAD(AN4 ,TRUE)
      if PotPos <> PotDat then
        HSerSend "4"
        PotPos = PotDat
        HSerSend PotPos
      end if
    Loop
    

    The QB64 Code Looks a lot more complex because of all of the event handling code that is required by Windows but the good news is that most of the following code is generated for you by InForm. Notice also that a lot of it is unused boiler plate code such as the empty Select Statements.I have left them in the code below to show you the kind of events that your program can respond too even though my code is not using them.

    ': This program uses
    ': InForm - GUI library for QB64 - v1.1
    ': Fellippe Heitor, 2016-2019 - fellippe@qb64.org - @fellippeheitor
    ': https://github.com/FellippeHeitor/InForm
    '-----------------------------------------------------------
    
    ': Controls' IDs: ------------------------------------------------------------------
    DIM SHARED XpressEvaluationBoard AS LONG
    DIM SHARED RA0CB AS LONG
    DIM SHARED RA1CB AS LONG
    DIM SHARED RA2CB AS LONG
    DIM SHARED RA3CB AS LONG
    DIM SHARED RA4PB AS LONG
    DIM SHARED RA5CB AS LONG
    DIM SHARED PxStr AS STRING * 1 'one byte transfers
    
    ': External modules: ---------------------------------------------------------------
    '$INCLUDE:'InForm\InForm.ui'
    '$INCLUDE:'InForm\xp.uitheme'
    '$INCLUDE:'QB64_Xpress_Com6.frm'
    
    ': Event procedures: ---------------------------------------------------------------
    SUB __UI_BeforeInit
    END SUB
    
    SUB __UI_OnLoad
        OPEN "COM6:115200,N,8,1,BIN,CS0,DS0" FOR RANDOM AS #1
    END SUB
    
    SUB __UI_BeforeUpdateDisplay
        'This event occurs at approximately 30 frames per second.
        'You can change the update frequency by calling SetFrameRate DesiredRate%
        IF LOC(1) THEN
            GET #1, , PxStr
            IF PxStr = "4" THEN
                GET #1, , PxStr
                Control(RA4PB).Value = ASC(PxStr)
            END IF
            IF PxStr = "5" THEN
                Control(RA5CB).Value = NOT Control(RA5CB).Value
            END IF
      END IF
    END SUB
    
    SUB __UI_BeforeUnload
        'If you set __UI_UnloadSignal = False here you can
        'cancel the user's request to close.
        CLOSE #1
    END SUB
    
    SUB __UI_Click (id AS LONG)
        SELECT CASE id
            CASE RA0CB
                PxStr = "0"
            CASE RA1CB
                PxStr = "1"
            CASE RA2CB
                PxStr = "2"
            CASE RA3CB
                PxStr = "3"
        END SELECT
    END SUB
    
    SUB __UI_MouseEnter (id AS LONG)
        SELECT CASE id
            CASE XpressEvaluationBoard
            CASE RA0CB
            CASE RA1CB
            CASE RA2CB
            CASE RA3CB
            CASE RA4PB
            CASE RA5CB
        END SELECT
    END SUB
    
    SUB __UI_MouseLeave (id AS LONG)
        SELECT CASE id
            CASE XpressEvaluationBoard
            CASE RA0CB
            CASE RA1CB
            CASE RA2CB
            CASE RA3CB
           CASE RA4PB
            CASE RA5CB
        END SELECT
    END SUB
    
    SUB __UI_FocusIn (id AS LONG)
        SELECT CASE id
            CASE RA0CB
            CASE RA1CB
            CASE RA2CB
            CASE RA3CB
            CASE RA5CB
        END SELECT
    END SUB
    
    SUB __UI_FocusOut (id AS LONG)
        'This event occurs right before a control loses focus.
        'To prevent a control from losing focus, set __UI_KeepFocus = True below.
        SELECT CASE id
            CASE RA0CB
           CASE RA1CB
            CASE RA2CB
            CASE RA3CB
            CASE RA5CB
        END SELECT
    END SUB
    
    SUB __UI_MouseDown (id AS LONG)
        SELECT CASE id
            CASE XpressEvaluationBoard
            CASE RA0CB
            CASE RA1CB
            CASE RA2CB
            CASE RA3CB
            CASE RA4PB
            CASE RA5CB
        END SELECT
    END SUB
    
    SUB __UI_MouseUp (id AS LONG)
        SELECT CASE id
            CASE XpressEvaluationBoard
            CASE RA0CB
            CASE RA1CB
            CASE RA2CB
            CASE RA3CB
            CASE RA4PB
            CASE RA5CB
        END SELECT
    END SUB
    
    SUB __UI_KeyPress (id AS LONG)
        'When this event is fired, __UI_KeyHit will contain the code of the key hit.
        'You can change it and even cancel it by making it = 0
        SELECT CASE id
            CASE RA0CB
            CASE RA1CB
            CASE RA2CB
            CASE RA3CB
            CASE RA5CB
        END SELECT
    END SUB
    
    SUB __UI_TextChanged (id AS LONG)
        SELECT CASE id
        END SELECT
    END SUB
    
    SUB __UI_ValueChanged (id AS LONG)
        SELECT CASE id
            CASE RA0CB
            CASE RA1CB
            CASE RA2CB
            CASE RA3CB
            CASE RA5CB
        END SELECT
    END SUB
    
    SUB __UI_FormResized
    END SUB
    

    Striping out the unused sections of the above for claraty, the only code segments I actually wrote, as opposed to InForm Generated are as follows:

    Open a communications channel when the Window Loads:

    SUB __UI_OnLoad
        OPEN "COM6:115200,N,8,1,BIN,CS0,DS0" FOR RANDOM AS #1
    END SUB
    

    Read inbound control Data:

    SUB __UI_BeforeUpdateDisplay
        'This event occurs at approximately 30 frames per second.
        'You can change the update frequency by calling SetFrameRate DesiredRate%
        IF LOC(1) THEN
            GET #1, , PxStr
            IF PxStr = "4" THEN
                GET #1, , PxStr
                Control(RA4PB).Value = ASC(PxStr)
            END IF
            IF PxStr = "5" THEN
                Control(RA5CB).Value = NOT Control(RA5CB).Value
            END IF
      END IF
    END SUB
    

    Send Outbound Control Data:

    SUB __UI_Click (id AS LONG)
        PxStr = ""
        SELECT CASE id
            CASE RA0CB
                PxStr = "1"
            CASE RA1CB
                PxStr = "2"
            CASE RA2CB
                PxStr = "3"
            CASE RA3CB
                PxStr = "4"
        END SELECT
        PUT #1, , PxStr
    END SUB
    

    And finally Close the communications channel when the Window closes:

    SUB __UI_BeforeUnload
        'If you set __UI_UnloadSignal = False here you can
        'cancel the user's request to close.
        CLOSE #1
    END SUB
    

    As you can see the Windows user code is not much longer than the GCBASIC code.

    The images below attempt to show the resultant system in action:


    Conclusion:

    Whilst the above may have only scratched the surface of what is possible it clearly shows that with three, open source BASIC Tools - GreatCowBASIC, QB64 and InForm - developing Windows Applications along side your Microcontroller Applications is no longer the domain of Bloated IDE’s, Complex Control Languages and obscure libraries.

    So if you are comfortable with BASIC you can do it all in BASIC and just like the 90's the tools are free and bloated IDE’s are not required.

    Give it a try, your Microcontroller applications will benefit and who knows, you may end up being a Windows Programer too.....

    Footnote:

    QB64 is available for Windows XP through Windows 10, Linux, MacOS and Raspberry PI. I am not aware that InForm has or will be added for those Platforms but the more people use it the more likely a port will become.

    Update:
    Fellippe Heitor QB64 Developer and the Creator of InForm has contacted me with the excellent news that:
    “InForm is available in whatever platform QB64 works, so there you have it.”

    Thanks Fellippe, we have several Mac, Linux and Pi users here that will be eager to try it out.

    Disclaimer:

    I am a Great Cow BASIC Developer but have no experience with QB64 or InForm beyond the examples I posted in this article. I suspect, however, that I may delve much deeper into these excellent tools.

    Cheers
    Chris

    Links to the Programs and Forums mentioned above:

    QB64 download - https://www.portal.qb64.org/
    QB64 Forum - https://www.qb64.org/forum/

    InForm download - https://www.qb64.org/inform/
    InForm forum - https://www.qb64.org/forum/index.php?board=11.0

    GCBASIC - http://gcbasic.sourceforge.net/Typesetter/index.php/Home
    GCBASIC Forum - https://sourceforge.net/p/gcbasic/discussion/

     

    Last edit: Chris Roper 2020-01-12
  • mmotte

    mmotte - 2020-01-12

    Thank You Chris,
    I am always looking for ways to make these microcontrollers useful. The interface to a PC or web is always a big step. Over the years i have done it through several methods :
    1) using excel with VBA
    2) using php and inserting data into a DB
    3) using C# for web page interface
    4) using already made bluetooth apps on cellphone

    I certainly will be looking at QB64 and how I can use it. thanks for the heads up and examples. I esp appreciate examples.

    Thanks
    mike w9ys

     
    • Chris Roper

      Chris Roper - 2020-01-12

      A pleasure MIke and a heads up, some errors had crept into the posted code snippets during editing so I have updated and tested all of the example code again to ensure that it is reproducible and corrected or clarified certain parts of the text.

      Time and talent permitting my next Project may well be a debug tool for GCBASIC using all three tools to the fullest.

      Cheers
      Chris

       

      Last edit: Chris Roper 2020-01-12
  • David Stephenson

    Yes I've been trying to push QB64 here with no success. It is a pity GCBasic has gone with freebasic.
    The only slight downside is the QB64 compiles the code into C+ but it does not seem to slow things down much.
    It would seem that it is still under active development as new features keep getting added I particularly like the range of variable types. The original developer (along with his website) has disappeared and the qbasic forum which started it off lost its moderator (Mac) over a decade ago and has now had to migrate to "taptalk" after the shut-down of network54.
    Yes I did spent a lot of time on that forum.
    As I said in another thread I tried several versions of basic to allow me to download serial data ( at fast speed >200kBaud) and QB64 was the only one that worked.

     
    • Chris Roper

      Chris Roper - 2020-01-12

      I am pleased that others have used and are using QB64. I saw it mentioned in a post last week so decided to give it a try, you may well have been the source.

      GCBASIC has not "Gone with FreeBasic" as such, but It is Written in FreeBASIC as that offered the Portability at the time so as a result people tend to use FreeBASIC to “Try” and talk to GCBASIC.

      I see the release of InForm as the catalyst that is required to get more users, both here and elsewhere, into QB64 as it offers an easy introduction to Windows Programming via InForm along with the ability to communicate with external hardware, all in a BASIC dialect that is not too far removed from GCBASIC.

      Cheers
      Chris

       
  • Jim giordano

    Jim giordano - 2020-01-13

    To post back on topic, I tried QB64 and Inform.
    QB64 is interesting, the ui is very nostalgic, but also seems too archaic these days. It seems to imply it is cross platform, but I'm not sure if it means you can get QB64 for any platform, or that it produces binaries that can be run on any platform from the same code. If the latter, it is certainly worth looking into.
    Inform I didn't care for. I struggled to get it to do what I wanted. Even though I went in and changed the colors, it still gave me terrible blue push-buttons.

    I find fbedit for freebasic much easier to use for designing forms and gui programs. Also with freebasic you just make standard windows api calls.

     
    • stan cartwright

      stan cartwright - 2020-01-13

      If you only use basic then freebasic is best and cross platform but again the fbide isn't.
      Does old vb work in win 10 but what about Tiny basic?
      All I want is to code a microcontroller.

       
  • David Stephenson

    It may look dated, but it works. I think the original intension for the IDE was to get as close as possible to the original (now it's a bit more colourful with highlighting and such).
    I seem to remember that there was some resistance to an IDE as many users liked to use notepad.
    I've written a program in QB64 that calculates NMR spectra (that is doing quantum mechanics) and it's fairly quick.

     

    Last edit: David Stephenson 2020-01-14
  • mmotte

    mmotte - 2020-01-14

    Been playing with QB64 for couple nights . loaded some examples to get the feel for the syntax and commands. I have a project that I am going to try so I was looking at the plotting routines. Use a GCB program in a PIC to send data to the QB64 program and plot the points on the PC

    Last night I downloaded QB64 to Linux Mint. I ran the install sh script from terminal, it installed . And there was the QB64 program Icon. Clicked on it and it instantly ran. Clicked on files tab > Open to go to some of the demo programs. This was the only thing that took a few seconds to learn to navingate. Selected a program and pressed OK. Ran the program and it opened a window and ran, no problem. Very slick!

    mike w9ys

     
  • stan cartwright

    stan cartwright - 2020-01-14

    So it's about connecting a pic to windows to show data?
    There was another basic mentioned in the forum..ms small basic? that did the same I think but I can't find it. It was a small program suggested by one of the gcb team.

     
    • Anobium

      Anobium - 2020-01-14

      Microsoft Small BASIC - not a patch on the approach discussed here. Microsoft Small BASIC has very complex issues with the UI to display comports, and, Microsoft Small BASIC is not portable.

       
  • stan cartwright

    stan cartwright - 2020-01-14

    This subject has been a bit hard to follow.
    Is it uses a pic program that connects to a QB64 program via serial and the QB64 can display/manipulate the pic data sent and display it in a pc window?
    You guys get so technical sometimes.

     
  • Boris Breuer

    Boris Breuer - 2020-01-14

    Hey there - for GUI things etc. I had a look at Gambas - it's an interpreter with Syntax similar to visual basic. Sadly, it ist a linux thing. Didn't see a port to windows or mac. But i find it promising because it has an automatical approach for installation routines at least for debian based-distros. There is a binding for an editor component (qt I think) but don't know how capable this is and how the performance would be.

    For this multiplattform thingy :) I am a bit in love with the lazarus/freepascal-project but must admit, that I didn't do much the last time with it. I have a project in mind with it for a small board computer with an imx233, a tft-display and a clock-modul DS-something... With lazarus there is a port of synwrite for multiplattform (Win/Lin/Mac) - theoretically it should be feasible to get an IDE for Great Cow BASIC for those plattforms with it, but it would take a change to the Pascal language and would mix things a bit up. And, honestly, I do not know how exactly a new language description for the syntax highlighter etc. would be crafted out - looked complex. To bring data to a visual context, there are indeed plenty of gui-elements with lazarus. Even a report-generator (freereport). Maybe this could be worth a consideration? By the way, this would also be kind of funny, because they have a cross-compiler for AVR-µC, too :)

    Sorry guys if the advertisement for another language wasn't appropriate, but I thought it would give some new inspiration to broaden the plattforms by decreasing the codebase - at least for the IDE.

     
  • stan cartwright

    stan cartwright - 2020-01-14

    Boris- it's about gcb...I think.
    QB64 as an interface between pic/avr to win display but I could have misinterpretted it all.
    This would be real world useful. I run out of ideas for pics. Most things are cheaper to buy than make.

     
  • stan cartwright

    stan cartwright - 2020-01-14

    Boris- it's about gcb...I think.
    QB64 as an interface between pic/avr to win display but I could have misinterpretted it all.
    This would be real world useful. I run out of ideas for pics. Most things are cheaper to buy than make.
    Looks complicated https://en.wikipedia.org/wiki/QB64

     
  • stan cartwright

    stan cartwright - 2020-01-14

    This subject has been a bit hard to follow.
    Is it uses a pic program that connects to a QB64 program via serial and the QB64 can display/manipulate the pic data sent and display it in a pc window?
    You guys get so technical sometimes.

     
  • stan cartwright

    stan cartwright - 2020-01-14

    It does look retro

     
  • stan cartwright

    stan cartwright - 2020-01-14

    Isn't FreeBASIC editor 1.0.6.8 totally different to freebasic IDE 0.4.6
    I think the old one is simpler.

     
  • Chris Roper

    Chris Roper - 2020-01-14

    I think that my article and it’s intent was not as clear as I thought it was.

    The intent was to show a Language (Quick BASIC) that would be easy for GCBASIC users to get to grips with, that did not require a complex IDE or a steep learning curve and was also portable.

    The use of InForm to create a windows app talking to the Xpress Board was just my attempt to show that it would be easy to interface any PIC or AVR device to a window display using GCBASIC, QB64 and Inform.

    It was not intend to start discussion of portable IDE's, Graphical development tools or even FreeBASIC.

    I was just highlighting a tool that I thought would be of interest to GCBASIC users who had a need to create a PC interface without having to learn how to use complex tools or additional languages with a tip of my hat to Fellippe Heitor for his time and effort with InForm.

    I hope that clears things up.

    Cheers
    Chris

     
  • stan cartwright

    stan cartwright - 2020-01-15

    Not really. ....but it was informative and interesting.
    I thought you got used to gcb because you had used basic as primary programming language and we were talking about programming pic/avr.
    I don't see how QB64 would make programming pics easier....as any other editor.
    I can see the point of interfaceing a pic to a win pc....didn't they call that terminal...but that was text only. Real world stuff.
    Thanks for all the fish

     
  • sfyris

    sfyris - 2022-02-07

    Interesting. Thanks a lot

     

Log in to post a comment.