How to modify variable that is part of while loop

Hey all,
I am trying to put together a Serial tester and I want to send the ASCII letter ‘U’ out the serial port continuously when I click one radio button, and when I click another radio button I want it to stop.

I created a shared property called alwaysSend and set it up as TYPE integer, and SCOPE public(I really do not understand the whole scope thing, but since I want to use the ‘alwaysSend’ in two actions I figured Public was the way to go).

In my one radio button I created an Action that does the following:
alwaysSend = 1

While alwaysSend = 1
SerialConnection1.Write(“U”)

Wend

This works when I click teh radio button the action is attached to.

In my Stop send button I have an action:
alwaysSend = 0

Which I thought would clear the alwaysSend variable, but instead it causes the application to hang and crash.

Where did I go wrong?

Well here I sit beating my head against a wall…

I tried creating a method as it seems to be a good idea to treat the constant transmission as function.

Here is what I have in the method:

Method Name: SendAlways
No parameters
No return type
Scope: Public

And here is what I have for code:

dim AlwaysSend as integer

If AlwaysSend = 1 then
SerialConnection1.Write(“U”)
End If

In my two actions elsewhere I have:
To enable the method:

SendAlways.AlwaysSend = 1

And to Disable teh method I have:

SendAlways.AlwaysSend(0)

When I hit the RUN button I get the following errors:

SPI_Test.SendUcont.Action, line 3
This method doesn’t return a value
SendAlways.AlwaysSend = 1

SPI_Test.SendUcont.Action, line 3
Type “Int32” has no member named “AlwaysSend”
SendAlways.AlwaysSend = 1

SPI_Test.StopSendUcont.Action, line 1
This method doesn’t return a value
SendAlways.AlwaysSend(0)

SPI_Test.StopSendUcont.Action, line 1
Type “Int32” has no member named “AlwaysSend”
SendAlways.AlwaysSend(0)

Moving the AlwaysSend variable to teh Parameters Window makes no difference

This cannot be this hard. What am I doing wrong?

Jim

This will get clearer as you experiment.

You’ve created a variable, AlwaysSend, within your method. That variable only exists within that method, and cannot be accessed directly outside of that method. As such, SendAlways.AlwaysSend is meaningless.

Instead, create a parameter to your method, alwaysSend As Integer, so you can call your method as SendAlways( 1 ) or SendAlways( 2 ).

Having said that, your code is going to lock up your app. The app runs in a single thread, so code in a method runs until it finishes. While it runs, nothing else does, so the UI will be unresponsive. Normally, that’s fine, but you’ve created an endless loop so the method will never end.

You’re getting into higher-level stuff for a beginner, but you will want is either a Thread or a Timer. The former lets you run code concurrently with the main thread, while the latter runs code during idle time. Look up each in the documentation.

This loop will run forever as there’s nothing to stop it.

Thanks. I predominantly program Microcontrollers hence why I went with the Global variable scheme thinking it would work here…NOPE! Got that wrong LOL. I do not see this as very high level. Simple USART transmit. In a micor this would have been done in 20 minutes and off to the races. But here its a learning curve as everything in life.

Thanks
Jim

To clarify, it’s now what you’re trying to do (send the signal) that’s high-level, it’s how you’re trying to do it (in a loop while keeping the UI responsive).

Yes! Agree!

Ok, I have a solution that I can live with.
I took Kems advice and went with a Timer, and then I n my button actions I either enable teh timer, or Disable teh timer as need be.

This works just fine and does what I want

Case Closed! Thank You all

Jim

1 Like