arduino + K8056

The original code is the following !

/* Test Velleman K8056 card on RS232 port. /
/
Usage: k8056 [-h] [-v] [-a card_address] [-d device] [-S|-C|-T relay#] [-B relay_status] [-A new_address] [-E|-D|-F] */

/* See README file for details. /
/
V.20051007 /
/
V.20060928 Updated by Mårten Persson (sm7syx) useful 'usage' added ! */

/***********************************************************************
Copyright (C) 2005 Daniel CLEMENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
***********************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
#include <math.h>
#include <sys/fcntl.h>

#define BAUDRATE B2400

struct termios oldtio, newtio ;
int k8056_port ;
char k8056_device[16] = "/dev/k8056" ; /* This is the default device, can be reset on command line. /
unsigned char k8056_instruction = 'E';
unsigned char k8056_addr = 1 ;
unsigned char k8056_relay_address = '0' ;
int k8056_trame_number = 4 ; /
Increase value with a long serial cable or in wireless operation. */
int verbose = 0 ;
char cmd[6] ;

/------------------------------------------------------------------------------/
/* Print some helpful information in lack of man page (added by: sm7syx) /
/
------------------------------------------------------------------------------*/
void usage (void) {
printf("\nk8056 Usage: k8056 [-h] [-v] [-a card_address] [-d device] [-S|-C|-T relay#] [-B relay_status] [-A new_address] [-E|-D|-F]\n");
printf(" For more detaild help use: k8056 -h\n\n");
}

void help (void) {
printf("\nk8056 is a program for controlling the 'Velleman K8056' RS-232 I/O card\n\n");
printf("Usage: k8056 [-h] [-v] [-a card_address] [-d device] [-S|-C|-T relay#] [-B relay_status] [-A new_address] [-E|-D|-F]\n\n");
printf(" -h : this help\n");
printf(" -v : verbose\n");
printf(" -d device : Rs232 device, default /dev/k8056 (To do: ln -s /dev/ttyS0 /dev/k8056 for example)\n");
printf(" -a card_address : Address of the card (1..255) in decimal\n");
printf(" -S|-C|-T relay# : Instructions to set/clear/toggle a relay number ('1'..'9')\n");
printf(" -A new_address : Set the new address of the card (1..255)\n");
printf(" -B relay_status : Set the eight relay with relay_status byte (1..255)\n");
printf(" -E|-D|-F : Emergency stop / Display address / Force address to 1\n\n");
printf(" ** Command examples **\n");
printf(" k8056 -v -E : Emergency stop all relays cards (with verbose).\n");
printf(" k8056 -v -S6 : Set relay #6 (with verbose).\n");
printf(" k8056 -v -C5 : Clear relay #5 (with verbose).\n");
printf(" k8056 -v -T5 : Toggle relay#5 => set relay #5 (with verbose).\n");
printf(" k8056 -B 127 : Clear relay #1, Set relay #2..8\n");
printf(" k8056 -a 2 -d /dev/ttyS14 -S1 : Set relay #1 on card address 2 and device /dev/ttyS14.\n\n");
}

/------------------------------------------------------------------------------/
/* Open serial port /
/
------------------------------------------------------------------------------/
void initserie (void) {
k8056_port = open(k8056_device, O_RDWR | O_NOCTTY );
if (k8056_port < 0)
{
fprintf(stderr, "Error opening device %s !\n", k8056_device) ;
exit(-1);
}
tcgetattr(k8056_port,&oldtio); /
save current port settings */

newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
newtio.c_oflag = 0;
tcflush(k8056_port, TCOFLUSH); /* Flushes written but not transmitted. */
tcsetattr(k8056_port, TCSANOW, &newtio);
}

/---------------------------------------------------------------------------------/
/* Checksum Checkum (2 complement of the sum of the 4 previous byte + 1) /
/
---------------------------------------------------------------------------------/
unsigned char checksum(char cmd[]) {
unsigned char checksum ;
/
Ex. VB: checksum = ( 255 - ( ( ( (a+b+c+d / 256 ) - Int( (13 + cmd[1] + cmd[2] + cmd[3]) / 256 ) ) * 256 ) ) + 1 ; /
/
( 255 - ((a+b+c+d) modulo 256) ) + 1 Calcul de checkum (complement à 2 de la somme de 4 octets + 1). */
checksum = ~((13 + cmd[1] + cmd[2] + cmd[3]) & 0xFF) + 1 ;
return checksum ;
}

/--------------------------------------------------------------------------------/
/* SendCommand Send command to k8056 card /
/
--------------------------------------------------------------------------------/
void SendCommand(char cmd[]) {
int i ;
tcflush(k8056_port, TCOFLUSH) ; /
Flushes written but not transmitted. */
for (i=0 ; i < k8056_trame_number ; i++)
{
write(k8056_port, cmd, 5) ;
usleep(5000) ;
}
}

//---------------------------------------------------------------
// Parse the command line to find additionnal arguments
//---------------------------------------------------------------
char parse_cmdline(int argc,char *argv[]) {
int helpflg=0;
int errflg=0;
int c;

while ((c = getopt(argc, argv, "S:C:T:A:B:a:d:EDFhv")) != EOF)
{
switch (c)
{
case 'S':
case 'C':
case 'T':
k8056_instruction = c ;
k8056_relay_address = optarg[0] ;
break;
case 'A':
case 'B':
k8056_instruction = c ;
k8056_relay_address = atoi(optarg);
break;
case 'E':
case 'D':
case 'F':
k8056_instruction = c ; /* The 3rd byte = 0 (k8056_relay_address) /
break;
case 'a':
k8056_addr = atoi(optarg);
break;
case 'd':
strcpy(k8056_device, optarg) ;
break;
case 'v':
verbose = 1;
break;
case 'h':
helpflg++;
break;
case '?':
errflg++;
break;
}
if (helpflg)
{
help();
exit (1);
}
}
if (optind < 2 || errflg) /
if optind = 1, option missing. */
{
usage();
exit (1);
}
return 1;
}

/------------------------------------------------------------------------------/
/* MAIN /
/
------------------------------------------------------------------------------*/
int main(int argc, char *argv[]) {
parse_cmdline(argc,argv) ;

/* Open serial port */
initserie() ;

if (verbose) printf("Send command on k8056 card:\n Device: %s, Card address: %d, Instruction: %c, Relay #%c\n"
, k8056_device, k8056_addr, k8056_instruction, k8056_relay_address) ;

cmd[0] = 13 ; /* 13 /
cmd[1] = k8056_addr ; /
Adresse /
cmd[2] = k8056_instruction ; /
Instruction /
cmd[3] = k8056_relay_address ; /
# relais, address or status /
cmd[4] = checksum(cmd) ; /
Checksum */

if (verbose) printf(" Checksum = %d\n", checksum(cmd)) ;
SendCommand(cmd) ;

/* Close serial port /
tcsetattr(k8056_port, TCSANOW, &oldtio); /
Backup old port settings */
close(k8056_port);

usleep(300000) ; /* Pause between two command. */
if (verbose) printf("Done\n");

/* Quitt... */
exit(0);
}

I added delay (5); to replace usleep(5000) ;
But no much better :cry: