MODEM仿真

LoRa AT 命令还需要调试固件,先用Python通过串口仿真一下我们定义的Class A设备。如果需要开发主机程序,此段代码稍作修改,比如把导入的库修改一下,可以运行在MicroPython设备上。

#!/usr/bin/env python
# coding: utf-8
#
# Author: Allan K Liu
# Date: 2017-09-11
#
# From Chip to Cloud, Full Stack IoT Development with Python
#
# The simulator is ported from my previous simulate_cdma for ZTE MC8360V2
# And it simulates LoRaWAN class A device's Hayes AT command set,
# which is defined by STM Electronics NV.
#
# This design can be tailored for LoRaHAM / LoRaBLE / LoRaPy projects.
# The script is quite simple, however in order to simulate more devices, 
# I will refactor it into OOP style.
#
# Hardware Setup:
# With USB/UART bridge breakout module, and short-circuit RX/TX pins for a local loop.
# 
# --- | TXD ----> RXD |----------- 
# MCU | RXD <---- TXD | UART/USB | <----> PC
# --- | GND <---> GND |-----------

import serial
import datetime
import random

def response(ser, res):
    ser.write(res)
    print("LoRaDevClsA:"+res)

ser = serial.Serial('COM52', 9600, timeout=1)
print "********** START *********"
response(ser,"\r\n^SIMST:1\r\n")

while (ser.readable()):
    line = ser.readline()
    if line:
        print "MCU(%d):%s"%(len(line),line)
    else:
        continue
    if len(line)==4 and "AT" in line:
        response(ser,"\r\nOK\r\n")       
    elif "ATE0" in line:
        response(ser,"\r\nOK\r\n")
    elif "AT+DEUI\r\n" in line:
        response(ser,"\r\n+DEUI=12:34:56:5A:A5:FE\r\n\r\n+OK\r\n")
    elif "AT+DADDR" in line:
        res = "\r\n+DAAR=FE:22:33:55:66:88\r\n\r\nOK\r\n"
        response(ser, res)
    elif "AT+APPKEY" in line:
        response(ser,"+APPKEY=12345678\r\nOK\r\n")
    elif "AT+TXP" in line:
        response(ser,"+TXP=20\r\nOK\r\n")
    elif "AT+DR=" in line:    
        response(ser,"+DR=900\r\nOK\r\n")
    elif "AT+SENDB=" in line:
        response(ser,"\r\nOK\r\n")
    elif "AT+SEND" in line:
        res = "\r\n\r\nOK\r\n"
        response(ser, res)
    elif "AT+RECVB" in line:
        response(ser,"+DB=12345678\r\nOK\r\n")
    elif "AT+RECV" in line:
        response(ser,"+DB=12345678\r\nOK\r\n")
    elif "AT+VER" in line:
        response(ser,"+VER=1.0\r\nOK\r\n")
    elif "AT+CFM" in line:
        response(ser,"+CFM=1\r\nOK\r\n")
    elif "AT+CFS" in line:
        response(ser,"+CFS=1\r\nOK\r\n")
    elif "AT+SNR" in line:
        response(ser,"+SNR=12298932098434320")
    elif "AT+RSSI" in line:
        response(ser,"\r\n+RSSI=-50\r\n\r\nOK\r\n")
    elif "AT+BAT" in line:
        response(ser,"+BAT=3.1\r\n\r\nOK\r\n")
    elif "AT^MDM" in line:
        response(ser,"^MDM=3.1\r\n\r\nOK\r\n")
    elif "AT^FREQ" in line:
        response(ser,"+FREQ=3.1\r\n\r\nOK\r\n")
    elif "AT^BW" in line:
        response(ser,"+BW=3.1\r\n\r\nOK\r\n")
    elif "AT^SF" in line:
        response(ser,"+SF=3.1\r\n\r\nOK\r\n")
    elif "AT^LDR" in line:
        response(ser,"+LDR=3.1\r\n\r\nOK\r\n")
    else:
        response(ser,"\r\nERROR\r\n")

在修改ARM mbed上的代码,意识到MODEM的指令集应该采用代码生成器或模板方式产生代码,从定义(JSON)出发,产生Python模拟器、设C/C++固件源码以及网关服务源码。当前LoRa的AT MODEM指令算是少的,要是复杂点儿的就最好这样生成代码,减少代码输入工作量和误差。

你可能感兴趣的:(MODEM仿真)