-
Notifications
You must be signed in to change notification settings - Fork 0
/
relais.cpp
86 lines (73 loc) · 3.07 KB
/
relais.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "relais.h"
#define BAUDRATE B2400
#ifndef K8056_DEVICE
#define K8056_DEVICE "/dev/ttyS0"
#endif
struct termios oldtio, newtio ;
int k8056_port ;
char k8056_device[] = K8056_DEVICE; /* 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 = 100 ; /* Increase value with a long serial cable or in wireless operation. */
char cmd[6] ;
/*------------------------------------------------------------------------------*/
/* 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) ;
}
}
/*--------------------------------------------------------------------------------*/
/* openPort �ffnet und schlie�t Port port */
/*--------------------------------------------------------------------------------*/
void openPort(int port, bool auf) {
/* Open serial port */
if (port > 0 && port < 10) {
initserie() ;
cmd[0] = 13 ; /* 13 */
cmd[1] = k8056_addr ; /* Adresse */
cmd[2] = auf?'S':'C'; /* Instruction */
cmd[3] = (char) (port+48); /* # relais, address or status */
cmd[4] = checksum(cmd) ; /* Checksum */
//std::cout << cmd[2] << cmd[3] << std::endl;
SendCommand(cmd) ;
/* Close serial port */
tcsetattr(k8056_port, TCSANOW, &oldtio); /* Backup old port settings */
close(k8056_port);
} else {
std::cerr << "Port " << port << " gibts nicht!" << std::endl;
}
usleep(1000000);
}