Port bit manipulation useing variables


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2004
    Location
    nebraska
    Posts
    79

    Default Port bit manipulation useing variables

    Hello,
    I Am writing a Modbus RTU program to talk with an ILon 100 internet Server. I am trying to streamline my code. I am using modbus single coil write function to write the value of PortB single bit values. The program below works but is there a better way than Case Select. When I say better I mean less code and faster. This is not the whole code but is the jist of the Case Select.

    Code:
    WriteSingleCoil:
    W0 = buffer[3]*255 + buffer[4]      'Combines MFrame 2 byte address into 1 word.
    W0 = w0 - 10                                'Sets up bit address for portB
    If buffer[5] > 0 Then
        Coilstate = 1
    else
        Coilstate = 0
    endif
    Select Case W0
        Case 1
            PORTB.0 = Coilstate
        Case 2
            PORTB.1 = Coilstate
        case 3
            PORTB.2 = Coilstate
        case 4
            PORTB.3 = Coilstate
        case 5
            PORTB.4 = Coilstate
        case 6
            PORTB.5 = Coilstate
        case 7
            PORTB.6 = Coilstate 
        Case 8
            PORTB.7 = Coilstate
        Case else              'test to see if program is getting here
            portb = 255
    End Select
    For B0 = 1 To 8                        ' Send the response to Master
        B1 = buffer[B0]
        Gosub charout
    Next B
    Return
    Is there a way to do something like this.


    PortB.w0 = Coilstate

    I tryed this but it does not work. I'm going to try to use an aliass next but Im not sure if thats a good choice either.
    Thanks
    Nick
    Last edited by ScaleRobotics; - 20th November 2010 at 15:42. Reason: added code tags

  2. #2
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    Is there a way to do something like this.
    PortB.w0 = Coilstate
    Yes

    PortB.0[W0] = Coilstate


    Cheers

    Al.
    All progress began with an idea

  3. #3
    Join Date
    Dec 2004
    Location
    nebraska
    Posts
    79


    Did you find this post helpful? Yes | No

    Default

    Hey
    Thanks for your answer but I don't completly follow.

    PortB.0[W0] = Coilstate

    What is the .0 for.
    I want to do something like this.

    PortB.[W0] = Coilstate

    But It does not compile.
    Where W0 is the pin or bit of port B to change.

    Thanks
    Nick

  4. #4
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    Nick, PortB.0[W0] = Coilstate is a workaround to obtain PortB.[W0] = Coilstate and have the code compiling.

    Cheers

    Al.
    All progress began with an idea

  5. #5
    Join Date
    Dec 2004
    Location
    nebraska
    Posts
    79


    Did you find this post helpful? Yes | No

    Default

    Thanks Al, that worked

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts