我需要通过连接到PC的串行COM1端口的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');
}
我不确定我在做什么,或者为什么短信没有出去?
答案 0 :(得分:1)
您的脚本
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
答案 1 :(得分:0)
尝试:
sudo nodejs yourfilejs.js
在ubuntu上如果没有sudo会引发错误&#39;权限被拒绝&#39;
答案 2 :(得分:0)
+1为Mermoz的答案。但是它给了我一个不推荐使用Buffer的警告,并且无法发送消息。这是为我工作的解决方案。希望对别人有帮助。
var SerialPort = require("serialport");
function sendSMS(){
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", async function () {
console.log('Serial communication open');
serialPort.write("AT+CMGF=1\r",11);
await sleep(100);
serialPort.write("AT+CSCS=\"gsm\"\r",15);
await sleep(100);
serialPort.write("AT+CSCA=\"<Service center number>\"\r",19);
await sleep(100);
serialPort.write("AT+CMGS=\"<Receiver's number>\"\r\n\0",23);
await sleep(1000);
serialPort.write("test\r",6);
await sleep(1000);
serialPort.write("\r",0);
await sleep(100);
serialPort.write("\x1A",2);
serialPort.on('data', function(data) {
console.log("Received data: " + data);
});
});
}
function sleep(ms){
return new Promise(resolve=>{
setTimeout(resolve,ms)
})
}