Using the S7.Net library to read data wrong result - vb.net

I spent a couple of days to communicate with the PLC, now, I would like to call the read method, strangely, I get an incoherent value returned.
I would like to get the DWord DB172.DW1 value, the first value represents a quantity, the second one is either 0 or 1, my syntax is like this :
Dim result as Object = MyPLC.read(“DB172.DBW1″)
MsgBox(result.ToString & ” ” & result.GetType.ToString)
I obtain as displayed message :
12 System.UInt16
On DB172.DBW2, I obtain this one :
3073 System.UInt16
What am I doing wrong? Thanks

It looks like MyPLC returns System.UInt16, which is two bytes. The hex value of 12 is C and of 3073 is C01. It appears that MyPLC(“DB172.DBW1″) just returns the high order byte into a System.UInt16 value and MyPLC(“DB172.DBW2″) returns both values. You can try:
Dim u1 As System.UInt16 = MyPLC("DB172.DBW1")
Dim b1() As Byte = BitConverter.GetBytes(u1)
Dim u2 As System.UInt16 = MyPLC("DB172.DBW2")
Dim b2() As Byte = BitConverter.GetBytes(u2)
and examine the values in the arrays

Many thanks Jim for your reply, actually, it's a little more complex than I thought.
Actually, to read the DWord number x, the first value is obtained by querying DB172.DBW(2x), the second value is obtained by querying DB172.DBW(2x+1)
So, for example, to read the DWord number 10, the 2 values are obtained like this :
Dim Value1 as byte = MyPLC.read(DB172.DBDW20")
Dim Value2 as byte = MyPLC.read(DB172.DBDW21")

Related

Negative result in HEX to DEC vb.net

Need help in converting hex to dec in VB.NET
I use the code below in smaller number which returns the expected result.
leftPaddingHex = Val("&H" & "99000533")
but when using 99000533 it return negative result -1728051917. I am expecting to get 2566915379. I am using to get the correct result in ms sql. using the code below.
CONVERT(BIGINT,CONVERT(varbinary(4),(SELECT master.dbo.fn_cdc_hexstrtobin(#leftPadding))))
Need help on this. Thanks in advance.
Thank you.
Aze
I just thought that I'd post this alternate method.
The various integer type's Parse and TryParse methods allow you to specify a System.Globalization.NumberStyles parameter. You can specify the System.Globalization.NumberStyles.AllowHexSpecifier to parse a hexadecimal string. The only restriction is that the string can not be prefixed with "0x" or "&h".
Dim unsigned32 As UInt32
Dim itParsed As Boolean = UInt32.TryParse("99000533", System.Globalization.NumberStyles.AllowHexSpecifier, Nothing, unsigned32)
The Val method returns Double. This cannot be changed
One workaround would be to use Long data type in the final result (that is, your leftPaddingHex) and to check if the result in the intermediate stage is negative, we add it with UInt.MaxValue + 1 to correct it:
Dim leftPaddingHex As Long = Val("&H" & "99000533")
If leftPaddingHex < 0 Then
leftPaddingHex = leftPaddingHex + UInt32.MaxValue + 1
End If
For hexadecimal with even larger number, I suggest you to take a look on this

Find Nth number in a long variable in vbnet

I have a very long number that goes like this:
numb as long=011212201220200112202001200101121220200120010112200101120112122....
It will be more than 4,000,000,000 digits. My problem is to find any digit in the number. If it was integer I would convert to string and do this:
numb(200)
But his is Long. Do you know how to find this?
As with integers, you can convert a long into a string too, in order to get the n-th element
Dim numb As Long = 9876543210
Dim targetDigit As Integer = 3 ' Set target as the 3rd digit
numb.ToString()(targetDigit -1) ' Retuns the 3rd digit: 7
Side note: I suspect that you may know this but, a long datatype can only hold
Integers ranging in value from -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807
That's only 19 digits! No where near 4 billion.
Source: MSDN

how i can figuring the highest number in my array...visual basic

how i can figure the highest number in my array...below is the code...can someone help me to solve my problems...n i wan to show the result in the label from the other windows form....thank u... :
Public Class Frm2
Public Parties(9) As String
Public Votes(9) As String
Dim vote As Integer
Dim Party As String
Party = TParty.Text
vote = TVote.Text
For I As Integer = 0 To Parties.Length - 1
If Parties(I) = "" Then
Parties(I) = TParty.Text()
For J As Integer = 0 To Votes.Length - 1
If Votes(J) = "" Then
Votes(J) = TVote.Text()
MsgBox(TParty.Text & TVote.Text & " votes")
TParty.Clear()
TVote.Clear()
Exit Sub
End If
Next J
End If
Next
MsgBox("you can vote now")
If you want to use an algorithm to find the highest number into an array (let's say Votes), the classic is coming from the so-called Bubble Sort:
Dim max As Long 'change the type accordingly, for example if votes are 1-10 then Integer is better
max = Votes(0) 'set the first vote as the max
For j = 1 To Votes.Length - 1
If Votes(j) >= max Then max = Votes(j) 'if another element is larger, then it is the max
Next j
Now the variable max stores the highest value of the array Votes, that you can show anywhere as, for example, in MyForm.MyLabel.Text = max. More useful info here.
Please note that now you declare Public Votes(9) As String, which means they are strings so not usable as numbers. You might want to declare them with a different data type, or use the CInt() method to convert strings in integers as suggested by ja72.
I thought this would only work with a Variant array, but in quick testing it seems to work with an array of Longs as well:
Dim Votes(9) as Long
Dim Max As Long
Max=WorksheetFunction.Max(Votes)
Note that, as Matteo says, you should change Votes() to an array of numeric types. I'd use Long, as it's a native VBA type.
EDIT: As noted by Dee, the code in this question is actually VB.Net. I added that as a tag. In VBA the solution would be even simpler, as Max is an array property:
Max=Votes.Max
(I suppose it would be a good idea to change the variable name from "Max".)

VBA overflow error with byte variables

Can someone please explain why the following code would generate an overflow error in VBA when the recipient of the operation c is an Integer?
Dim a As byte, b As Byte
Dim c As Integer
a = 3: b = 100
c = a * b
or does it mean that every operation involving 'Byte` variables would have to yield only a result be between 0 and 255 regardless of the recipient variable type?
or does it mean that every operation involving byte variables would have to yield only a result be between 0 and 255 regardless of the recipient variable type
Yes, because Bytes only hold values from 0 to 255, multiplying 3 x 100, you are passing (overflowing) its capacity, even though afterwards you are passing the result into an integer.
Because you are multiplying two Bytes together, VBA assumes the result to be a Byte too. It is only after the calculation that the result is then cast to an integer.
To get around this, you must cast at least one of the variables. This lets VBA know that it must make room for a larger result:
Dim a As Byte, b As Byte
Dim c As Integer
a = 3
b = 100
c = a * CInt(b) ' <-- Cast b as Integer to prevent Overflow error

Sum of elements (4) on mulitple lines in 2d array (txt file)

I have a text file that reads:
Left Behind,Lahaye,F,7,11.25
A Tale of Two Cities,Dickens,F,100,8.24
Hang a Thousand Trees with Ribbons,Rinaldi,F,30,16.79
Saffy's Angel,McKay,F,20,8.22
Each Little Bird that Sings,Wiles,F,10,7.70
Abiding in Christ,Murray,N,3,12.20
Bible Prophecy,Lahaye and Hindson,N,5,14.95
Captivating,Eldredge,N,12,16
Growing Deep in the Christian Life,Swindoll,N,11,19.95
Prayers that Heal the Heart,Virkler,N,4,12.00
Grow in Grace,Ferguson,N,3,11.95
The Good and Beautiful God,Smith,N,7,11.75
Victory Over the Darkness,Anderson,N,12,16
The last element of each line is a price. I would like to add up all the prices. I've been searching for so many hours now and cannot find a thing to answer my question. This seems soooo easy but I cannot figure it out!!! Please help out. BTW, this list is bound to change (adding of lines, deletion of lines, altering of lines) so if you can, please nothing concrete but instead leave the code open to changes. Thanks!!!
Just so you can see my pooooorrrr work, here is what I have (I think I deleted my code and rewrote a different way for several hours now.):
Dim Inv() As String = IO.File.ReadAllLines("Books.txt")
Dim t As Integer = Inv.Count - 1
Dim a As Integer = 0 to t
Dim sumtotal As String = sumtotal + Inv(4)
also,
for each line has either an "F" or an "N". how do I add up all the F's and all the N's. Do I do it via if statements?
First, you'll be better off using Double as your type instead of String. Second, observe how I use the Split function on each line, cast its last element as a double, and add it to the total. Yes, using an If Statement is how you can determine whether or not to add to the count of F or the count of N.
Dim lstAllLines As List(Of String) = IO.File.ReadAllLines("Books.txt").ToList()
Dim dblTotal As Double = 0.0
Dim intCountOfF As Integer = 0
Dim intCountOfN As Integer = 0
For Each strLine As String In lstAllLines
Dim lstCells As List(Of String) = strLine.Split(",").ToList()
dblTotal += CDbl(lstCells(3))
If lstCells(2) = "F" Then
intCountOfF += 1
Else
intCountOfN += 1
End If
Next