diff --git a/services/serialPortService.js b/services/serialPortService.js index 8a61d89..a8cbc0e 100644 --- a/services/serialPortService.js +++ b/services/serialPortService.js @@ -1,53 +1,53 @@ -import { SerialPort } from 'serialport' -import { autoDetect } from '@serialport/bindings-cpp' +import { SerialPort } from "serialport"; +import { autoDetect } from "@serialport/bindings-cpp"; + +const path = "/dev/ttyUSB0"; +const serialPort_ = new SerialPort( + { path, baudRate: 9600, autoOpen: true }, + function (err) { + if (err) { + return console.log("Error: ", err.message); + } + } +); + +console.log(serialPort_); +serialPort_.on("data", (data) => { + console.log( + "Received data:", + data, + data.toString(), + data.toString("hex"), + parseInt("0x" + data.toString("hex").slice(2, -2)) + ); + // Process the response to check if the box was opened successfully +}); + +serialPort_.on("error", (error) => { + console.error("Error:", error); +}); async function serialPortList(req, res) { - - SerialPort.list() - .then(async pathsList => { - for (const pathObject of pathsList) { - await new Promise(resolve => { - setTimeout(() => resolve(), 100); - }) - - const serialPort = new SerialPort({ path: pathObject.path, baudRate: 9600, autoOpen: false, }, function (err) { - if (err) { - return console.log('Error: ', err.message); - } - }) - - // console.log(111111, serialPort) - - const buff = Buffer.allocUnsafe(4); - buff.write(`#0${req.params.id}$`); - - serialPort.on('open', () => { - console.log('Serial port opened'); - serialPort.write(buff) - }); - - serialPort.on('data', (data) => { - console.log('Received data:', data); - // Process the response to check if the box was opened successfully - }); - - serialPort.on('error', (error) => { - console.error('Error:', error); - }); - } - }) - .catch((error) => { - console.error('Error listing serial ports:', error); - }); + const buff = Buffer.from("24"+(+req.params.id).toString(16).padStart(4,'0')+"26", "hex"); + console.log(buff,parseInt("0x" + buff.toString("hex").slice(2, -2))); + + serialPort_.write(buff, function (err, result) { + if (err) { + console.log("Error while sending message : " + err); + } + if (result) { + console.log("Response received after sending message : " + result); + } + }); + res.send("send:" + parseInt("0x" + buff.toString("hex").slice(2, -2))); } - async function serialPort(req, res) { const buff = Buffer.allocUnsafe(4); buff.writeUInt8(`#0${req.params.id}$`); - const result = await serialport.write(buff) + const result = await serialport.write(buff); - res.send(result) + res.send(result); // serialport.open(function (err) { // if (err) { @@ -71,4 +71,4 @@ async function serialPort(req, res) { // }) } -export default serialPortList \ No newline at end of file +export default serialPortList; diff --git a/services/serialPortService.jsy b/services/serialPortService.jsy new file mode 100644 index 0000000..27cd1cb --- /dev/null +++ b/services/serialPortService.jsy @@ -0,0 +1,77 @@ +import { SerialPort } from 'serialport' +import { autoDetect } from '@serialport/bindings-cpp' + +async function serialPortList(req, res) { + console.log(1) + + SerialPort.list() + .then(async pathsList => { + for (const pathObject of pathsList) { + await new Promise(resolve => { + setTimeout(() => resolve(), 100); + }) + + const serialPort = new SerialPort({ path: pathObject.path, baudRate: 9600, autoOpen: false, }, function (err) { + if (err) { + return console.log('Error: ', err.message); + } + }) + + // console.log(111111, serialPort) + + const buff = Buffer.allocUnsafe(4); + //buff.write(`#0${req.params.id}$`); + + buff.write("$"+req.params.id+"&"); + + serialPort.on('open', () => { + console.log('Serial port opened'); + serialPort.write(buff) + }); + + serialPort.on('data', (data) => { + console.log('Received data:', data); + // Process the response to check if the box was opened successfully + }); + + serialPort.on('error', (error) => { + console.error('Error:', error); + }); + } + }) + .catch((error) => { + console.error('Error listing serial ports:', error); + }); +} + +async function serialPort(req, res) { + const buff = Buffer.allocUnsafe(4); + buff.writeUInt8(`#0${req.params.id}$`); + + const result = await serialport.write(buff) + + res.send(result) + + // serialport.open(function (err) { + // if (err) { + // return console.log('Error opening port: ', err.message); + // } + + // // Because there's no callback to write, write errors will be emitted on the port: + // serialport.write('main screen turn on'); + // }) + + // serialport.on('error', function (err) { + // console.log('Error: ', err.message); + // }) + + // serialport.on('readable', function () { + // console.log('Data:', serialport.read()); + // }) + + // serialport.on('data', function (data) { + // console.log('Data:', data); + // }) +} + +export default serialPortList diff --git a/services/wget-log b/services/wget-log new file mode 100644 index 0000000..08d3beb --- /dev/null +++ b/services/wget-log @@ -0,0 +1,101 @@ +--2024-11-07 14:26:48-- http://localhost:3000/open/101 +Resolving localhost (localhost)... 127.0.0.1 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... Read error (Connection timed out) in headers. +Retrying. + +--2024-11-07 14:41:49-- (try: 2) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... Read error (Connection timed out) in headers. +Retrying. + +--2024-11-07 14:56:51-- (try: 3) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 14:58:22-- (try: 4) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 14:58:26-- (try: 5) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 14:58:31-- (try: 6) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 14:58:37-- (try: 7) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 14:58:44-- (try: 8) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 14:58:52-- (try: 9) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 14:59:01-- (try:10) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 14:59:11-- (try:11) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 14:59:21-- (try:12) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 14:59:31-- (try:13) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 14:59:41-- (try:14) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 14:59:52-- (try:15) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:00:02-- (try:16) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:00:12-- (try:17) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:00:22-- (try:18) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:00:32-- (try:19) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:00:42-- (try:20) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Giving up. + diff --git a/wget-log b/wget-log new file mode 100644 index 0000000..4a93244 --- /dev/null +++ b/wget-log @@ -0,0 +1,101 @@ +--2024-11-07 15:41:49-- http://localhost:3000/open/101 +Resolving localhost (localhost)... 127.0.0.1 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:46:53-- (try: 2) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:46:55-- (try: 3) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:46:58-- (try: 4) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:47:02-- (try: 5) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:47:08-- (try: 6) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:47:15-- (try: 7) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:47:22-- (try: 8) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:47:31-- (try: 9) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:47:41-- (try:10) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:47:52-- (try:11) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:48:02-- (try:12) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:48:13-- (try:13) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:48:23-- (try:14) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:48:34-- (try:15) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:48:45-- (try:16) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:48:56-- (try:17) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:49:06-- (try:18) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:49:17-- (try:19) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Retrying. + +--2024-11-07 15:49:27-- (try:20) http://localhost:3000/open/101 +Connecting to localhost (localhost)|127.0.0.1|:3000... connected. +HTTP request sent, awaiting response... No data received. +Giving up. +