Sending AT commands via socat and chat - RoEdAl/asterisk-chan-quectel GitHub Wiki

Sending AT commands via socat and chat

In order to send AT commands via socat and chat (usually from ppp-chat package):

  1. Create chat script:

    sim900-init.chat:

    "" AT
    OK AT+CLVL=100
    OK AT+CMIC=0,0
    OK AT+ECHO=0,0,0,0
    OK AT+SIDET=0,0
    OK
    
  2. Create bash scipt:

    sim900-init.sh:

    #!/bin/sh
    
    # IOCTL: Put the terminal into exclusive mode
    TIOCEXCL=0x540c
    
    socat -d \
        file:/dev/ttyUSB0,ioctl-void=$TIOCEXCL,flock-ex-nb,b115200,csize=3,cstopb=0,crtscts=0,clocal=1,cfmakeraw,nonblock,crnl \
        exec:'/usr/sbin/chat -v -t 5 -s -f sim900-init.chat',pty
    
  3. Run created script:

    It is a good idea to run this script as non-root:

    chmod +x sim900-init.sh
    ./sim900-init.sh
    

    Output (echo is on):

    send (AT^M)
    expect (OK)
    AT
    OK
     -- got it
    
    send (AT+CLVL=100^M)
    expect (OK)
    
    AT+CLVL=100
    OK
     -- got it
    
    send (AT+CMIC=0,0^M)
    expect (OK)
    
    AT+CMIC=0,0
    OK
     -- got it
    
    send (AT+ECHO=0,0,0,0^M)
    expect (OK)
    
    AT+ECHO=0,0,0,0
    OK
     -- got it
    
    send (AT+SIDET=0,0^M)
    expect (OK)
    
    AT+SIDET=0,0
    OK
     -- got it
    

Manual pages.


If your software still uses lock files for serial ports shell script should be modified:

#!/bin/sh

socat -d \
    file:/dev/ttyUSB0,lockfile=/var/lock/LCK..ttyUSB0,b115200,csize=3,cstopb=0,crtscts=0,clocal=1,cfmakeraw,nonblock,crnl \
    exec:'/usr/sbin/chat -v -s -f sim900-init.chat',pty