first commit

main
kavehjamshidi 2 months ago
commit f74d79389d
  1. 1
      .env
  2. 1
      .gitignore
  3. 8
      controller/serialPort.js
  4. 15
      index.js
  5. 2465
      package-lock.json
  6. 20
      package.json
  7. 39
      services/serialPortService.js

@ -0,0 +1 @@
PORT = 3000

1
.gitignore vendored

@ -0,0 +1 @@
node_modules/

@ -0,0 +1,8 @@
import { Router } from 'express';
import serialPort from '../services/serialPortService.js';
const router = new Router()
router.get('/:id', serialPort);
export default router

@ -0,0 +1,15 @@
import express from 'express';
import { configDotenv } from "dotenv";
import router from './controller/serialPort.js';
const app = new express();
configDotenv();
// app.use(express.urlencoded({ extended: false }))
app.use(router)
async function run() {
app.listen(process.env.PORT);
console.log(`This app listens to http://localhost:${process.env.PORT}`)
}
await run()

2465
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -0,0 +1,20 @@
{
"name": "roshano_boxmodule",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "pm2 start index.js --watch"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^16.4.5",
"express": "^4.20.0",
"pm2": "^5.4.2",
"serialport": "^12.0.0"
}
}

@ -0,0 +1,39 @@
import { SerialPort } from 'serialport'
const serialport = new SerialPort({ path: 'COM3', baudRate: 9600, autoOpen: false, }, function (err) {
if (err) {
return console.log('Error: ', err.message);
}
})
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 serialPort
Loading…
Cancel
Save