Results 1 to 7 of 7

Thread: How to convert MSComm String to Integer

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    85

    How to convert MSComm String to Integer

    I am a VB6 Newbie and I'd like some help in coverting a stream of serial data from MSComm (Com1) to integer. The serial data stream comes in at a slow rate from a industrial PLC. Multiple 16-bit values are transmitted from the PLC to my PC as a constant stream of bytes as the machine runs.

    The serial data goes into "strBuffer"
    Dim strBuffer As String

    Since the industrial PLC is sending 16-bit data values, I believe I need to extract multiple strings of length=2, convert them into integers and then do my math. (I can always synch the data on 16b boundaries so that's not an issue).

    As a test I have tried the following and it does not work.
    Dim ZZZ as integer
    Dim ZS as string

    ZS = (Mid(strBuffer, 1, 2))
    ZZZ = CInt(ZS) *** Always generates a Error 13 Type Mismatch.

    I'm so lost on how to resolve this. I've gone round and round for hours. Please pass on your suggestions. Thanks Robo

  2. #2
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033

    from MSDN

    Val Function


    Returns the numbers contained in a string as a numeric value of appropriate type.

    Syntax

    Val(string)

    The required stringargument is any validstring expression.

    Remarks

    The Val function stops reading the string at the first character it can't recognize as part of a number. Symbols and characters that are often considered parts of numeric values, such as dollar signs and commas, are not recognized. However, the function recognizes the radix prefixes &O (for octal) and &H (for hexadecimal). Blanks, tabs, and linefeed characters are stripped from the argument.

    The following returns the value 1615198:

    Val(" 1615 198th Street N.E.")

    In the code below, Val returns the decimal value -1 for the hexadecimal value shown:

    Val("&HFFFF")

    Note The Val function recognizes only the period (.) as a valid decimal separator. When different decimal separators are used, as in international applications, use CDbl instead to convert a string to a number.
    Help keep this forum clean: Remember to mark your thread as resolved · Search before you post · Remember to rate posts that help

    VS2010: Visual Studio 2010 Keybinding Posters
    · Service Pack 1
    Tools: GhostDoc - automatically generates XML documentation comments
    · NuGet package Manager · PowerCommands IDE extensions
    Source Control: ankhsvn - integration for SVN
    · Windows Shell Extension for Subversion

    Development Laptop: Intel Core i5 430M 2.26 GHz @ 2.53 GHz
    · 4096 MB, DDR3 PC3-8500F (533 MHz), Kingston · ATI Mobility Radeon HD 5470 · 15.6 @ 16:9, 1366x768 pixel, HD LED LCD

    I follow:
    JoelOnSoftware - A weblog by Joel Spolsky, a programmer working in New York City, about software and software companies
    ScottGu's Blog - Scott Guthrie works for Microsoft as the Product Manager of the .NET Framework
    Portugal-a-Programar - Portuguese Developers Community
    .NET Rocks! - is a weekly Internet audio talk show for .NET Developers.

    Programming Languages:
    C#
    · VB.NET · JAVA · PHP · Javascript
    Other:
    XML
    · HTML · CSS · JQuery · SQL



    *** Proudly Portuguese ***

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    85
    Thanks for the suggestion but VAL will not work. The byte data being streamed via the serial port ranges from 0-255 in value.

    Any other suggestions?

  4. #4
    Hyperactive Member D12Bit's Avatar
    Join Date
    Oct 2000
    Location
    Guatemala
    Posts
    373
    Can you post a piece of the actual stream?
    working with the real data can be better that to assume what type of characters you are working with...
    "Who Dares Wins" - "Quien se Arriesga Gana"
    Mail me at:

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    85
    Here is copy of a single stream of data from the serial port. I converted it to hex so that I could see its values.

    74 01 38 02 08 03 18 04 68 05 78 06 48 07 58 08 A8 09 B8 0A 88 0B 98 0C E8 0D F8 0E C8 0F D8 10 38 11 28 12 18 13 08 14 78 15 68 16 58 17 48 18 B8 19 A8 1A 98 1B 88 1C F8 1D E8 1E D8 1F C8 20 08 21 18 22 28 23 38 24 48 25 58 26 68 27 78 28 88 29 98 2A A8 2B B8 2C C8 2D D8 2E E8 2F F8 30 18 31 08 32 38 33 28 34 58 35 48 36 78 37 68 38 98 39 88 3A

  6. #6
    Hyperactive Member D12Bit's Avatar
    Join Date
    Oct 2000
    Location
    Guatemala
    Posts
    373
    I was checking the other thread too...and seems like you still need to add couples of hex values to get your individual results needed to be stored...right?

    like
    HEX---->DEC
    0138----312
    0208----520

    and so on...

    but you have 115 HEX values and 66 Fields to store data...anyway...
    how I see it...you just nedd to use a HEX->DEC routine to convert the HEX data to a string that can be converted an Integer varType...

    I think that I made one at the office a few months ago...
    but the formula that I used to do the conversion was something like:

    Value= y
    Position of the value=X (from 0 to n, starting from the right, if zero use table)

    y*16^x

    HEX:0318
    0 => 0*16^3 = 0
    3 => 3*16^2 = 768
    1 => 1*16^1 = 16
    8 => 8*16^0 = 8

    0 + 768 + 16 + 8 = 792

    Is this of any help?
    btw, I need to translate first the A thru F to their DEC value with a table
    "Who Dares Wins" - "Quien se Arriesga Gana"
    Mail me at:

  7. #7
    Hyperactive Member D12Bit's Avatar
    Join Date
    Oct 2000
    Location
    Guatemala
    Posts
    373
    hmmm...
    I was checking on zuperman's suggestion...
    and found this:
    http://support.microsoft.com/default...EN-US;Q161304&

    the correct use of the Val() when converting an HEX value to a DEC value...

    "Who Dares Wins" - "Quien se Arriesga Gana"
    Mail me at:

Posting Permissions

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



Click Here to Expand Forum to Full Width