wtorek, 6 listopada 2018

powershell - how to send sms or text message; serial port and powershell

I tried to find code to talk to COM port. Without proper results so I wrote this simple code. Modern phones probably have modem deactivated - I must confirm it, but I tried my old, what's a funny word, HSDPA modem and it is working.
[System.IO.Ports.SerialPort]::getportnames() #list of ports

#my usb modem is connected as virtual COM8
#speed, parity, stop bits should be valid for all devices
$port= new-Object System.IO.Ports.SerialPort COM8,9600,None,8,one
$port.open()

$port.open(); Start-Sleep -Milliseconds 100
$port.Write("AT`r"); Start-Sleep -Milliseconds 100
if ($port.BytesToRead -gt 0) { $port.ReadExisting(); Start-Sleep -Milliseconds 100 } 

$msg = "AT+CMGF=1`r"; $port.Write($msg); Start-Sleep -Milliseconds 100
if ($port.BytesToRead -gt 0) { $port.ReadExisting(); Start-Sleep -Milliseconds 100 } 

#replace xxx with correct number and YY with corect country prefix
$msg = "AT+CMGS=""+YYxxxxxxxxx""`r"; $port.Write($msg); Start-Sleep -Milliseconds 100
if ($port.BytesToRead -gt 0) { $port.ReadExisting();  Start-Sleep -Milliseconds 100 } 

$msg = "something stupid"; $port.Write($msg); Start-Sleep -Milliseconds 100
if ($port.BytesToRead -gt 0) { $port.ReadExisting(); Start-Sleep -Milliseconds 100 } 

#escape character (CTRL-Z from terminal)
$port.Write([char]26); Start-Sleep -Milliseconds 100
if ($port.BytesToRead -gt 0) { $port.ReadExisting(); Start-Sleep -Milliseconds 100 } 

$port.Close()

Output is of course - optional - in my main code it will be connected with commands. 100 ms is only "just in case".

AT commands and communication

AT
OK
AT+CPIN?
+CPIN: SIM PIN

OK
AT - like EHLO in SMTP
AT+CPIN? - query - is PIN required? possible answers are:
+CPIN: SIM PIN - when You MUST enter PIN
+CPIN: SIM PIN2 - when You MUST enter PIN2
+CPIN: SIM PUK - when You MUST enter PUK
+CPIN: SIM PUK2 - when You MUST enter PUK2
+CPIN: READY - when device is not expecting any code
AT - like EHLO in SMTP
AT+CPIN-=
COMMAND NOT SUPPORT
AT+CPIN=
ERROR
AT+CPIN=0000
OK
AT+CPIN?
+CPIN: READY

OK
AT+CPIN=0000
+CME ERROR: operation not allowed
AT+CPIN-= - my mistake - and His majesty answer: COMMAND NOT SUPPORT
AT+CPIN= - my mistake - and His majesty answer: ERROR, because I didn't provide pin
AT+CPIN=0000 - yes - this of course fake pin, but answer is real OK
AT+CPIN? - yes - my device is ready +CPIN: READY, so the same command entering pin...
AT+CPIN=0000 ... will finish with +CME ERROR: operation not allowed
AT+CMGF=1
OK
AT+CMGF=1 - set device in text messages mode, with 0 it will switch in PDU mode - Protocol Descritpion Unit - also for text messages but encoded way
AT+CMGS="xxxxxxxxx"
> mamma mia

+CMGS: 109

OK
AT+CMGS="xxxxxxxxx" - I've been started message addressed to phone xxxxxxxxx
teminal is waiting for the message finished with escape character (26 ascii - Ctrl-Z)
+CMGS: 109 this and OK confirm, that message was sent

Brak komentarzy:

Prześlij komentarz