Cnc events are events emitted by cnc to notify relevant changes in cnc kernel (real or simulated) in real time.
These events are handled by Cnc which implements EventEmitter and can be listened calling to on function of this class, like this
const handler = (event) =>
{
console.log('Cnc state has changed to ', event.data.state)
};
cnc.on('CncStateEvent', handler)
To avoid memory leaks, it is recommended to remove unused listeners.
cnc.removeListener('CncStateEvent', handler);
//or removing all listerner at once
cnc.removeAllListeners()
Cnc event data format is exported like a typescript interface by libfagorclient. All cnc events fit this schema and fill with meaningful information data field. This field is also typed for every cnc event type and can be found within interfaces section of this manual.
Below is a list showing the details of each cnc event. Unless otherwise indicates, all events are usable for both kernel types, real and simulated:
This event is generated when the kernel state changes. data field is typed by CncStateEventData interface:
/*
data: {
channel: 1,
cncKernelType: 0,
state: 'ERROR'
},
name: "CncStateEvent",
orderStamp: "1051",
timeStamp: "12881781",
type: "CNC_EVENT
*/
cnc.on('CncStateEvent', (event) => {
if(event.data.state === 'ERROR'){
console.error('Cnc is in error');
}
})
Possible values for state are:
This event is generated when the kernel operation mode changes. Cnc kernel has two operation modes, block and program mode. By default, cnc boots in program mode but this mode can be changed using enterBlockMode method of Cnc class or leaveBlockMode method of Cnc class data field is typed by CncModeEventData interface:
cnc.getChannel(1).cncServices.enterBlockMode();
//if the request has been susccesful, cnc object will emit CncModeEvent event with 'BLK_SELEC' mode.
cnc.on('CncModeEvent', (event) => {
console.log()
})
/*
data: {
channel: 1,
cncKernelType: 0,
mode: "BLK_SELEC"
},
name: "CncModeEvent",
orderStamp: "1055",
timeStamp: "13591843",
type: "CNC_EVENT"
*/
cnc.getChannel(1).cncServices.leaveBlockMode();
/*
data: {
channel: 1,
cncKernelType: 0,
mode: "PRG_SELEC"
},
name: "CncModeEvent",
orderStamp: "1056",
timeStamp: "13597843",
type: "CNC_EVENT"
*/
This event is generated when the kernel raises an error or warning. data field is typed by CncErrorMessageEventData interface:
cnc.getChannel(1).cncServices.enterBlockMode();
cnc.on('CncModeEvent', (event) => {
if(event.data.mode === 'BLK_SELEC'){
cnc.getChannel(1).cncServices.setBlock('#ERROR[1]');
}
});
cnc.on('CncErrorEvent', (event) => {
console.log('error', event);
})
//we press start and block is executed, so the cnc raises an error and CncErrorEvent is generated.
/*
data: {
cncKernelType: 0,
showTextId: "1",
value: [{
id: 1
text: "1"
type: 5
}]
},
name: "CncErrorEvent",
orderStamp: "1061",
timeStamp: "14612828",
type: "CNC_EVENT"
}*/
The data field contains a description of the error, and using this information we can retrieve the text of the error calling to getErrorInfo method from General
cnc.on('CncErrorEvent', (event) => {
console.log('error', event);
//if we receive more than one error, we can iterate all of them to get whole metadata of error
cnc.general.getErrorInfo(0,0,'english').then(error => {
console.log('idx =', 0, error)});
/*
{
group: "errors-messages",
name: "getErrorInfo",
return: {
data:
errorData: [
{dimension: "", meaning: "SYSTEM_TIME", value: "17226000000"},
{dimension: "", meaning: "ERROR_TYPE", value: "ERROR"},
{dimension: "", meaning: "ERROR", value: "1"},
{dimension: "", meaning: "CHANNEL", value: "1"},
{dimension: "", meaning: "RESET_WITH_ESC", value: "1"},
{dimension: "", meaning: "HELP_FILE", value: "errlist.htm#ERR0001"},
{dimension: "", meaning: "TEXT", value: "↵SYSTEM ERROR."},
{dimension: "", meaning: "FILE_NAME", value: "--"},
{dimension: "", meaning: "SOURCE", value: "123-17153"}],
returnCode: 0
}
}
}
*/
});
In the response of the service (typed by ServiceResponse), errorData contains all meaningful information about the error.
Notice, that some errors can be resetted or skipped, for further information check cncServices.
This event is generated when during the execution of a program or block the MSG instruction has been executed. data field is typed by CncErrorMessageEventData interface, but in this case it is not necessary to retrieve any extra information.
cnc.getChannel(1).cncServices.enterBlockMode();
cnc.on('CncModeEvent', (event) => {
if(event.data.mode === 'BLK_SELEC'){
//We program a MSG instruction with "CUSTOM MESSAGE" text.
cnc.getChannel(1).cncServices.setBlock('#MSG["CUSTOM ERROR"]');
cnc.on('CncMessageEvent', (event) => {
console.log('error', event);
})
//After start key it is pressed, CncMessageEvent arrives with
/*
{
data: {
cncKernelType: 0,
showTextId: "1",
value: [{id: 1, text: "CUSTOM MESSAGE", type: 0},
{id: 2, text: "", type: 0},
{id: 3, text: "", type: 0},
{id: 4, text: "", type: 0}]
},
name: "CncMessageEvent",
orderStamp: "37",
timeStamp: "19123734",
type: "CNC_EVENT"
}
*/
}
});
In this case value fields contains an array with message in use for each channel of cnc (maximum 4).
(Only available in real kernel mode )
This event is generated when during the execution cycle of PLC a message has been programmed. data field is typed by CncErrorMessageEventData interface, but in this case it is not necessary to retrieve any extra information.
cnc.on('PlcMessageEvent', (event) => {
console.log('plc message', event);
});
/*
{
data: {
cncKernelType: 0,
showTextId: "4294967295",
value: [
{id: 256, text: "PLC MESSAGE 256", type: 0}
}
},
name: "PlcMessageEvent"
orderStamp: "3589"
timeStamp: "21865468"
type: "CNC_EVENT"
}
*/
In this case value fields contains an array with all active messages activated by PLC program.
(Only available in real kernel mode )
This event is generated when the layout of simulated or physical jog keyboard has changed. data field is typed by CncJogKeyEventData interface.
For example, if we press stop key we will receive an events with the rising edge.
cnc.on('CncJogKeyEvent', (event) => {
console.log(event);
/*
{
data: {
cncKernelType: 0,
key: "STOP",
value: 1,
},
name: "CncJogKeyEvent",
orderStamp: "1817",
timeStamp: "20326781",
type: "CNC_EVENT"
}
*/
});
This event is very useful to handle block execution in block mode when the block has to be set after START has been pressed. If no block is set, cnc kernel waits until a block is set. On the contrary, we can listen to START press event to set the block. In this case, the block will be executed immediately after it is programmed.
cnc.getChannel(1).cncServices.enterBlockMode();
cnc.on('CncModeEvent', (event) => {
if(event.data.mode === 'BLK_SELEC'){
cnc.on('CncJogKeyEvent', (event) => {
if(event.data.key === 'START'){
const ax = cnc.getChannel(1).axes[0];
const block = `G0${ax.name}${ax.pos.external + 100}`
cnc.getChannel(1).cncServices.setBlock(block);
}
}
}
});