K8056 problem with serial port, VB 2008

Hi,
I 've just finished my K8056 Velleman kit. I Try to write a program in VB 2008 but I don’t really know how to use VB.

here my program, I just want to set the relay n°1 with a button

Imports System
Imports System.IO.Ports

Public Class Form1
    Dim Port As SerialPort = New SerialPort("COM1", 2400, Parity.None, 8, StopBits.One)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Essai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Essai.Click
        Port.Open()
        Port.Write(Chr(13) & "1" & "S" & "1" & "88")
        Port.Close()
    End Sub
End Class

How can I calculate the Checksum, is it really necessary?
what is the code to configurate the serial port, is it correct?

thanks for your answer

fred

Sorry, I have no VB2008 experience.
Regarding the checksum calculation, please check the VB6 demo source.

ok but do you have any example with VB6?? It should be almost the same program…

regards

See above: The source code of the demo program is available for download. It contains all possible commands for this item.

You serial port settings are correct :slight_smile:

velleman.be/downloads/files/ … b_demo.zip
Try reusing the calculations made in the VB6 code and see if it works

ok thanks for your answer but i think the problem is in the parameter that i try to send to the card: CHR$13 (is it 0x13 in hexa?), adress 1 (in hex?), S (in ASCII code?), adress 1 (in ASCII code?):

00010011 : CHR$13 (ASCII)? or 0x13 in hex
00000001 : adresse of the card --> 1
01010011 : instruction ‘S’ in ASCII
00011001 : nb of the relais --> 1 or 0x31 in hex

10000000 : addition
01111111 : 2-complement
00000001+: add one

10000000 : checksum=128??? is CORRECT??

Nothing happen on the card but with the demo program, it’s working…
fred

13 = 0x0D
 1 = 0x01
83 = 0x53
49 = 0x31

i’m not entirely sure, but try 18 (0x12) as the checksum
in hex: - (FF - (0D+01+53+31)) = -6D = 12

ok thanks but what about the +1??

I understand (FF - (0D+01+53+31)), I suppose it’s the 2-complement but why do you put a “-” before all the equation??? (sorry i’m a beginner)

fred

PS: I suppose that the Led D10 on board is red only in case of a good communication between the card and the pc??

Hi,

Just to say that I found the error: Here my program in VB2008 just to communicate with the K8056 board and set the relay one. I can send you the code when i will finish it if you want.

[code]
Imports System
Imports System.IO.Ports

Public Class Form1
Dim Port As IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort(“COM1”, 2400, IO.Ports.Parity.None, 8, IO.Ports.StopBits.One)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Port.Close()
End Sub

Private Sub Essai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Essai.Click

    Dim messagestring As String
    messagestring = Chr(13) & Chr(1) & "S" & Chr(49) & Chr(110) 'number in decimal
    messagestring = messagestring & messagestring
    messagestring = messagestring & messagestring

    Port.Open()
    Port.Write(messagestring)
    Port.Close()
End Sub

End Class[/code]

Thanks for your help…

see u

PS: for the checksum in hexa: [FF-(0D+01+53+31)]+1 = 6E or 110 in decimal

Hi all

I found out that the formula of the demo program may get a wrong checksum of 256 while using the command “Send a Byte” with the value 176.
That’s why I came to the following solution to get the checksum for the K8056 in VB6:

Public Function funcChecksum(bytAddress As Byte, strCommand As String, bytValue As Byte) As String

Dim X2 As Integer

X2 = 13 + bytAddress + Asc(strCommand) + bytValue
X2 = Not (X2)
X2 = X2 + 1
X2 = X2 And 255

funcChecksum = Chr$(X2)

End Function

hi all,

Here a routine function in VB2008 that works with the good formula for the checksum… address, command and relay_nb are integer.

[code] Public Function instruction_card8056(ByVal address As Integer, ByVal command As Integer, ByVal relay_nb As Integer) As Integer

    checksum = ((255 - (13 + address + command + relay_nb) + 1)) 'complément à 2 des 4 bytes

    messagestring = Chr(13) & Chr(address) & Chr(command) & Chr(relay_nb) & Chr(checksum)
    messagestring = messagestring & messagestring       'double envois de la commande
    messagestring = messagestring & messagestring       'double envois de la commande

    Port.Open()
    Port.Write(messagestring)
    Port.Close()

    Sleep(200)      'pause de 200ms entre 2 commande

    Exit Function
End Function

[/code]
Some define in the beginning of the main:

    Dim relay8 As Integer = 56 ' number "8" in ASCII
    Dim relay_on As Integer = 83 ' "S" character for SET

Here is a vb sub for sending the command “B” (send a byte)
using Val7654’s formula

[code]Private Sub K8056_SendByte(Relays As Byte)
'oparates 8 relays of k8056 with address address

Dim checksum As Integer

checksum = 13 + address + Asc(“B”) + Relays
checksum = Not (checksum)
checksum = checksum + 1
checksum = checksum And 255

factor = RTrim(LTrim(Chr$(Relays)))

messagestring = Chr$(13) & Chr$(address) & “B” & factor & Chr$(checksum)
messagestring = messagestring & messagestring
messagestring = messagestring & messagestring
MSComm.Output = messagestring
End Sub[/code]