Untitled

 avatar
unknown
plain_text
a year ago
1.8 kB
6
Indexable
const SerialPort = require('serialport');
const Readline = require('@serialport/parser-readline');
const port = new SerialPort('COMx', { baudRate: 9600 }); // Replace 'COMx' with the actual COM port assigned to your phone
const parser = port.pipe(new Readline({ delimiter: '\r\n' }));

// Function to send SMS
function sendSMS(number, message) {
    port.write('AT\r\n', (err) => {
        if (err) {
            return console.log('Error writing: ', err.message);
        }
        console.log('AT command sent');
    });

    setTimeout(() => {
        port.write('AT+CMGF=1\r\n', (err) => {
            if (err) {
                return console.log('Error writing: ', err.message);
            }
            console.log('Set SMS mode to text');
        });
    }, 1000);

    setTimeout(() => {
        port.write(`AT+CMGS="${number}"\r\n`, (err) => {
            if (err) {
                return console.log('Error writing: ', err.message);
            }
            console.log('Recipient number set');
        });
    }, 2000);

    setTimeout(() => {
        port.write(`${message}\x1A`, (err) => { // \x1A is ASCII code for CTRL+Z
            if (err) {
                return console.log('Error writing: ', err.message);
            }
            console.log('Message sent');
        });
    }, 3000);
}

// Open errors will be emitted as an error event
port.on('error', function(err) {
  console.log('Error: ', err.message);
});

// Switches the port into "flowing mode"
port.on('data', function (data) {
  console.log('Data:', data);
});

// Wait for the port to open, then send SMS
port.on('open', function() {
    console.log('Port opened');
    setTimeout(() => {
        sendSMS('+1234567890', 'Hello from Node.js!');
    }, 5000);
});
Editor is loading...
Leave a Comment