declaring variables

Questions regarding syntax

declaring variables

Postby semper_linux » Sat Dec 04, 2010 6:16 pm

Hi Forum,

I couldn't find the 'absolute idiot' section to post this so forgive me for posting what I'm sure is a dumb question (or two) - the general problem is that my background is (68000) assembler and I'm struggling to get my head round OOP / Classes / Destructors / Inheritance etc. Anyway, here goes...

I want to write a fairly simple app that reads data from the Com port and outputs the processed data back to the COM port and occasionally writing info to the screen.

My immediate problem is that I get a Syntax error (:no syntax matching in line 2 col 1) when compiling. I'm 99% sure this is something to do with how I am declaring the Public variables. As far as I understand it, I must declare these variables as Public because they are used in different places in the code.
I'm not even sure if I'm building the app correctly - I'm just looking at examples and trying this and that until I get the correct results.

The following is the 'bare bones' of the code I have written so far which demonstrates the problem.....

************************************** Beginning of code *******************************

'The variable declarations
Public s as string '(s) is the rx'd data store
Public n as integer '(n) is the count of bytes rx'd
Dim zeil(99) as string 'This is a simple data array used later on
Dim I as integer 'A counter used in for-next loops

'This is the start of the main procedure
WaitIp:

'Here we make sure the array zeil is empty - also used as a wait loop
for I=1 to 99
zeil(I)=""
next I
'now we test to see if data has been rx'd at the COM port
if n > 0 goto NextStep 'if n > 0, data has been rx'd so continue processing
end if
goto WaitIp 'otherwise go back and re-run the loop

NextStep:
Editor1.Append(s)
' lots more processing code here
GOTO WaitIp 'End of the main process - go back and wait for new data


'This Sub sets up the COM port at application start up
Private Sub Form_OnOpen()
SerialPort1.Open()
SerialPort1.BaudRate = "Baud2400"
SerialPort1.Flush()
End Sub


'This Sub is the handler for data rx'd at the COM port
Sub SerialPort1_OnEvent()
SerialPort1.ReadLine(s) 'The data rx'd is stored in string variable (s)
SerialPort1.BytesRead(n) 'The byte count is now stored in integer variable (n)
End Sub


'This Sub closes the port and shuts down the app
Private Sub CommandButton1_OnEvent()
Close
Stop
End Sub

********************************** End of code ************************************


If anyone can point me in the right direction, I'd be very grateful (please don't say "give up" as I'd love to complete this !)

Am I correct in putting the main code body at the beginning ? - I assume this is where execution starts.
Should this 'main' code be in a sub such as a Public|Private Sub Main() / End Main ?

As you can see, I'm pretty lost !!

Many thanks for any assistance.

Semper_linux.
semper_linux
 
Posts: 2
Joined: Sat Dec 04, 2010 5:43 pm

Re: declaring variables

Postby Henning » Sat Dec 04, 2010 9:46 pm

First of all, you can't execute code outside a Sub or Function.

This way you code will compile and run. If as expected I don't know.:
Code: Select all
''************************************** Beginning of code *******************************
'The variable declarations
Public s as string '(s) is the rx'd data store
Public n as integer '(n) is the count of bytes rx'd
Dim zeil(99) as string 'This is a simple data array used later on
Dim I as integer 'A counter used in for-next loops

'This Sub sets up the COM port at application start up
Private Sub Form_OnOpen()
  SerialPort1.Open
  SerialPort1.BaudRate = "Baud2400"
  SerialPort1.Flush
  RunIt
End Sub

Sub RunIt()
WaitIp:

  'Here we make sure the array zeil is empty - also used as a wait loop
  for I=1 to 99
  zeil(I)=""
  next I
  DoEvents  'needed or the app will lock-up, not respond to anything
  'now we test to see if data has been rx'd at the COM port
  if n > 0 goto NextStep 'if n > 0, data has been rx'd so continue processing
  end if
  GOTO WaitIp 'otherwise go back and re-run the loop
 
NextStep:
  Frm_Main.Editor1.Append(s)
' lots more processing code here
  GOTO WaitIp 'End of the main process - go back and wait for new data

End Sub

'This Sub is the handler for data rx'd at the COM port
Sub SerialPort1_OnEvent()
  SerialPort1.ReadLine(s) 'The data rx'd is stored in string variable (s)
  n = SerialPort1.BytesRead 'The byte count is now stored in integer variable (n)
End Sub


'This Sub closes the port and shuts down the app
Private Sub CommandButton1_OnEvent()
  SerialPort1.Close
  Me.Close
'  Stop
End Sub

'********************************** End of code ************************************


And this is how I would do it without GOTO's:
Code: Select all
'************************************** Beginning of code *******************************
'The variable declarations
Public s as string '(s) is the rx'd data store
Public n as integer '(n) is the count of bytes rx'd
Dim zeil(99) as string 'This is a simple data array used later on
Dim StopRunning As Boolean

'This Sub sets up the COM port at application start up
Private Sub Form_OnOpen()
  SerialPort1.Open
  SerialPort1.BaudRate = "Baud2400"
  SerialPort1.Flush
  RunIt
End Sub

Sub RunIt()
  Dim I as integer 'A counter used in for-next loops
 
  While Not StopRunning

    'Here we make sure the array zeil is empty - also used as a wait loop
    for I=1 to 99
    zeil(I)=""
    next I
    DoEvents  'needed or the app will lock-up, not respond to anything
    'now we test to see if data has been rx'd at the COM port
    if n > 0 Then
      'NextStep 'if n > 0, data has been rx'd so continue processing
      Frm_Main.Editor1.Append(s)
      n = 0
    end if
  Wend
  SerialPort1.Close
  Me.Close
 
End Sub

'This Sub is the handler for data rx'd at the COM port
Sub SerialPort1_OnEvent()
  SerialPort1.ReadLine(s) 'The data rx'd is stored in string variable (s)
  n = SerialPort1.BytesRead 'The byte count is now stored in integer variable (n)
End Sub


'This Sub closes the port and shuts down the app
Private Sub CommandButton1_OnEvent()
  StopRunning = True
'  SerialPort1.Close
'  Me.Close
'  Stop
End Sub

'********************************** End of code ************************************


/Henning
Henning
 
Posts: 262
Joined: Mon Feb 09, 2009 12:03 am
Location: Sweden

Re: declaring variables

Postby semper_linux » Sat Dec 04, 2010 9:59 pm

Thank you for the quick reply Henning - cleared up quite a few things for me - many thanks.

Still get that Syntax error however when compiling / de-bugging !
semper_linux
 
Posts: 2
Joined: Sat Dec 04, 2010 5:43 pm


Return to Coding Questions

cron