Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Variable in PortSet?


raichlebw Feb 5, 2023 10:07 PM

Hello,

Althought I have done this for years, it now seems that PortSet will not accept a variable parameter. I'm compiling for a CR1000.

This compiles (just a fragment):

Public i As Long

Public load(1,8) As Boolean

For i = 1 To 8
   PortSet(1,load(1,i))
Next i

But this does not:

Public i As Long

Public load(1,8) As Boolean

For i = 1 To 8
    PortSet(i,load(1,i))
Next i

This is an exerpt from a program that I've used for many years. I probably have updated the complier since last use (Covid and all). I'm running "Version: CR1000.Std.32.05 CompileDate: 01/06/2020", which seems to be the latest.

Is this a known bug? Know solutions?

Thanks,

Brian


kirving Feb 8, 2023 05:09 PM

I think this has been the case for a long time for the CR1000 at least.  Here's an old function from a 2012 program:

' relay-control-sub:setPortState()

sub setRelayState(relay,state as boolean)

    ' portSet() does not allow variable for port, so brute force it:

    if relay = 9 then

        portSet(9,state)

    else if relay = 8 then

        portSet(8,state)

    else if relay = 7 then

        portSet(7,state)

    else if relay = 6 then

        portSet(6,state)

    else if relay = 5 then

        portSet(5,state)

    else if relay = 4 then

        portSet(4,state)

    else if relay = 3 then

        portSet(3,state)

    else if relay = 2 then

        portSet(2,state)

    else if relay = 1 then

        portSet(1,state)

    endif 

endSub

 

I'd probably use a select-case construct now to do that, to be a bit leaner.

Ken


kirving Feb 8, 2023 05:17 PM

Here's another way to format that subroutine to save a few lines:

' relay-control-sub:setPortState()

sub setRelayState(relay,state as boolean)

    ' portSet() does not allow variable for port, so brute force it:

    if relay = 9 then : portSet(9,state)

    else if relay = 8 then : portSet(8,state)

    else if relay = 7 then : portSet(7,state)

    else if relay = 6 then : portSet(6,state)

    else if relay = 5 then : portSet(5,state)

    else if relay = 4 then : portSet(4,state)

    else if relay = 3 then : portSet(3,state)

    else if relay = 2 then : portSet(2,state)

    else if relay = 1 then : portSet(1,state)

    endif

endSub

Or using select case...

sub setRelayState(relay,state as boolean)

    ' portSet() does not allow variable for port, so brute force it:

    select case relay

    case = 9: portSet(9,state)

    case = 8: portSet(8,state)

    case = 7: portSet(7,state)

    case = 6: portSet(6,state)

    case = 5: portSet(5,state)

    case = 4: portSet(4,state)

    case = 3: portSet(3,state)

    case = 2: portSet(2,state)

    case = 1: portSet(1,state)

    endselect

endSub


pokeeffe Feb 8, 2023 05:27 PM

If you want to keep the for..loop, replace PortSet with WriteIO, then generate the appropriate mask string at the start of each loop using bit logic and the index value.


raichlebw Feb 8, 2023 11:56 PM

Thanks all. In the interim I was turned onto the Select Case option, which worked nicely. I am curious about the WriteIO option, so will check that out.

I hate to think I was using that old of a compiler...

Brian


Hickis1974 Feb 26, 2025 03:49 AM

One workaround is to use a conditional structure instead of looping directly with variables: 

For i = 1 To 8
    Select Case i
        Case 1
            PortSet(1, load(1,i))
        Case 2
            PortSet(2, load(1,i))
        Case 3
            PortSet(3, load(1,i))
        ' ... tiếp tục với các cổng còn lại
    End Select
Next i

 While not very pretty, it ensures the code runs correctly. If there are more ports, this might be a bit cumbersome, but at least it works.

Log in or register to post/reply in the forum.