|
-
Oct 26th, 2002, 08:26 PM
#1
Thread Starter
Lively Member
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
-
Oct 26th, 2002, 09:09 PM
#2
Frenzied Member
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.
-
Oct 27th, 2002, 08:16 AM
#3
Thread Starter
Lively Member
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?
-
Oct 27th, 2002, 11:58 AM
#4
Hyperactive Member
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: 
-
Oct 27th, 2002, 04:14 PM
#5
Thread Starter
Lively Member
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
-
Oct 27th, 2002, 08:01 PM
#6
Hyperactive Member
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: 
-
Oct 27th, 2002, 08:08 PM
#7
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|