K8056 - Send instructions with C#

Hello,

I want to send instructions with C# to my K8056. I just found this VB Demo but this couldn’t help me. I don’t understand this byte converting adding and so on thing. May someone give me an example for this?

This is my code for opening the Port. I hope this is correct.


            sP = new SerialPort("COM11");
            sP.BaudRate = 2400;
            sP.DataBits = 8;
            sP.Parity = Parity.None;
            sP.StopBits = StopBits.One;
            sP.Open();

Now I just need something to send.

Snake

Hello,

I don’t know exactly what I have to send. I know I have to send the card number and what to do and the relay number and the checksum. My problem is that I don’t know how I can create the checksum and in which format I have to send these components. May someone please help me?

Snake

Page 10 and 11 of the K8056 manual clearly explain the sequence that needs to be sent to K8056. The available commands are also explained.
If this is not enough information, you can download the K8056 demo prog source code (VB6) from our website. It allows you to study the command sequence. Even if you do not have a VB compiler, you can open the source as a .txt file

I did all this a long time before I asked.

Now I asked someone else.

Just in case someone else is interested in this information:


        SerialPort sP;

        /// <summary>
        /// InitRelayCard
        /// </summary>
        /// <param name="ComPort">e.g. "COM1"</param>
        public void InitRelayCard(string ComPort)
        {

            sP = new SerialPort(ComPort);

            sP.BaudRate = 2400;
            sP.DataBits = 8;
            sP.Parity = Parity.None;
            sP.StopBits = StopBits.One;

            sP.Open();

        }


        /// <summary>
        /// SetRelay
        /// </summary>
        /// <param name="Relay">e.g. '1';  1..8</param>
        /// <param name="CardAddress">1..255</param>
        public void SetRelay(char Relay, int CardAddress)
        {

            double checksumd = 13 + CardAddress + (int)'S' + (int)Relay;
            double checksum = 255 - (((checksumd / 256) - (int)(checksumd / 256)) * 256) + 1;
            string messagestring;

            messagestring = Convert.ToString((char)13) + Convert.ToString((char)CardAddress) + "S" + Relay + Convert.ToString((char)checksum);
            messagestring = messagestring + messagestring;

            sP.Write(messagestring);

        }

Hey snake,

I wrote a C# dll a while ago that does exactly what you want. Its simpler to use and has more options. Feel free to use the code and add to it, just keep My credits in the summery and add your own since there are still Caveats.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.Threading;


/// Created By: A.Rotteveel.
///
///
///             How To use
///
/// * Add the K8056 to the project reference,
/// * 
/// * 
/// * K58056 vellemancard = new K58056();                             //create a new object
/// * 
/// * vellemancard.Relay_Card = 1;                                    //set the relaycard number
/// * 
/// * vellemancard.Do_Action(K58056.Command.On, K58056.Switch.all);   // Turns everything on 
/// *                                                                 // with the basic Do_Action method
/// * 
/// * vellemancard.Do_Action(K58056.Command.Toggle, i);               // Toggles a switch wth the
/// *                                                                 // Do_Action overload
/// * vellemancard.Emergency()                                        // toggle the emergence stop
/// *
/// *
/// 
/// * CAVEAT
/// * 
/// * right now there is no command to retrieve or set the relaycard 
/// * number as set in the velleman card Pico.
/// */
/// 
public class K58056
{
    // create a serial port
    SerialPort port = new SerialPort("COM1", 2400, Parity.None, 8, StopBits.One);

    private int relay_card = 1; // set default relaycard number to 1

    public enum Command
    {
        //Commands and their ascii values
        On = 83,            //S
        Off = 67,           //C
        Emergency = 69,     //E
        Toggle = 84          //T
    }
    public enum Switch
    {
        // switches and their ascii value 
        one = 49,
        two = 50,
        three = 51,
        four = 52,
        five = 53,
        six = 54,
        seven = 55,
        eight = 56,
        all = 57
    }

    // make sure we can set and get the relay card number
    public int Relay_Card
    {
        get { return relay_card; }
        set { relay_card = value; }
    }

    //Constructor
    public void k8056()
    {
        this.Relay_Card = relay_card;
    }

    //Methodes

    // Do_Action,  the event to call when you wich to toggle a relay
    public void Do_Action (Command command, Switch switch_number)
    {
        //make a checksum
        int checksum =  CheckSum(this.Relay_Card, 
                                 Convert.ToInt32(command),
                                 Convert.ToInt32(switch_number));
        //make the string data to send to the com port
        string message = Message(this.Relay_Card,
                                 Convert.ToInt32(command),
                                 Convert.ToInt32(switch_number),
                                 checksum);
        // send the data to the com port
        Com_Write(message);
    }
    
    // overload for thoose whoom wish to adress the relay with an int
    // (for example thoose that want to loop through all the switches)
    public void Do_Action(Command command, int switch_number)
    {
        Switch relay = new Switch();
        switch (switch_number) //get the number and set the switch
        {
            case 1:
                relay = Switch.one;
                break;
            case 2:
                relay = Switch.two; 
                break;
            case 3:
                relay = Switch.three;
                break;
            case 4:
                relay = Switch.four;
                break;
            case 5:
                relay = Switch.five;
                break;
            case 6:
                relay = Switch.six;
                break;
            case 7:
                relay = Switch.seven;
                break;
            case 8:
                relay = Switch.eight;
                break;
            default:
                relay = Switch.all;
                break;
        }

        Do_Action(command, relay); // call the origional Do_Action()
    }
    
    // A simple way to call the emergency stop
    public void Emergency_Stop()
    {
        Do_Action(Command.Emergency, Switch.all);
    }

    // create a checksum
    private int CheckSum(int relaycard_address, int command, int switch_number)
    {
        // create the 'absolute' checksum
        float float_checksum = (float)(13 + relaycard_address + command + switch_number) / 256;
        // make an int checksum
        int int_checksum = (13 + relaycard_address + command + switch_number) / 256;
        //calculate the difference
        float difference = (float)(float_checksum - int_checksum) * 256;
        // voila, one checsum
        int checksum = 255 - Convert.ToInt32(difference) + 1;

        //return the checksum
        return checksum;
    }

    //create the data to send to the com port
    private string Message(int card_address, int command, int switch_number, int checksum)
    {
        // create the characters we need
        // based on the data we have
        char enterchr = (char)13;
        char card_addresschr = (char)card_address;
        char commandchr = (char)command;
        char relaynumberchr = (char)switch_number;
        char checksumchr = (char)checksum;

        //make a nice character array
        char[] message_char_array = new char[5];
        message_char_array[0] = enterchr;
        message_char_array[1] = card_addresschr;
        message_char_array[2] = commandchr;
        message_char_array[3] = relaynumberchr;
        message_char_array[4] = checksumchr;

        //and turn the character array into a string
        string message = new string(message_char_array);
        
        //make the message longer so we are sure to send the data atleast 4 times
        message += message;   // double the message once
        message += message;   // double it twice

        //send it back
        return message;
    }

    //write everything to the port
    private void Com_Write(string message)
    {
        // Open the port
        port.Open();
        // Write a string
        port.Write(message);
        // Close the port
        port.Close();

        // IMPORTANT SIDE NOTE************************************************************
                            // it takes about a second to send data to the port;        **
        Thread.Sleep(1000); // so lets make sure we wait 1 second before we continue    **
    }                       //************************************************************
}

compile this as a dll, and add it to your project, at the top there is a small example of how to initialize the card and thats based on a console program. Simply post this in the form_load part.


K58056 vellemancard = new K58056();       //create a new object
 vellemancard.Relay_Card = 1;             //set the relaycard number
vellemancard.Do_Action(K58056.Command.On, K58056.Switch.all);   
// Turns everything on 
vellemancard.Do_Action(K58056.Command.Toggle, i);              
 // Toggles swich i;
 vellemancard.Emergency()        // toggle the emergence stop

if you have Questions just hollar, Chryso