Author Topic: Serial Comm's in InForm Project  (Read 5812 times)

0 Members and 1 Guest are viewing this topic.

Offline caroper

  • Newbie
  • Posts: 6
    • View Profile
Serial Comm's in InForm Project
« on: January 10, 2020, 04:24:10 pm »
I am new to QB64 and InForm but I cut my teeth in programing with BASIC back in the 80's so QB64 was like a breath of fresh air when I found it last week, especially as I use GCBASIC to program Microcontrollers. The Cherry on top was InForm with which I saw a chance to try my hand at Windows programming.

My test project is to try and create a Windows interface to a Microchip PIC Development board.
The Hardware is WIN7 Pro 64 Bit and a  Microchip Xpress 16f18855 development board - talking via USB as COM6:

I have the code working one way with Windows able to control the 4 LED's via Check Boxes RA0..RA3.

 
InForm01.png


But RA5 should reflect the state of the button on the board and is not responding.

The Microcontroller code is working as an attached terminal correctly received the string that InForm should receive.

The Relevant code snippets are:

Code: QB64: [Select]
  1. DIM SHARED XpressEvaluationBoard AS LONG
  2. DIM SHARED PxStr AS STRING * 1
  3. ': External modules: ---------------------------------------------------------------
  4. '$INCLUDE:'InForm\InForm.ui'
  5. '$INCLUDE:'InForm\xp.uitheme'
  6. '$INCLUDE:'QB64_Xpress_Com6.frm'
  7.  

Code: QB64: [Select]
  1. SUB __UI_OnLoad
  2.     OPEN "COM6:19200,N,8,1,BIN,CS0,DS0" FOR RANDOM AS #1
  3.  

This is the part that is not working - any suggestions would be welcome.
Code: QB64: [Select]
  1. SUB __UI_BeforeUpdateDisplay
  2.     'This event occurs at approximately 30 frames per second.
  3.     'You can change the update frequency by calling SetFrameRate DesiredRate%
  4.     IF LOC(1) THEN
  5.         GET #1, , PxStr
  6.         IF PxStr = "5" THEN
  7.             x = RA5CB.value
  8.             IF x <> 0 THEN
  9.                 x = 0
  10.             ELSE
  11.                 x = -1
  12.             END IF            
  13.             RA5CB.value = x
  14.         END IF
  15.     END IF
  16.  

and back to apparently working code:
Code: QB64: [Select]
  1. SUB __UI_BeforeUnload
  2.     'If you set __UI_UnloadSignal = False here you can
  3.     'cancel the user's request to close.
  4.     CLOSE #1

Code: QB64: [Select]
  1. SUB __UI_Click (id AS LONG)
  2.      PxStr = ""
  3.      SELECT CASE id
  4.         CASE RA0CB
  5.             PxStr = "1"
  6.  
  7.         CASE RA1CB
  8.             PxStr = "2"
  9.  
  10.         CASE RA2CB
  11.             PxStr = "3"
  12.  
  13.         CASE RA3CB
  14.             PxStr = "4"
  15.     END SELECT
  16.     PUT #1, , PxStr
  17.  

Everything else is just the Boiler plate code generated by InForm.

Any hints would be appreciated as being a novice at windows programming I have probably made a simple mistake that I am too inexperienced to see.

Thanks for QB64 and InForm, combined with Great Cow BASIC - the language I use to Program Microcontrollers - I can envisage a world of cooperative PC/Controller apps.

Cheers
Chris
« Last Edit: January 10, 2020, 05:12:52 pm by caroper »

Offline caroper

  • Newbie
  • Posts: 6
    • View Profile
Re: Serial Comm's in InForm Project
« Reply #1 on: January 10, 2020, 04:30:27 pm »
In case it helps here is the Microcontroller Code that I can confirm works by attaching a terminal:

Code: [Select]
#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  ' Overive 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

GhostA = PortA

Do
  InChar = HSerReceive

  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

  If GhostA.5 <> PortA.5 then
    GhostA = PortA
    InChar = "5"
    HSerSend InChar
  end if

Loop

Cheers
Chris

edit: Thanks odin but as GCBASIC and QB64 use the same keywords I purposely formated my code as to use the highlighting.
I will however leave it as edited by a Moderator.
« Last Edit: January 10, 2020, 05:46:31 pm by caroper »

FellippeHeitor

  • Guest
Re: Serial Comm's in InForm Project
« Reply #2 on: January 10, 2020, 05:13:17 pm »
Hello, Chris. Welcome to our forum. I'm glad to hear QB64 + InForm are going to be useful to you.

Here's a simple fix for you to be able to display the button's state in the checkbox: the thing is that we need an array called Control() to manipulate controls in a form, which means we cannot do RA5CB.value directly. Replace __UI_BeforeUpdateDisplay in your code with this and let me know if that does the job:

Code: QB64: [Select]
  1. SUB __UI_BeforeUpdateDisplay
  2.     'This event occurs at approximately 30 frames per second.
  3.     'You can change the update frequency by calling SetFrameRate DesiredRate%
  4.     IF LOC(1) THEN
  5.         GET #1, , PxStr
  6.         IF PxStr = "5" THEN
  7.             Control(RA5CB).value = NOT Control(RA5CB).value
  8.         END IF
  9.     END IF
  10.  
« Last Edit: January 10, 2020, 05:23:08 pm by FellippeHeitor »

Offline caroper

  • Newbie
  • Posts: 6
    • View Profile
Re: Serial Comm's in InForm Project
« Reply #3 on: January 10, 2020, 05:36:37 pm »
Thanks Fellippe,

That works perfectly.

One more question if I may:

I have placed my SHARED variable (DIM SHARED PxStr AS STRING * 1 )  along with the system generated Variables, but it is lost on form edits.
Is there a better place to set up user variables that is non volatile?

Cheers
Chris
« Last Edit: January 10, 2020, 05:38:51 pm by caroper »

FellippeHeitor

  • Guest
Re: Serial Comm's in InForm Project
« Reply #4 on: January 13, 2020, 03:09:52 pm »
Hey, Chris.

When you edit an existing .frm file, InForm will attempt to preserve anything below the following line in your .bas file:

Code: QB64: [Select]
  1. ': Event procedures: ---------------------------------------------------------------

If it doesn't find that exact line, it will overwrite the existing .bas with an empty sketch again, while keeping a backup copy in the same folder, for safety.

That means that anything you have customised above said line will be removed when you save over.

One alternative is to only update the .frm file, in case your recent edits only involve the design and you didn't add/remove controls. That way, InForm doesn't need to touch the .bas and your SHARED variables remain intact. In the File menu choose "Save project as..." and check the box that says "Don't save .bas project (Save .frm only)":

Captura de Tela 2020-01-13 às 17.09.06.png

Hope that helps.