Send Byte to the K8056 RelaisCard

I am trying to send a Byte thru the serial port to the K8056 Card. In my efforts I was partly successful. With my code I can send only decimal numbers a bow 44. The card ignores everything lower then 45 decimal. To get my program work properly, I have to be able to send all numbers from 1 to 255. Can you help me please? Here is the code that I use to send data to the Card:

Public Class Form1

    Dim Port As Integer
    Dim Portteller As Integer
    Dim Portname As String

    Private Sub CBPORT_SelectedIndexChanged(
             ByVal sender As System.Object,
             ByVal e As System.EventArgs)
        Handles CBPORT.SelectedIndexChanged

        SerialPort1.PortName = "COM" & CBPORT.Text
        On Error Resume Next
        SerialPort1.BaudRate = 2400
        SerialPort1.StopBits = 1
        SerialPort1.DataBits = 8

        If SerialPort1.IsOpen Then
            CBPORT.BackColor = Color.Green
            CBPORT.BackColor = Color.Red
        End If

    End Sub

    Private Sub ButSet_Click(ByVal sender As System.Object,
            ByVal e As System.EventArgs) Handles ButSet.Click
    End Sub

    Sub SendCommand()
        Dim Buf As String
        Dim Cardnr As Integer  ' Cardnumber 
        Dim ByteToWrite As String   ' Relais number
        Dim Chksom As Integer  ' Checksom
        Dim I As Integer

        Cardnr = Int(TBAdres.Text)
        ByteToWrite = TBByte.Text
        I = CInt(ByteToWrite)
        Chksom = 256 - ((13 + Cardnr + Asc("B") + I) Mod 256)
        Buf = Chr(13) & Chr(Cardnr) & "B" & Chr(I) & Chr(Chksom)

        If SerialPort1.IsOpen = True Then SerialPort1.Write(Buf)

    End Sub
End Class

Best regards,
Igor Juricic

Hey, try the following:

Public Const COMMAND_SEND_BYTE As Byte = &H42 ' "B"

Sub Foobar()
  SerialPort1.PortName = "COM1"
  SerialPort1.BaudRate = 2400
  SerialPort1.StopBits = 1
  SerialPort1.DataBits = 8

  Dim buffer(4) As Byte ' 0..4
  buffer(0) = &HD
  buffer(1) = Byte.Parse(TBAdres.Text)
  buffer(2) = COMMAND_SEND_BYTE
  buffer(3) = Byte.Parse(TBByte.Text)
  buffer(4) = 256 - ((buffer(0)+buffer(1)+buffer(2)+buffer(3)) Mod 256)

  SerialPort1.Write(buffer, 0, buffer.Length())

End Sub

Try not to use strings for binary data, since they are meant to handle text. The write method on IO.Ports.SerialPort is overloaded to handle a byte() buffer.


Thanks for the hint. It works perfect. Only I had to change

buffer(4) = 256 - ((buffer(0)+buffer(1)+buffer(2)+buffer(3)) for numbers larger than 171

Dim Buf As Integer

Buf = Cint(buffer(0))+Cint(buffer(1))+Cint(buffer(2))+Cint(buffer(3)))

buffer(4) = 256 - (Buf Mod 256)

Glad it’s working :slight_smile: Seems like byte+byte is implicitly converted to int, bah! You might also want to move the checksum code to a function in case you need it for other packets: (haven’t tested it yet though, but it should be ok)

Function Checksum(ByRef buffer As Byte()) As Byte
    Dim chk As Integer = 0

    For I As Integer = 0 To buffer.Length - 2
        chk = chk + buffer(I)

    Return CByte(256 - (chk Mod 256))
End Function

Or you could check “Remove integer overflow checks” in your project properties