nestjs node-snap7 读写S7 uint数组 与 bool数组
时间: 2023-12-20 16:07:04 浏览: 18
fetch-blob:Node.js 中的 Blob 实现,最初来自 node-fetch
好的,下面是nestjs和node-snap7的示例代码,用于读写S7 uint数组和bool数组。
首先,安装node-snap7和nestjs:
```
npm install node-snap7
npm install @nestjs/core @nestjs/common @nestjs/platform-express
```
然后在nestjs中引入node-snap7模块,实现读写S7数据的功能。以下代码演示了如何读取S7 uint数组和bool数组:
```typescript
import { Injectable } from '@nestjs/common';
import * as snap7 from 'node-snap7';
@Injectable()
export class S7Service {
private s7client = new snap7.S7Client();
constructor() {
this.s7client.connect('192.168.1.100', 0, 1, err => {
if (err) {
console.error('S7 connection failed:', err);
} else {
console.log('S7 connection successful');
}
});
}
async readUIntArray(db: number, start: number, length: number): Promise<number[]> {
return new Promise((resolve, reject) => {
const buffer = Buffer.alloc(length * 2);
this.s7client.DBRead(db, start, length * 2, snap7.S7WL.Word, (err, res) => {
if (err) {
reject(err);
} else {
for (let i = 0; i < length; i++) {
const value = res.readUInt16BE(i * 2);
buffer.writeUInt16BE(value, i * 2);
}
const array = Array.from(new Uint16Array(buffer.buffer));
resolve(array);
}
});
});
}
async readBoolArray(db: number, start: number, length: number): Promise<boolean[]> {
return new Promise((resolve, reject) => {
const buffer = Buffer.alloc(length);
this.s7client.DBRead(db, start, length, snap7.S7WL.Bit, (err, res) => {
if (err) {
reject(err);
} else {
buffer.fill(0);
res.copy(buffer);
const array = Array.from(new Uint8Array(buffer.buffer));
resolve(array.map(value => value === 1));
}
});
});
}
async writeUIntArray(db: number, start: number, array: number[]): Promise<void> {
return new Promise((resolve, reject) => {
const buffer = Buffer.alloc(array.length * 2);
const uintArray = new Uint16Array(array);
for (let i = 0; i < array.length; i++) {
buffer.writeUInt16BE(uintArray[i], i * 2);
}
this.s7client.DBWrite(db, start, buffer.length, buffer, snap7.S7WL.Word, (err, res) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
async writeBoolArray(db: number, start: number, array: boolean[]): Promise<void> {
return new Promise((resolve, reject) => {
const buffer = Buffer.alloc(array.length);
const uintArray = new Uint8Array(array.map(value => value ? 1 : 0));
buffer.fill(0);
uintArray.forEach((value, index) => {
buffer.writeUInt8(value, index);
});
this.s7client.DBWrite(db, start, buffer.length, buffer, snap7.S7WL.Bit, (err, res) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
}
```
在上面的代码中,我们在S7Service类中定义了四个异步方法:readUIntArray、readBoolArray、writeUIntArray和writeBoolArray。这些方法分别用于读取和写入S7 uint数组和bool数组。
readUIntArray和readBoolArray方法的实现非常相似。它们都接收三个参数:db(数据块)、start(起始地址)和length(数组长度)。这些方法通过node-snap7模块的DBRead方法从S7 PLC读取数据,并将读取的数据转换为JavaScript数组。readUIntArray方法将读取的数据转换为Uint16Array,然后转换为JavaScript数组。readBoolArray方法将读取的数据转换为Uint8Array,然后转换为JavaScript数组,并将数组的值转换为布尔值。
writeUIntArray和writeBoolArray方法的实现也非常相似。它们都接收三个参数:db(数据块)、start(起始地址)和array(要写入的数组)。这些方法将JavaScript数组转换为Buffer,并通过node-snap7模块的DBWrite方法将数据写入S7 PLC。
最后,我们需要在nestjs的控制器中调用S7Service的方法,以实现读写S7 uint数组和bool数组的功能。以下是一个简单的nestjs控制器示例:
```typescript
import { Controller, Get } from '@nestjs/common';
import { S7Service } from './s7.service';
@Controller()
export class AppController {
constructor(private readonly s7Service: S7Service) {}
@Get('uint')
async readUIntArray(): Promise<number[]> {
return this.s7Service.readUIntArray(1, 0, 10);
}
@Get('bool')
async readBoolArray(): Promise<boolean[]> {
return this.s7Service.readBoolArray(1, 0, 10);
}
@Get('write-uint')
async writeUIntArray(): Promise<void> {
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
return this.s7Service.writeUIntArray(1, 0, array);
}
@Get('write-bool')
async writeBoolArray(): Promise<void> {
const array = [true, false, true, false, true, false, true, false, true, false];
return this.s7Service.writeBoolArray(1, 0, array);
}
}
```
在上面的代码中,我们定义了四个路由,分别对应于读取S7 uint数组、读取S7 bool数组、写入S7 uint数组和写入S7 bool数组。这些路由通过调用S7Service的方法来实现读写S7数据的功能。
阅读全文