zabsalahid / serialport-gsm

SerialPort-GSM is a simplified plugin for communicating with gsm modems. (Primarily for sms) (Focused in PDU mode)
MIT License
90 stars 47 forks source link

How To Concurent SMS send #52

Closed refiridho closed 4 years ago

refiridho commented 4 years ago

Hi im triying to send sms with concurent, but only one sms succesfully sending on my number inbox. and in log i see (node:8945) UnhandledPromiseRejectionWarning: Error: Error Resource temporarily unavailable Cannot lock port (node:8945) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

how to solve this ?

Apollon77 commented 4 years ago

Please show your code

refiridho commented 4 years ago

const serialportgsm = require('serialport-gsm'); let gsmModem = serialportgsm.Modem() let options = { baudRate: 115200, dataBits: 8, parity: 'none', stopBits: 1, xon: false, rtscts: false, xoff: false, xany: false, autoDeleteOnReceive: true, enableConcatenation: true, incomingCallIndication: true, incomingSMSIndication: true, pin: '', customInitCommand: 'AT^CURC=0', logger: console }

let phone = { name: "Hello", number: "+62xxxxx", numberSelf: "+62xxxxx", mode: "PDU" }

gsmModem.on('open', () => { console.log(Modem Sucessfully Opened); const message = phone.name; gsmModem.sendSMS(phone.number, message, false, (result) => { console.log(null, Callback Send: Message ID: ${result.data.messageId}, + ${result.data.response} To: ${result.data.recipient} ${JSON.stringify(result)}); }); }); gsmModem.open('/dev/ttyUSB0', options);

I use this in my api enpoint, i try to hit concurent. first hit sending in my inbox, but second hit show the error code like above

leonardn commented 4 years ago

gsmModem.sendSMS

Singleton then, I think you only need this:

const i;
for (i = 0; i < 100; i++) {
  gsmModem.sendSMS(/* param/options */);
}
refiridho commented 4 years ago

this not i Mean @leonardn ... i try to concurency not looping ..

Apollon77 commented 4 years ago

The Serial protocol and also the GSM modem works sequencial, so in fact the library queues up requests when you send multiple and process them one after the other.

Apollon77 commented 4 years ago

And a serial port to a serial device can only be opened by one process, so the error is completely correct. It is simply not possible to connect two processes with the same serial device at the same timepoint

Apollon77 commented 4 years ago

I would close this here

refiridho commented 4 years ago

okay @Apollon77 i understand ... i wrong to connect two processes with the same serial device... btw Thanks All ...