0

I am using nodejs and serial-port npm package to connect my smartphone via the com port. I am sending sms using AT commands. The problem I am facing is that it only send sms when there is no empty character in the string. e.g "myTest" will be sent but "my Test" does not get sent and gives error. Here is my code.

const SerialPort = require('serialport');
const serialPort = new SerialPort('myComPort');

serialPort.on('open', () => {
 serialPort.write('AT+CMGF=1\r'); // set SMS text mode
 serialPort.write('AT+CMGS="02122323232"\r'); // send sms message
 serialPort.write('my' + ' ' +String.fromCharCode(26)+'Test' + ' '+ String.fromCharCode(26)+'STring\r\n');
 serialPort.write('\x1A');
 serialPort.write('^z'); 
});

How I can send string with spaces as sms.

Saqib S
  • 501
  • 5
  • 21

2 Answers2

0

Did you try it in heperterm app or something like? Other I think all lines must finished not '\r' but '\r\n'

const SerialPort = require('serialport');
const serialPort = new SerialPort('myComPort');

serialPort.on('open', () => {
 serialPort.write('AT+CMGF=1\r\n'); // set SMS text mode
 serialPort.write('AT+CMGS="02122323232"\r\n'); // send sms message
 serialPort.write('my Test STring\r\n');
 serialPort.write('\x1A');
 serialPort.write('^z'); 
});
Yaroslav Gaponov
  • 1,997
  • 13
  • 12
0

The question text here is a bit different, but the code is virtual identical with the code in this question, and all the same issues apply so all of my answer applies as well.

The differences are that you are correctly terminating the command line with just \r (excellent), and you should use String.fromCharCode(26) to create the escape character.

hlovdal
  • 26,565
  • 10
  • 94
  • 165
  • I have used it as follows serialPort.write('my test string'+String.fromCharCode(26)+'\r\n'); It still does not work :( – Saqib S Aug 02 '19 at 08:00
  • But did you also fix all the other issues? – hlovdal Aug 02 '19 at 10:26
  • yes of course. My only problem is that I cannot send a string with spaces. other than that, the text message is sent. – Saqib S Aug 05 '19 at 05:58
  • @Chasmatu Could you update the question with the new code then? – hlovdal Aug 05 '19 at 07:48
  • I updated the code. Could you please review it again. Thanks. Please bare with me as I am novice in AT commands. – Saqib S Aug 05 '19 at 08:34
  • @Chasmatu I mean [update this question](https://stackoverflow.com/posts/57215371/edit) with your new code. – hlovdal Aug 05 '19 at 13:05
  • @Chasmatu You have not added code to **read and parse** the response lines from the modem nor have you added code to wait for the "ready to receive payload" prompt from the modem. This is what I meant by asking if you had fixed all the other issues (outside of fromCharCode), which still is missing. Information for how to do this is provided in my linked answer. – hlovdal Aug 07 '19 at 12:21