[SOLVED] Problem translating VB6 to PB (serial port )

Just starting out? Need help? Post your questions and find answers here.
CrazyFrog112
User
User
Posts: 47
Joined: Sun Jan 27, 2008 6:17 pm

[SOLVED] Problem translating VB6 to PB (serial port )

Post by CrazyFrog112 »

Hello, I have made a PC Thermometer that sends data trough serial port ( COM ).
I got a VB6 source code that reads correctly the temperature and when I translated into PB code, my return value is not correct.

Here are the functions in VB6 :

Code: Select all

Option Explicit
Dim MSComm1 As Object

Public Function open_iic_bus(serial_port As Object) As Integer
    Set MSComm1 = serial_port
    If Not MSComm1.PortOpen Then
        MSComm1.PortOpen = True
        MSComm1.RTSEnable = True
        MSComm1.DTREnable = True
    End If
    open_iic_bus = NO_ERROR
    Exit Function
End Function

Public Sub close_iic_bus()
    If MSComm1.PortOpen Then
        MSComm1.PortOpen = False
    End If
End Sub

Public Sub IIC_start()
        SDA_output
        SDA_high
        SCL_high
        SDA_low
End Sub

Public Sub IIC_stop()
        SDA_output
        SDA_low
        SCL_high
        SDA_high
End Sub

Public Function IIC_tx_byte(ByVal B As Long) As Integer
    Dim i As Integer
    For i = 0 To 7
        If (B And &H80) Then
            IIC_tx_bit_1
        Else
            IIC_tx_bit_0
        End If
        B = B * 2
    Next
    IIC_tx_byte = IIC_rx_bit
End Function

Public Function IIC_rx_byte(ByVal acknowledge As Integer) As Integer
    Dim i As Integer
    Dim retval As Integer
    For i = 0 To 7
        retval = retval * 2
        If IIC_rx_bit() Then
            retval = retval + 1
        End If
    Next
    If acknowledge Then
        IIC_tx_bit_0
    Else
        IIC_tx_bit_1
    End If
    IIC_rx_byte = retval
End Function
 
Private Sub IIC_tx_bit_1()
        SCL_low
        SDA_output
        SDA_high
        SCL_high
        SCL_low
End Sub
         
Private Sub IIC_tx_bit_0()
        SCL_low
        SDA_output
        SDA_low
        SCL_high
        SCL_low
End Sub
                         
Private Function IIC_rx_bit() As Integer
        Dim retval As Integer
        SDA_input
        SCL_low
        SCL_high
        retval = SDA_value()
        SCL_low
        IIC_rx_bit = retval
End Function
                          
Private Sub SDA_output()
    'there is no real difference between SDA_output
    'and SDA_input, because we have two unidirectional
    'lines instead of a single bidirectional one
    'This call is left here for compatibility with
    'other hardware implementations
    MSComm1.DTREnable = True
End Sub

Private Sub SDA_input()
    'since DATA is open-collector, putting DTR high sets it as PC input
    MSComm1.DTREnable = True
End Sub

Private Sub SCL_high()
    MSComm1.RTSEnable = True
    iic_wait
End Sub

Private Sub SCL_low()
    MSComm1.RTSEnable = False
    iic_wait
End Sub

Private Sub SDA_high()
    MSComm1.DTREnable = True
    iic_wait
End Sub
Private Sub SDA_low()
    MSComm1.DTREnable = False
    iic_wait
End Sub

Private Function SDA_value() As Integer
    SDA_value = MSComm1.CTSHolding
End Function


Private Sub iic_wait()
'void , insert here wait code for very fast systems
'actual measured speed is 1,5 kbps on a P90...
End Sub

Here is the main function :

Code: Select all

Private Function temperature(ByVal address As Integer) As Double

    Dim temperature_int As Long
    Dim temperature_frac As Long
    
    'For I2C bus communication, addresses are shifted one place to left,
    'as the least significant bit is used for the R/W flag.
    'In binary, shifting to left is equivalent to  multiplying by two
    address = address * 2
    
    On Error GoTo errors
    open_iic_bus MSComm1.object
    
    'an extra stop doesn't hurt...and ensures we start from a clean bus condition
    IIC_stop
    
    'read sequence, as per DS1621 datasheet
    IIC_start                           'Bus Master initiates a START condition.
    IIC_tx_byte address                 'Bus Master sends DS1621 address; R/ W= 0 (DS1621 generates acknowledge bit).
    IIC_tx_byte &H22                    'Bus Master sends Access Config command protocol.DS1621 generates acknowledge bit.
IIC_stop
    

    IIC_start                           'Bus Master initiates a START condition.
    IIC_tx_byte address                 'Bus Master sends DS1621 address; R/ W= 0 (DS1621 generates acknowledge bit).
    IIC_tx_byte &HAC                    'Bus Master sends Access Config command protocol.DS1621 generates acknowledge bit.
    IIC_tx_byte (12)
IIC_stop                             'DS1621 generates acknowledge bit.
   
    IIC_start                           'Bus Master generates a repeated START condition.
    IIC_tx_byte address                 'Bus Master sends DS1621 address; R/ W= 0.DS1621 generates acknowledge bit.
    IIC_tx_byte &H51                    'Bus Master sends Start Convert T command protocol.DS1621 generates acknowledge bit.
    IIC_stop                            'Bus Master initiates STOP condition.

    
    IIC_start                           'Bus Master initiates a START condition.
    IIC_tx_byte address                 'Bus Master sends DS1621 address; R/ W= 0 (DS1621 generates acknowledge bit).
    IIC_tx_byte &HAA                    'Bus Master sends Read Temperature command protocol.DS1621 generates acknowledge bit.
    IIC_start                           'Bus Master generates a repeated START condition.
    IIC_tx_byte address + 1             'Bus Master sends DS1621 address; R/ W= 1 = READING (DS1621 generates acknowledge bit).
    temperature_int = IIC_rx_byte(1)    'Bus Master receives first byte of data and generates acknowledge.
    
    temperature_frac = IIC_rx_byte(1)   'Bus Master receives second byte of data from DS162 and does not generate acknowledge to signal end of reception.
    IIC_stop                            'Bus Master initiates STOP condition.
    
    'some bynary math to convert to a data format Visual Basic can understand
    temperature = (temperature_int * 256 + temperature_frac) / 128 * 5 / 10
    If temperature_int >= 128 Then
        temperature = temperature - 256
    End If
    Exit Function

errors:
    temperature = ERROR_TEMPERATURE_NOT_READ
End Function
And here is what I translated :

Code: Select all

;Procedure SDA_Value()



Procedure.i open_iic_bus()
 comport.s = "COM1"
 OpenSerialPort(0, comport, 9600, #PB_SerialPort_NoParity, 7, 1, #PB_SerialPort_RtsCtsHandshake, 1024, 1024)
EndProcedure

Procedure close_iic_bus()
CloseSerialPort(0)
EndProcedure

Procedure iic_wait()
EndProcedure

Procedure SDA_value()
sda_value = GetSerialPortStatus(0, #PB_SerialPort_CTS)
ProcedureReturn sda_value
EndProcedure

Procedure SDA_low()
SetSerialPortStatus(0, #PB_SerialPort_DTR, 0)
EndProcedure

Procedure SDA_high()
SetSerialPortStatus(0, #PB_SerialPort_DTR, 1)
EndProcedure

Procedure SCL_low()
SetSerialPortStatus(0, #PB_SerialPort_RTS, 0)
EndProcedure

Procedure SCL_high()
SetSerialPortStatus(0, #PB_SerialPort_RTS, 1)
EndProcedure

Procedure SDA_input()
SetSerialPortStatus(0, #PB_SerialPort_DTR, 1)
EndProcedure

Procedure SDA_output()
SetSerialPortStatus(0, #PB_SerialPort_DTR, 1)
EndProcedure

Procedure IIC_rx_bit()
SDA_input()
SCL_low()
SCL_high()
retval = SDA_value()
SCL_low()
IIC_rx_bit = retval
ProcedureReturn IIC_rx_bit
EndProcedure

Procedure IIC_tx_bit_0()
        SCL_low()
        SDA_output()
        SDA_low()
        SCL_high()
        SCL_low()
EndProcedure

Procedure IIC_tx_bit_1()
        SCL_low()
        SDA_output()
        SDA_high()
        SCL_high()
        SCL_low()
EndProcedure

Procedure IIC_rx_byte(acknowledge.l)
  For i=0 To 7
  retval = retval * 2
        If IIC_rx_bit() 
            retval = retval + 1
        EndIf
  Next i
  
  If acknowledge 
        IIC_tx_bit_0()
    Else
        IIC_tx_bit_1()
    EndIf
    IIC_rx_byte = retval
 ProcedureReturn IIC_rx_byte
 EndProcedure
 
Procedure IIC_tx_byte(B.l)
   For i=0 To 7
    If B And $80
   
            IIC_tx_bit_1()
        Else
        
            IIC_tx_bit_0()
        EndIf
        B = B * 2
   Next i
    IIC_tx_byte = IIC_rx_bit 
    ProcedureReturn IIC_tx_byte
EndProcedure
 
Procedure IIC_stop()
        SDA_output()
        SDA_low()
        SCL_high()
        SDA_high()
        
EndProcedure

Procedure IIC_start()
        SDA_output()
        SDA_high()
        SCL_high()
        SDA_low()
        
EndProcedure   

     Procedure.d temperature(adress.l)
     adress = adress * 2
    ; Debug adress
     open_iic_bus()
    IIC_stop()
     IIC_start()
     IIC_tx_byte(adress)
     IIC_tx_byte($22)
     IIC_stop()
     IIC_start()
     IIC_tx_byte(adress)
     IIC_tx_byte($AC)
     IIC_tx_byte($C)
     IIC_stop() 
     IIC_start()
     IIC_tx_byte(adress)
     IIC_tx_byte($51)
     IIC_stop()
     Delay(1000)
     IIC_start()
     IIC_tx_byte(adress)  
     IIC_tx_byte($AA)
     Delay(1000)
     IIC_start()
     ass=adress+1
     IIC_tx_byte(ass)
     Delay(1000)   
     temperature_int.l = IIC_rx_byte(1)
     temperature_frac.l = IIC_rx_byte(1)
     IIC_stop()
     Debug temperature_int
     Debug temperature_frac
     temperature = ((temperature_int * 256 + temperature_frac) / (128 * 5 ))/ 10
    If temperature_int >= 128 
        temperature = temperature - 256
    EndIf
    ProcedureReturn temperature
    EndProcedure
    For i=1 To 5
    Debug temperature($48)
    Delay(1000)
    Next i

Am I missing something? Can anyone tell me if it is an error in translation?
Thanks in advance !
Last edited by CrazyFrog112 on Mon Jun 07, 2010 8:49 am, edited 1 time in total.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Problem translating VB6 to PB (serial port communication

Post by SFSxOI »

are you sure your math is correct?
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
CrazyFrog112
User
User
Posts: 47
Joined: Sun Jan 27, 2008 6:17 pm

Re: Problem translating VB6 to PB (serial port communication

Post by CrazyFrog112 »

Yes.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Problem translating VB6 to PB (serial port communication

Post by SFSxOI »

Math is always the first thing I think about when I convert VB6 things like this to PB. I invariably do something in the conversion wrong for some reason. :)

From what I can tell the conversion looks pretty straight forward, your conversion looks ok on first glance.

For this:

Code: Select all

In the VB6 code:

IIC_tx_byte (12)

In your code

IIC_tx_byte($C)
Are you sure that function is not expecting an integer instead of hex? Just an odd guess, cause thats the only thing I can see that would be any different in your conversion. Yes, int 12 is = $C hex - i'm just taking a stab at it cause i dont see anything else in the code right now - and the original VB6 code says integer on a lot of things but i don't think it makes any difference really.
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

Re: Problem translating VB6 to PB (serial port communication

Post by Perkin »

taking the two temperature_int and temperature_frac and assigning a default value of 10 and 17

Code: Select all

temperature_int=10
temperature_frac=17
Debug (temperature_int * 256 + temperature_frac) / 128 * 5 / 10
Debug ((temperature_int * 256 + temperature_frac) / (128 * 5 ))/ 10
returns different values (10 and 0), so just remove the extra brackets from the second line.

You might also want to change those two vars into *.f floats - will give better precision
%101010 = $2A = 42
CrazyFrog112
User
User
Posts: 47
Joined: Sun Jan 27, 2008 6:17 pm

Re: Problem translating VB6 to PB (serial port communication

Post by CrazyFrog112 »

I tried both suggestions, and still nothing. I get -1.0 ( that means temperature_int = 255 and temperature_frac = 255 ).
Maybe is because how I opened the serial port.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Problem translating VB6 to PB (serial port communication

Post by SFSxOI »

The port open looks ok, unless parity or data - stop bits makes a difference, or handshaking.

Try the data param set to 8:

Code: Select all

OpenSerialPort(0, comport, 9600, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_RtsCtsHandshake, 1024, 1024)
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

Re: Problem translating VB6 to PB (serial port communication

Post by Perkin »

I see in you PB code, you've got two Debug commands to show the temperature_* values,
What are they returning.
%101010 = $2A = 42
CrazyFrog112
User
User
Posts: 47
Joined: Sun Jan 27, 2008 6:17 pm

Re: Problem translating VB6 to PB (serial port communication

Post by CrazyFrog112 »

They are returning 255 , both of them
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Problem translating VB6 to PB (serial port communication

Post by SFSxOI »

If they are both 255 then the return should be -246 with the math as its presently in your code.

with this:

Code: Select all

temperature = ((temperature_int * 256 + temperature_frac) / 128 * 5 ) / 10

both at 255 then the return should then be -1

but your getting 1.0 ?
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
CrazyFrog112
User
User
Posts: 47
Joined: Sun Jan 27, 2008 6:17 pm

Re: Problem translating VB6 to PB (serial port communication

Post by CrazyFrog112 »

With math in my code, yes, I get -246, but if I correct the brackets as in your example, i get -1.0. But the real temperature that the VB6 compiled program shows is 21.88 degrees.
infratec
Always Here
Always Here
Posts: 7623
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Problem translating VB6 to PB (serial port communication

Post by infratec »

Hi CrazyFrog112,

when you read in 255 for both values, than it has nothing todo with the
later coming math part (in first case).

First:
I don't think that it is allowed to make a 1 second delay inside the I2C packet.
So please comment out the 3 Delay(1000) commands.
(They are also not available in the VB6 code)

Than, according to the datasheet you have to wait at least 10ms after a write to the
config register. So please add a Delay(15) after the IICStop after writing AC and C.

But in general:
You have to check if something happens on the COM port.

Since you are able to solder ICs, I think you have also a multimeter.
So please add Delays(1000) in your SetSerialPortStatus() procedures.
Than you should be able to meassure the signals at the COM port pins.

If you can see something (which should match your code), you should try
to set the config register and read the value back. (After removing the delays :mrgreen: )
If you can read what you have written, you can be sure that your basic routines are working.

Than you can look deeper in the rest of the code.

I hope this are a few hints which helps you to solve the problem.

Best regards,

Bernd
infratec
Always Here
Always Here
Posts: 7623
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Problem translating VB6 to PB (serial port communication

Post by infratec »

Oh, one other thing:

I can't see a start conversion command ($EE),
I see only a stop conversion command ($22)

Even in the continious mode you have to start the conversion.
An also again when you stopped it with $22.

So I'm not sure if your main program flow is Ok.

In generall: please read the datasheet of the DS1621.

Bernd
CrazyFrog112
User
User
Posts: 47
Joined: Sun Jan 27, 2008 6:17 pm

Re: Problem translating VB6 to PB (serial port communication

Post by CrazyFrog112 »

Yes, I agree, but the VB6 source code as I posted, works fine if I compile.
CrazyFrog112
User
User
Posts: 47
Joined: Sun Jan 27, 2008 6:17 pm

Re: Problem translating VB6 to PB (serial port communication

Post by CrazyFrog112 »

I also think that this piece of code has to do with it :

Code: Select all

Public Function IIC_tx_byte(ByVal B As Long) As Integer
    Dim i As Integer
    For i = 0 To 7
        If (B And &H80) Then
            IIC_tx_bit_1
        Else
            IIC_tx_bit_0
        End If
        B = B * 2
    Next
    IIC_tx_byte = IIC_rx_bit
End Function
the If (B And &H80 ) looks suspect.
Post Reply