nestjs S71200 连接 单个地址读写 多个地址读写 断连重连
时间: 2023-12-06 10:03:29 浏览: 134
要在nestjs中实现与S7-1200通信,可以使用node-snap7库。这个库是Snap7库的Node.js绑定,支持与S7-1200通信。
下面是如何使用node-snap7库在nestjs中实现连接、读取和写入单个地址的值:
1.安装node-snap7库
```
npm install node-snap7
```
2.在nestjs中创建一个service,并引入node-snap7库:
```
import { Injectable } from '@nestjs/common';
import * as snap7 from 'node-snap7';
@Injectable()
export class Snap7Service {
private client: snap7.S7Client;
constructor() {
this.client = new snap7.S7Client();
}
connect(ip: string, rack: number, slot: number) {
return new Promise((resolve, reject) => {
this.client.connectTo(ip, rack, slot, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
readBool(db: number, byte: number, bit: number) {
return new Promise((resolve, reject) => {
this.client.readArea(snap7.S7AreaDB, db, byte, 1, snap7.S7Word, (err, data) => {
if (err) {
reject(err);
} else {
const value = (data[0] & (1 << bit)) !== 0;
resolve(value);
}
});
});
}
writeBool(db: number, byte: number, bit: number, value: boolean) {
return new Promise((resolve, reject) => {
const buffer = Buffer.alloc(2);
buffer.writeInt16BE(value ? (1 << bit) : 0);
this.client.writeArea(snap7.S7AreaDB, db, byte, buffer.length, snap7.S7Word, buffer, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
}
```
3.在nestjs的controller中使用Snap7Service:
```
import { Controller, Get, Param } from '@nestjs/common';
import { Snap7Service } from './snap7.service';
@Controller('snap7')
export class Snap7Controller {
constructor(private readonly snap7Service: Snap7Service) {}
@Get('connect/:ip/:rack/:slot')
connect(@Param('ip') ip: string, @Param('rack') rack: number, @Param('slot') slot: number) {
return this.snap7Service.connect(ip, rack, slot);
}
@Get('readBool/:db/:byte/:bit')
async readBool(@Param('db') db: number, @Param('byte') byte: number, @Param('bit') bit: number) {
const value = await this.snap7Service.readBool(db, byte, bit);
return { value };
}
@Get('writeBool/:db/:byte/:bit/:value')
async writeBool(@Param('db') db: number, @Param('byte') byte: number, @Param('bit') bit: number, @Param('value') value: boolean) {
await this.snap7Service.writeBool(db, byte, bit, value);
return { success: true };
}
}
```
4.在nestjs中使用Snap7Controller来连接、读取和写入单个地址的值:
```
http://localhost:3000/snap7/connect/192.168.1.100/0/1
http://localhost:3000/snap7/readBool/1/0/0
http://localhost:3000/snap7/writeBool/1/0/0/true
```
5.在nestjs中读取多个地址的值,可以使用readMultiVars方法:
```
import { Injectable } from '@nestjs/common';
import * as snap7 from 'node-snap7';
@Injectable()
export class Snap7Service {
private client: snap7.S7Client;
constructor() {
this.client = new snap7.S7Client();
}
connect(ip: string, rack: number, slot: number) {
return new Promise((resolve, reject) => {
this.client.connectTo(ip, rack, slot, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
readMultiVars(vars: { area: number, dbNumber: number, start: number, amount: number, wordLen: number }[]) {
return new Promise((resolve, reject) => {
const items = vars.map((v) => {
return {
Area: v.area,
DBNumber: v.dbNumber,
Start: v.start,
Amount: v.amount,
WordLen: v.wordLen,
};
});
this.client.readMultiVars(items, (err, data) => {
if (err) {
reject(err);
} else {
const values = data.map((d) => {
if (d.WordLen === snap7.S7WLBit) {
return (d.Value[0] & (1 << d.BOffset)) !== 0;
} else {
return d.Value;
}
});
resolve(values);
}
});
});
}
}
```
6.在nestjs中使用Snap7Controller来读取多个地址的值:
```
import { Controller, Get } from '@nestjs/common';
import { Snap7Service } from './snap7.service';
@Controller('snap7')
export class Snap7Controller {
constructor(private readonly snap7Service: Snap7Service) {}
@Get('readMultiVars')
async readMultiVars() {
const vars = [
{ area: snap7.S7AreaDB, dbNumber: 1, start: 0, amount: 1, wordLen: snap7.S7Word },
{ area: snap7.S7AreaDB, dbNumber: 1, start: 2, amount: 2, wordLen: snap7.S7DWord },
{ area: snap7.S7AreaMK, dbNumber: 0, start: 0, amount: 1, wordLen: snap7.S7WLBit },
];
const values = await this.snap7Service.readMultiVars(vars);
return { values };
}
}
```
7.在nestjs中实现断连重连,可以在Snap7Service中添加一个reconnect方法:
```
import { Injectable } from '@nestjs/common';
import * as snap7 from 'node-snap7';
@Injectable()
export class Snap7Service {
private client: snap7.S7Client;
constructor() {
this.client = new snap7.S7Client();
}
connect(ip: string, rack: number, slot: number) {
return new Promise((resolve, reject) => {
this.client.connectTo(ip, rack, slot, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
readBool(db: number, byte: number, bit: number) {
return new Promise((resolve, reject) => {
this.client.readArea(snap7.S7AreaDB, db, byte, 1, snap7.S7Word, (err, data) => {
if (err) {
reject(err);
} else {
const value = (data[0] & (1 << bit)) !== 0;
resolve(value);
}
});
});
}
writeBool(db: number, byte: number, bit: number, value: boolean) {
return new Promise((resolve, reject) => {
const buffer = Buffer.alloc(2);
buffer.writeInt16BE(value ? (1 << bit) : 0);
this.client.writeArea(snap7.S7AreaDB, db, byte, buffer.length, snap7.S7Word, buffer, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
async reconnect() {
await this.client.disconnect();
await this.client.connect();
}
}
```
8.在nestjs中使用Snap7Controller来断连重连:
```
import { Controller, Get } from '@nestjs/common';
import { Snap7Service } from './snap7.service';
@Controller('snap7')
export class Snap7Controller {
constructor(private readonly snap7Service: Snap7Service) {}
@Get('reconnect')
async reconnect() {
await this.snap7Service.reconnect();
return { success: true };
}
}
```
阅读全文