K8056 Byte Instruction

I can control a single relay using the code as per published example.
I can not get the relay to respond when I attempt control via “B” - send byte.

Does anyone know the correct syntax for the use of a byte in the expression for the checksum? I want to address 2 relays at a time.

I am using vb 2005 and checksums greater than 127 display on a monitor as ?. The problem is that character between 128 - 255 are not represented in the standard vb encoding.
If I use unicode encoding I obtain the same character sequence as in the published example but the relays will not respond.
I monitor the transmitted data on a hyperTerminal.

The following is the code I am trying to use for control with a single byte command.

[code]
Private Sub Send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Send.Click
Dim checksum As Integer
Dim Relays As Byte
Relays = (TextBox2.Text)
checksum = (255 - ((((13 + 1 + Asc(“B”) + (Relays)) / 256) - Int((13 + 1 + Asc(“B”) + (Relays)) / 256)) * 256)) + 1

    messagestring = Chr(13) & Chr(1) & "B" & (Relays) & Chr(checksum)
    messagestring = messagestring & messagestring
    messagestring = messagestring & messagestring
    SendSerialData(messagestring)
End Sub

Sub SendSerialData(ByVal data As String)
    '  Dim enc As Encoding = Encoding.Unicode
    '  Send strings to a serial port.
    Using com1 As IO.Ports.SerialPort = _
    My.Computer.Ports.OpenSerialPort("COM1", 2400, IO.Ports.Parity.None, 8, IO.Ports.StopBits.One)

         '  com1.Encoding = enc  
        com1.WriteLine(data)
    End Using
End Sub[/code]

I need to control 2 relays at a time.
Thanks

This VB6 example turns on relay 7 and 8 at the same time:
3 = 1 + 2 = relay 7 & relay 8

[code]checksum = (255 - ((((13 + address + Asc(“B”) + 3) / 256) - Int((13 + address + Asc(“B”) + 3) / 256)) * 256)) + 1

messagestring = Chr$(13) & Chr$(address) & “B” & Chr$(3) & Chr$(checksum)
messagestring = messagestring & messagestring
messagestring = messagestring & messagestring
comm.Output = messagestring[/code]

The checksum syntax now works as long as no symbols or number has a code value larger than 128. The number represented by the byte must be greater that 48 for the values to be below 128.

What encoding is being used in the PIC device?

The behavior stated above occurred with ASCII or UTF-8 encoding. It was expected using ASCII since above 127 a ? is passed.

I am using vb 2005.
Any suggestions?

PIC uses ASCII encoding. We have no experience with this problem.

Would you verify that the vb6 example 3 = 1 + 2 = relay 7 & 8 does turn on those relays?

You don’t mean to imply 8 = 3 + 5 = relay 6 & 4 do you?
I think you just happened to chose an example that the binary representation matches the arithmetic.
I would think that these relays would be control by a 20 = 00010100. Is this correct?

I’ve tested the above code before I’ve posted it. It does turn on relay 7 and 8. 00010100 should turn on relay 4 & 6.

I installed vb6 and wrote the control program for the relays.
With vb6 everything works!!!

So the big questions is "what is the difference between vb6 and vb 2005?
The symbols look the same on the Hyper Terminal.

Also, I now have 2 boards working on an RS232 multi-drop. This is not recommended. Have you any experience with multi-drop and the relay boards?

An interesting note:
The Toggle (T), Set (S) and Pulse§ all will only respond to bytes of numeric value from 49 - 56. “S” & 57 sets all relays.

As we have no experience with VB2005, we cannot tell what the differences are.

Sorry, no experience with a multi-drop

T, S and P only respond to 49-57 ascii, this is documented in the manual.
49 ascii = 1, 56 ascii = 8 (= relay 1 to 8)
57 ascii= 9 = set all relays

Here are some useful links you can check for the differences between VB 6.0 and VB 2005.

Visual Basic Language Concepts: msdn2.microsoft.com/en-us/library/2x7h1hfk(VS.80.aspx
What’s New for Visual Basic 6.0 Users: msdn2.microsoft.com/en-us/library/ms172617(VS.80.aspx
Language Changes for Visual Basic 6.0 Users: msdn2.microsoft.com/en-us/library/skw8dhdd(VS.80.aspx

Very important!
Integer Data Type for Visual Basic 6.0 Users: msdn2.microsoft.com/en-us/library/7f5ztkz3(VS.80.aspx

For example:
Visual Basic 6.0 declaration: “As Long”
should be changed for Visual Basic 2005 to: “As Integer”

Visual Basic 6.0 declaration: “As Integer”
should be changed for Visual Basic 2005 to: “As Short”

Also important especially when calling DLL functions!
Parameter Passing Mechanism for Visual Basic 6.0 Users: msdn2.microsoft.com/en-us/library/41zywfyc(VS.80.aspx

In Visual Basic 6.0, if you do not specify ByVal or ByRef for a procedure parameter, the passing mechanism defaults to ByRef. This allows the variable passed into the procedure to be modified in the calling program.

When you declare a procedure in Visual Basic 2005, the passing mechanism defaults to ByVal for every parameter. This protects arguments against modification.

See also:

     Array Bounds for Visual Basic 6.0 Users 
     Array Size Declaration for Visual Basic 6.0 Users 
     ReDim Statement for Visual Basic 6.0 Users 

     Data Type Changes for Visual Basic 6.0 Users 
     Universal Data Type Changes for Visual Basic 6.0 Users 

     Declaration Syntax for Visual Basic 6.0 Users 
     String Length Declaration for Visual Basic 6.0 Users 
     Structure Declaration for Visual Basic 6.0 Users 
     Variable Scope for Visual Basic 6.0 Users 

    Function Changes for Visual Basic 6.0 Users 

     Format Function for Visual Basic 6.0 Users 
     Date and Time for Visual Basic 6.0 Users 
     String ($) Function for Visual Basic 6.0 Users 

    Miscellaneous Language Changes for Visual Basic 6.0 Users 

     Boolean Operator for Visual Basic 6.0 Users 
     File Handling for Visual Basic 6.0 Users 
     Module Changes for Visual Basic 6.0 Users 

    Procedure Changes for Visual Basic 6.0 Users 

     Parameter Passing Mechanism for Visual Basic 6.0 Users 
     Procedure Calling Sequence for Visual Basic 6.0 Users 
     Procedure Declaration for Visual Basic 6.0 Users

etc…

Thank you for your prompt replies.
Your service has been exceptional.

I was able to determine the encoding page after getting the system working with vb6. I now have the relays controlled by my vb 2005 program.

You may see this issue again since there will be more use of vb 2005. For your future reference the required character page is 1252. The following is a snippet of the code I use.

Sub SendSerialData(ByVal data As String) Dim enc As Encoding = Encoding.GetEncoding(1252) ' Send strings to a serial port. Using com1 As IO.Ports.SerialPort = _ My.Computer.Ports.OpenSerialPort("COM1", 2400, IO.Ports.Parity.None, 8) com1.Encoding = enc com1.WriteLine(data) End Using End Sub

Thanks again.

PS: 1 more question. What is the latency time of the cpu? I estimate it to be about 200us. I would like to turn on 2 relays located on different boards in rapid secession.

Please refer to the manual. It gives hints regarding the instruction timing. Respect this timing. You can optimise your software by increasing the speed until it no longer works, then reduce it again to a allow a reasonable margin.

Thats exactly what I have already done.
Thought you may have more information.
I will use 2 ports rather than attempt the multi-drop.
Thanks.

Hi Everybody
Is anybody able to give me an Example of Terminal Strings to switch Relais 1 on and off?

Many Thanks