IOWarrior24 MAX127.vbp Problem with VB6 code crash

This is the English forum for all topics related to IO-Warrior. Please post in English only

Moderator: Guido Körber

Post Reply
MartinP
Posts: 3
Joined: Sat Mar 11, 2006 8:54 am

IOWarrior24 MAX127.vbp Problem with VB6 code crash

Post by MartinP »

I have started a project using Max127 and VB6
There is a problem in this Code area.

Dim N As Long
Dim Report(8) As Byte

' MAXIM127 control byte
' START, I = Channel index, 0-5 V = RNG 1 and BIP 1, normal operation
Report(3) = &H80 Or (I * 16)

Ret = IowKitWrite(IOWarrior, IOW_PIPE_SPECIAL_MODE, Report(0), 8)
' read answer containing voltage value
Ret = IowKitRead(IOWarrior, IOW_PIPE_SPECIAL_MODE, Report(0), 8)

' extract bytes of value

N = (Report(2) * 256) Or Report(3)

' value is upper 12 bits of 16 bits
N = N / 16

If Report(2) has a value > 127 then the program crashes due to overflow because (Report(2) * 256) > 32768. On the 0-5 v Scale this represents a value of 2.50 v. i.e. 50% of full scale. From 0 to 2.50 v system works OK.

Any idea what is causing this?

MartinP
Robert Marquardt
Posts: 543
Joined: Mon Dec 01, 2003 6:09 pm

Post by Robert Marquardt »

Oops, i have not tested good enough (i hate VB6).
Try
N = Report(2)
N = N*256
N = N or Report(3)
MartinP
Posts: 3
Joined: Sat Mar 11, 2006 8:54 am

MAXIM127.vbp

Post by MartinP »

Hi Robert
Thanks for your prompt reply.
Unfortunately, although the program doesn't crash now, it is OK for 0 to 2.50 volts (ie 50% input) but displays decreasing negative values for input voltages in the range 2.50 to 5.00 volts. Could be something to do with the shift right / shift left function carried out by Report(3)???

Any ideas?

Thanks
Martin
Robert Marquardt
Posts: 543
Joined: Mon Dec 01, 2003 6:09 pm

Post by Robert Marquardt »

Change to

N = Report(2) and &HFF
N = N *256
N = N or Report(3)

VB is obviously sign extending on the first assignment. Now i know again *why* i hate VB.
MartinP
Posts: 3
Joined: Sat Mar 11, 2006 8:54 am

MAXIM127.vbp Code

Post by MartinP »

Hi Robert
I have fixed my problem by reassigning N (Long) to a new variable, M (Integer). I’m not sure of the reason but the following code appears to work properly now !!

N = Report(2) ' The “And &HFF” does not work
N = N * 256
N = N Or Report(3)

N = N / 16 ' value is upper 12 bits of 16 bits
' value is 12 bit signed!
' We need to add the upper twos complement sign bits for the Integer.
' This is a sign extension from 12 bit to 32 bit.

If (N And &H800) <> 0 Then
N = N Or &HFFFFF000
End If

Dim M As Integer

M = N ‘assign to Integer (M)
‘value now is a signed integer with 2047 = 10 V and -2047 = -10 V

Values(I).Caption = Format(M * 10# / 4096#, "0.000") ‘for +/- 10 v scale

Thanks a lot for your assistance. Hopefully, this can help others
Regards
Martin
Post Reply