Announcement

Collapse
No announcement yet.

CByte keyword?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • CByte keyword?

    I am a newbie to PB. My first effort with PB involves sending/receiving
    data through a Com port. The data is sent in hex format. In VB you can
    convert an integer value to a byte value using CByte(integer). How is
    this addressed in PB and where in the book can I find it? I have
    searched everywhere!

    Thanks
    Tim

    ------------------

  • #2
    I'm not quite sure I fully understand the question but here's my
    best guess:

    If you have an ASCII value or HEX string that you need sent over
    the comm port, you need to convert it to a CHR$() for transmission.
    Example...

    Assuming you have your comm port opened as #1

    t$ = "The Quick Brown Fox"
    for x = 1 to len(t$)
    h = asc(mid$(t$,x,1)) '<==> The ASCII value for the letter
    print #1,chr$(h);
    next x

    A more efficient example, however, would be deleting line 3 and
    replace line 4 with:

    print #1,mid$(t$,x,1);

    This example should be relevent for just about any occurance
    you would run across.

    Hope this helps. If it doesn't, how about posting a snippet
    of what you are trying to accomplish and let's go from there.


    ------------------


    [This message has been edited by Mel Bishop (edited April 08, 2002).]
    There are no atheists in a fox hole or the morning of a math test.
    If my flag offends you, I'll help you pack.

    Comment


    • #3

      I believe the CBYT function is what you're looking for. You can
      find the description in the PowerBasic Reference Guide under the
      heading CBCD, CBYT, CBDL, etc... . That's on page 59 of my manual
      (not sure if they're all the same )

      works like this
      bytevar? = CBYT(anynumber)

      you really shouldn't need this though, according to the manual
      PB converts this automatically. like this.

      thisbyte? = yourinteger%

      haven't tried it, should work though


      ------------------

      Comment


      • #4
        This is where I am really confused... I am assembling a string
        byte values that will need to be sent out through the COM port.
        The size is always greater than 5 byes (there are actually seven
        parts that make up the byte string).

        So I guess there are 3 questions:
        1. Is it appropriate to use a byte array
        2. Is it more appropriate to use a var dimensioned as byte but how do you set the length?
        3. When ready to send to the serial port then it have to do
        something like Print #1,chr$(bytevar)

        Thanks everyone!
        Tim

        ------------------

        Comment


        • #5
          It would be easiest to use a dynamic string, thus:
          Code:
          A$ = CHR$(65,66,67,68,69,70,71) ' or use "abcedf", etc
          PRINT #1, A$
          You could use a byte array, but you'd then have to reference each byte individually to construct the 7 byte sequence... hence, using a dynamic string is the easiest way.

          Another approach is to use a combination of both methods, using a UNION... a brief example of the concept follows:
          Code:
          UNION commtype
            bData(1 TO 7) AS BYTE
            sData AS STRING * 7
          END UNION
           
          DIM x AS commtype
          FOR z% = 1 TO 7
            x.bData(z%) = 64 + z%
          NEXT z%
          ...
          PRINT #1, x.sData
          ------------------
          Lance
          PowerBASIC Support
          mailto:[email protected][email protected]</A>
          Lance
          mailto:[email protected]

          Comment

          Working...
          X