Structure/Event/Interrupt question

Discuss the Firewing language

Structure/Event/Interrupt question

Postby jmusselman64 » Sun Mar 18, 2018 7:26 pm

Another question -
As I'm working on another project, I'm running in to problems trying to pass a structure to an event handler.
Eventually, I'll have 3 UARTS receiving packets and decoding them, so i'm trying to make a common routine I can call for all three.

I've passed structures before. It doesn't seem to like the interrupt routine I guess.

Here's a test program and module. When I compile, I get 'Invalid variable type : header_t' errors.

Main Program:
Code: Select all
'P24 Event Test

imports P24EventMod
 
public structure header_t
   Command as byte
   Length as ushort
   RcvdChksum as ushort
   CalcChksum as ushort
   valid as boolean
end structure

Public dim Testheader as header_t

'****************************************************************************                 
'    This event handler is triggered when data is received
'****************************************************************************
 public Sub OnData1(byref hdr as header_t, ByRef Data As Byte) Handles P24EvMod.OnData1
   Dim DataIn As Byte = Data
   
   With hdr
      .Length = &h1234
      .command = Data
   end with
 
End Sub

/* Main program...*/
Sub Main()
 
   While true
   End While
   
End Sub


and the test module:
Code: Select all
'Event module for Event Test
Module P24EvMod

'Declaration
Public Event OnData1(byref hdr as header_t, ByRef data As Byte)

'Interrupt code
private Interrupt OnRX1(Pic.U1RXInterrupt)
   
   ' read a data byte from the USART...
   Dim byteRead As Byte = U1RXREG.Byte0
   
       RaiseEvent OnData1(TestHeader,byteRead)      ' call the event handler
     
   IFS0.11 = 0
End Interrupt

End Module
jmusselman64
 
Posts: 24
Joined: Thu Jan 22, 2015 1:01 am

Re: Structure/Event/Interrupt question

Postby Coccoliso » Mon Mar 19, 2018 8:57 am

Hi,

the structure is shared between the main and the event manager so it's better to put it in a module

Code: Select all
module Structs

public structure header_t
   Command as byte
   Length as ushort
   RcvdChksum as ushort
   CalcChksum as ushort
   valid as boolean
end structure
end module


Then use it in the main program

Code: Select all
imports P24EvMod
imports Structs         // <-------------------- Want to use Structs.header_t so import it

Public dim Testheader as header_t   
// If want shares Testheader in all module you can put it in a module and import it in main and other module

'****************************************************************************                 
'    This event handler is triggered when data is received
'****************************************************************************
 public Sub OnData1(byref hdr as header_t, ByRef Data As Byte) Handles P24EvMod.OnData1
   Dim DataIn As Byte = Data
   Testheader = hdr      // <------------ main TestHeader is populated with hdr values
   With hdr
      .Length = &h1234
      .command = Data
   end with
 
End Sub

/* Main program...*/
Sub Main()
 
   While true
   End While
   
End Sub


and in event manager ..

Code: Select all
'Event module for Event Test
Module P24EvMod

imports Structs          // <-------------------- Want to use Structs.header_t so import it

'Declaration
Public Event OnData1(byref hdr as header_t, ByRef data As Byte)

'Interrupt code
private Interrupt OnRX1(Pic.U1RXInterrupt)
   
   ' read a data byte from the USART...
   Dim byteRead As Byte = U1RXREG.Byte0
   dim TestHeader as header_t   
   ' if define TestHeader here can return values in the event to main program
   ' now can fill TestHeader here with your values
   RaiseEvent OnData1(TestHeader,byteRead)      ' call the event handler
     
   IFS0.11 = 0
End Interrupt
End Module


With complex programs you should always put the name of the module ( is a namespace in VB.NET ) before the structure to avoid confusion on the types :
Code: Select all
dim TestHeader as Structs.header_t


I do not know if I answered your question but if you want to use a type in more modules you have to separate them from main
and this also applies to any values you want to make global in many modules
User avatar
Coccoliso
 
Posts: 177
Joined: Sat Sep 27, 2014 10:02 am

Re: Structure/Event/Interrupt question

Postby jmusselman64 » Mon Mar 19, 2018 3:13 pm

Thanks again, Coccoliso! I applied your changes and the test program compiled successfully. I think I get it now.

'...if you want to use a type in more modules you have to separate them from main
and this also applies to any values you want to make global in many modules.'

This isn't mentioned anywhere in Firewing that I could find; it must be a VB.net thing. I'm slowly getting up to speed on the 'nuances' of Firewing,

When this project is moving along better, I'll post a bunch of modules/code for the Microchip PIC24128GA204 'Curiosity' board I'm using.
It's been a fun experience.

Thanks again,
Jerry
jmusselman64
 
Posts: 24
Joined: Thu Jan 22, 2015 1:01 am

Re: Structure/Event/Interrupt question

Postby Coccoliso » Mon Mar 19, 2018 3:41 pm

Hi,
It's in a corner of the documentation here http://www.firewing.info/pmwiki.php?n=Reference.Scope
and in a post here http://www.firewing.info/forum/viewtopic.php?f=7&t=205&hilit=variable+scope#p1153
.. then I come from VB6, moved to VB.NET, landed in embedded with Swordfish and expanded memory and program space with FW32.
User avatar
Coccoliso
 
Posts: 177
Joined: Sat Sep 27, 2014 10:02 am


Return to Language

Who is online

Users browsing this forum: No registered users and 2 guests

cron

x