-
Notifications
You must be signed in to change notification settings - Fork 0
/
fpgaTest.js
70 lines (64 loc) · 2.29 KB
/
fpgaTest.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
const SerialPort = require('serialport')
const { asciiToTrytes } = require('@iota/converter')
const Mam = require('@iftt/mam')
const port = new SerialPort('/dev/ttyUSB1', {
baudRate: 115200
})
port.on('open', () => {
console.log('port is open!')
// Because there's no callback to write, write errors will be emitted on the port:
let mamState = Mam.init({
provider: 'https://nodes.devnet.iota.org',
// provider: 'https://nodes.thetangle.org:443',
attachToTangle: function (trunkTransaction, branchTransaction, minWeightMagnitude, trytes, callback) {
return new Promise((resolve, reject) => {
let bytestream = Buffer.from(JSON.stringify({
command: 'attachToTangle',
trunkTransaction,
branchTransaction,
minWeightMagnitude,
timestamp: new Date().getTime(),
trytes
}) + '\n')
let returnedTrytes = Buffer.from('')
// let totalSize = 0;
port.on('data', (data) => {
// console.log('data.length', data.length);
// totalSize += data.length;
// console.log('data[data.length - 1]', data[data.length - 1]);
returnedTrytes = Buffer.concat([returnedTrytes, data])
if (data[data.length - 1] === 10) {
let response = JSON.parse(returnedTrytes.toString('utf8'))
console.log(response)
resolve(response.trytes)
}
})
port.write(bytestream)
})
}
})
mamState = Mam.changeMode(mamState, 'public')
const publish = async data => {
console.log('PUBLISH!')
// Convert the JSON to trytes and create a MAM message
const trytes = asciiToTrytes(data)
const message = Mam.create(mamState, trytes)
// Update the MAM state to the state of this latest message
// We need this so the next publish action knows it's state
mamState = message.state
// Attach the message
await Mam.attach(message.payload, message.address, 3, 9, 'CRAIG9FPGA')
.then((data) => {
console.log('data', data)
})
.catch(err => {
console.log('err', err)
})
console.log('Sent message to the Tangle!')
console.log('Address: ' + message.root)
}
publish('This is an ICC FPGA TEST')
setTimeout(() => {
publish('This is another ICC FPGA TEST')
}, 15000)
})