当前位置 : 主页 > 网页制作 > Nodejs >

node.js – NodeJS:通过串口连接的GSM设备发送短信

来源:互联网 收集:自由互联 发布时间:2021-06-16
我需要通过串行COM1端口连接到PC的GSM设备发送短信. 我虽然能够收到短信,但是当我试图发送短信时 – 另一方根本就没有收到短信.当我尝试通过超级终端连接GSM设备并发出发送短信的命
我需要通过串行COM1端口连接到PC的GSM设备发送短信.

我虽然能够收到短信,但是当我试图发送短信时 – 另一方根本就没有收到短信.当我尝试通过超级终端连接GSM设备并发出发送短信的命令时,它工作正常.以下是我的代码:

var SerialPort = require("serialport").SerialPort
    var serialPort = new SerialPort("COM1", {
         baudrate: 9600,  dataBits: 8,  parity: 'none',  stopBits: 1, flowControl: false, xon : false, rtscts:false, xoff:false, xany:false, buffersize:0
    });

    serialPort.on("open", function () {
    console.log('Serial communication open');
            serialPort.write("AT+CMGF=1");
            serialPort.write('\r');
            delay(10);
            serialPort.write("AT+CNMI=2,2,0,0,0");
            serialPort.write('\r');
            delay(10);
      serialPort.on('data', function(data) {
            gsm_message_sending(serialPort, "S", "1111111111"); //valid number only
     });
});

function gsm_message_sending(serial,message,phone_no)
{
     serial.write("AT+CMGF=1");
     delay(100); // a simple for loop
     serial.write("AT+CNMI=2,2,0,0,0"); 
     serial.write('\r');
     delay(200);
     serial.write("AT+CMGS=\"+91");
     serial.write(phone_no);
     serial.write('"')
     serial.write('\r');
     serial.write(message); 
     //Now I have to send two time consecutively Ctrl+Z or char - 26
     //I am assuming that maybe cause of below code SMS are not being send
     // Method 1 :
     // serial.write(String.fromCharCode(26)) 
     //serial.write(String.fromCharCode(26)) 
    //Method 2
    //serial.write(Buffer([0x1A]));
    //serial.write(Buffer([0x1A]));
    //serial.write('^z');
    //serial.write('^z');
}

我不确定我在做什么错误或为什么短信没有出去?

你的脚本

var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort("/dev/ttyUSB0", {
     baudrate: 9600,  dataBits: 8,  parity: 'none',  stopBits: 1, flowControl: false, xon : false, rtscts:false, xoff:false, xany:false, buffersize:0
});

serialPort.on("open", function () {
    console.log('Serial communication open');
    serialPort.write("AT^SYSCFG=13,1,3FFFFFFF,2,4");
    serialPort.write('\r');
    serialPort.on('data', function(data) {
        console.log("Received data: " + data);
    });
    gsm_message_sending(serialPort, "test2", "<you phone number>");
});

function gsm_message_sending(serial, message, phone_no) {
    serial.write("AT+CMGF=1");
    serial.write('\r');
    serial.write("AT+CMGS=\"");
    serial.write(phone_no);
    serial.write('"')
    serial.write('\r');
    serial.write(message); 
    serial.write(Buffer([0x1A]));
    serial.write('^z');
}

使用E3131调制解调器完美地为我工作.
正如您所看到的,我更改了一些脚本,以便它只发出一个短信并打印消息:

Received data: AT^SYSCFG=13,1,3FFFFFFF,2,4
Received data:
OK

Received data: AT+CMGF=1
Received data:
OK

Received data: AT+CMGS="
Received data: 0671358943
Received data: "
Received data:
>
Received data: test2
Received data:
Received data: ^z
Received data:
+CMGS: 28

OK
网友评论