@jan-reitinger Hi Jan, here is the code of the entire block:
#define COM_BAUDRATE 9600 //baudrate, e.g. 9600, 19200, 57600, 115200
#define COM_PARITY_NONE 0 //no parity
#define COM_PARITY_ODD 1 //odd parity
#define COM_PARITY_EVEN 2 //even parity
#define BUFFER_SIZE 20 // MAXIMUM NUMBER OF BYTES TO SEND
#define COMMAND_LENGTH_MAX 20 //maximum length of command
//Assigning parameters to variables, these variables are read-only;
string parameter(0) serialdevice;//serial device is defined by parameter p0 of the REXLANG Block (e.g. /dev/ttyS0 in Linux or COM1 in Windows)
//Assigning inputs to variables
bool input(0) IsGreenButtonSignalOn;
bool input(1) IsRedButtonSignalOn;
bool input(2) IsYellowButtonSignalOn;
bool input(3) IsProcedureStandardSignal;
//Assigning Variables to Outputs, these variables are WRITE-Only
bool output(0) IsDeviceReady;
bool output(1) IsDeviceInError;
bool output(2) IsDeviceRunning;
string output(3) ResponseFromDMX;
long output(14) DebugMessage;
long output(15) ConnectionStatus;
//Variables Declaration
const long cr = 13; // (cr) is the termination character, carrige return (0Dh);
long hCom, s, receive; // Communication Handle
long buffer[BUFFER_SIZE]; // buffer for incoming data
long dataCount; // Number of bytes sent
bool read = true;
string data[BUFFER_SIZE+1];
int status=0;
long a;
void HomeMotor(void){
long readData[6];
readData[0]='L';
readData[1]='H';
readData[2]='O';
readData[3]='M';
readData[4]='E';
readData[5]='-';
s=Send(hCom,readData,6);
return;
}
void ClearError(void){
a=Write(hCom,"@DMK01EO=1[CR]");
if(a>0){
DebugMessage=a;
}
else{
DebugMessage=-1;
}
}
void MoveTo1000(void){
a=Write(hCom,"@01J+");
if(a>0){
DebugMessage=a;
status=0;
}
else{
DebugMessage=-1;
}
}
//the init procedure is executed once when the REXLANG function block initializes
long init(void)
{
/* PUT YOUR CODE HERE */
hCom=-1;
return 0;
}
//the main procedure is executed repeatedly (once in each sampling period)
long main(void)
{
/* PUT YOUR CODE HERE */
if(hCom<0)
{
hCom = OpenCom(serialdevice,COM_BAUDRATE,COM_PARITY_NONE); //Opening Serial Device
ConnectionStatus=hCom;
}
switch(status)
{
case 0:
a=Write(hCom,"@00EO[CR]");
if(a>0){
status=1;
}
break;
case 1:
dataCount = Read(hCom, data,BUFFER_SIZE);
if(dataCount>0){
if(data=="1"){
ResponseFromDMX="ON";
}
else{
ResponseFromDMX="OFF";
}
status = 0;
}
default:
break;
}
return 0;
}
//the exit procedure is executed once when the task is correctly terminated
// (system shutdown, downloading new control algorithm, etc.)
long exit(void)
{
/* PUT YOUR CODE HERE */
if(hCom>=0){
Close(hCom);
}
return 0;
}