-
Notifications
You must be signed in to change notification settings - Fork 1
/
mewtocol-serial.js
142 lines (128 loc) · 3.99 KB
/
mewtocol-serial.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//This lib could treat as node version serialport assistance.
//Form panasonic mewtocol plc communication.
//In many cases, the plc connection is serialport.
//You could download this file and simple add your code at the end of program.
const { SerialPort } = require('serialport');
//Default serialport configuration
const defaultport = "COM2";
const defaultBaudRate = 9600;
const port = new SerialPort({ path: defaultport, baudRate:defaultBaudRate, autoOpen: false });
/*
port.open(function (err) {
if (err) {
console.log('Port open fail: ' + err.message + "\n");
return;
}
console.log(defaultport + ' open sucesses.' + "\r");
});
port.on('error', err => {
console.log('There is an error: ' + err.message + "\n");
});
//ReadMessage
port.on('data', data => {
console.log('Data received: ' + data + "\n");
});
*/
//Listing system avaliable ports.
function listports() {
const ports = SerialPort.list();
console.log(ports.map(port => port.path + ":" + port.friendlyName).join("\n"));
}
//Check PLC Version
function PlcVer(){
try {
port.write("%EE#RT**"+ "\r");
console.log("Get ver msg has sent." + "\n")
}catch (err) {
console.log('Message send fail: ' + err.message+'\n');
}
}
//Read single contact data
//Please note that the format of the contact num is "X0001", "R0010"
//The relay number only accept X,Y,R,T
//Example ReadOne('X0001')
function ReadOne(ContactNum){
try {
port.write("%EE#RCS"+ContactNum+"**\r");
console.log("Read one relay msg has sent." + "\n")
}catch (err) {
console.log('Message send fail: ' + err.message+'\n');
}
}
//Write single contact data
//Please note that the format of the contact num is "Y0001", "R0010"
//The relay number only accept Y,R,T,C
//Example RelayON('Y0001')
function RelayON(ContactNum) {
try {
port.write("%EE#WCS"+ContactNum+"1**\r");
console.log("Single Relay on msg has sent." + "\n")
}catch (err) {
console.log('Message send fail: ' + err.message+'\n');
}
}
//Write single contact data
//Please note that the format of the contact num is "Y0001", "R0010"
//The relay number only accept Y,R,T,C
//Example RelayOff('Y0001')
function RelayOff(ContactNum){
try {
port.write("%EE#WCS"+ContactNum+"1**\r");
console.log("Single Relay off msg has sent." + "\n")
}catch (err) {
console.log('Message send fail: ' + err.message+'\n');
}
}
//Read multiple contacts, read a range of the contact once
//Example ReadMulti('X0001','X0010')
function ReadMulti(Start,End){
try {
port.write("%EE#RCP"+Start.toString()+End.toString()+"**\r");
console.log("Read Multi has sent." + "\n")
}catch (err) {
console.log('Message send fail: ' + err.message+'\n');
}
}
//Write multiple contact, write a range of contact once
function WriteMultiON(Start,End){
try {
port.write("%EE#WCP"+Start.toString()+End.toString()+"1**\r");
console.log("Write Multi ON msg has sent." + "\n")
}catch (err) {
console.log('Message send fail: ' + err.message+'\n');
}
}
//Write multiple contact, off command
function WriteMultiOFF(Start,End){
try {
port.write("%EE#WCP"+Start.toString()+End.toString()+"0**\r");
console.log("Write Multi OFF msg has sent." + "\n")
}catch (err) {
console.log('Message send fail: ' + err.message+'\n');
}
}
//Read register value
//Attention the register only available DT
//Example ReadDT(0001, 00100)
function ReadDT(Start,End){
try {
port.write("%EE#RDD"+Start.toString()+End.toString()+"**\r");
console.log("Write Multi OFF msg has sent." + "\n")
}catch (err) {
console.log('Message send fail: ' + err.message+'\n');
}
}
//Export functions, if you would like to using outside function.
module.exports = {
PlcVer,
ReadOne,
RelayON,
RelayOff,
ReadMulti,
WriteMultiON,
WriteMultiOFF,
ReadDT,
listports
}
//If you would like to use directly, using following examples.
//ReadDT('0001','0010')