No announcement yet.

Pad leading zeros to string in basic module

  • Filter
  • Time
  • Show
Clear All
new posts

  • Pad leading zeros to string in basic module

    DL06, F0-CP128 Basic Module, H0-ECOM100 Ethernet Module,
    Allflex RFID Reader using RS-232

    I have the above setup that is tied to a PC thru ethernet that sends records from a database into PLC Vmemory.
    I read the RFID field in the information and compare it to the RFID signal coming in thru the basic module.
    When I find a match, I need to select that record's information and send it to another Vmemory area.

    In order for the RFID signal to match the information from the PC, I need to pad the string with zeros to get a length of 32 characters. I cannot find a command to do this, but I thought this would work:

    10 INPUT3 ,$(0) : REM Input Data to String 0 ($(0))
    20 IF INPLEN=0 THEN PRINT1 "Device Timed Out" : GOTO 10
    30 PRINT1 "Received> ",$(0)
    40 PRINT1 "STRING LENGTH>",LEN($(0))
    50 REVERSE$($(0),32) REM Reverse the string to add zeros to get a 32 character string
    60 IF LEN($(0) < 32 GOTO 70 ELSE GOTO 100
    70 $(0)=$(0) + "0"
    80 PRINT1 "STRING LENGTH>",LEN($(0))
    90 GOTO 60
    100 REVERSE$($(0),32) Reverse the string back to normal
    110 PRINT1 "New string value> ",$(0)

    I reverse the string, add "0" until length is 32 characters, then reverse again and load into DPORT memory and BMOVE write.
    Is there a command to pad leading zeros?
    Would this logic work?

  • #2
    Another possible way is to start with a string of 32 zeroes. Add the received string. Then use the RIGHT$ command to pick off the final 32 characters.



    • #3
      Thanks Bernie.
      Something like this?

      10 INPUT3 ,$(0) : REM Input Data to String 0 ($(0))
      20 IF INPLEN=0 THEN PRINT1 "Device Timed Out" : GOTO 10
      30 PRINT1 "Received> ",$(0)
      40 REM ---
      50 $(1)="00000000000000000000000000000000" + $(0) REM 32 "0" characters
      60 $(1)=RIGHT$($(1),LEN($(0))) REM nth thru last character
      70 PRINT1 "New string value> ",$(1)

      The RIGHT$ statement counts from left to right in X amount of characters, right? Thats why in 60 I need to start inward the length of string 0 to start on the correct character? (INPUT3 varies in length)
      Last edited by commander; 08-22-2013, 09:10 AM.


      • #4
        The description of the third argument ('n') from the manual

        n is an expression and specifies the number of characters of string expression to be assigned to string variable. n must be in the range 0 to 254. RIGHT$ returns a string consisting of the nth through last character of its string expression.
        Has two different definitions. The first sentence leads me to believe that you would only need to specify '32'. But the last sentence seems to point to using your method. I guess just try both and see what works.

        Compare to the definition of 'n' in the LEFT$ statement where it is obviously the first 'n' characters.
        Last edited by bcarlton; 08-22-2013, 09:14 AM.



        • #5
          Yeah, it would be easier if it counted from the right so I could specify the right 32 characters.
          I'll try both. Unfortunately another department has borrowed the only converter from 120vac to 220vac to run this machine. (UK order) But I still have 2 weeks before it ships.

          Thanks again for the very quick reply.

          Now onto the compare code.....


          • #6
            Their example code

            Example >PRINT1 RIGHT$("FACTS EXTENDED BASIC",5)
            seems to support the first interpretation.



            • #7
              I could not get RIGHT$ to assign to a string variable. Tried MID$ too, same results. Seems to only work with PRINT. Went back to a loop to add character 0 and it works.

              Working code!

              AUTOSTART 1,0,9600
              20 REM August 2013
              30 REM Communication Program for F0-CP128
              40 REM File Name "xxx.abm"
              50 REM Read a string from a serial device (RFID) and write the ASCII codes
              60 REM for that string into a contiguous block
              70 REM of V-memory in a DL06 CPU starting at V15000
              80 REM ASCII INPUT must be 32 characters before writing to Vmemory
              90 STRING 2551,254 : REM Allocate String Space 10 strings *254 characters
              100 SETPORT 1,9600,N,8,1,N : REM Programming/Monitoring
              110 SETPORT 3,9600,N,8,1,N : REM Serial Device RFID Allflex Panel Reader
              120 SETINPUT 1,1,10,32,1000,20 : REM Define how INPUT will work
              130 REM --- Input 32 characters max or stop @ LF (ascii 10)
              140 S06_C(310)=1 : REM CoPro Turns on C310 Waiting for input in PLC
              150 INPUT3 ,$(0) : REM Input Data to String 0 ($(0))
              160 IF INPLEN=0 THEN PRINT1 "Device Timed Out" : GOTO 130 REM Loop
              170 PRINT1 "Received> ",$(0)
              180 PRINT1 "STRING LENGTH>",LEN($(0))
              190 REM --- New code to insert leading zeros
              200 $(0)="0"+$(0)
              210 IF LEN($(0))<32 GOTO 200
              220 REM
              230 REM
              240 PRINT1 "New string value> ",$(0)
              250 PRINT1 "STRING LENGTH>",LEN($(0))
              260 S06_C(311)=1 : REM CoPro Turns on C311 Data Received
              270 REM --- Move the input into memory
              280 FOR X=1 TO LEN($(0))+1 STEP 2 REM Note not using INPLEN
              290 DPORT((X-1),H)=ASC($(0),X)
              300 DPORT(X-1,L)=ASC($(0),X+1)
              310 NEXT X
              320 BMOVE W,VH(15000),K(LEN($(0)))
              330 REM ---
              340 GOTO 130 : REM Loop to wait for next tag
              Last edited by commander; 08-22-2013, 11:48 AM.


              • #8
                Odd ...

                I could not get RIGHT$ to assign to a string variable
                I'm glad you have it working as you want but, if you have some time, we'll have to look at the code you used which wouldn't assign a value.



                • #9
                  This part worked
                  $(0)="00000000000000000000000000000000"+$(0) REM 32 leading zeros

                  Kept getting a syntax error on the next lines
                  $(0)=RIGHT$($(0),32) REM Use the 32 characters on the right


                  $(0)=MID$($(0),LEN($(0))) REM Basically start on the character of the original string length and read the remaining 32 characters


                  • #10
                    Just a wild guess - try ": REM etc" instead of just "REM etc"
                    Or no REM statement at all just as a test



                    • #11
                      10 PRINT "HELLO WORLD" : REM This is a print statement

                      The ":" must be there as the separator for the command.
                      If you have an urgent issue, please contact AutomationDirect's Technical Support team.

             Technical Support: 1(800) 633-0405 or (770) 844-4200 Email Tech Support