--- /dev/null
+/**
+ ******************************************************************************
+ * @brief ATÃüÁîͨÐŹÜÀí(OS°æ±¾)
+ *
+ * Copyright (c) 2020, <morro_luo@163.com>
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2020-01-02 Morro Initial version.
+ * 2021-02-01 Morro Ö§³ÖURC»Øµ÷ÖнÓÊÕÊý¾Ý.
+ * 2021-02-05 Morro 1.ÐÞ¸Ästruct at_obj,È¥³ýÁ´±í¹ÜÀí»úÖÆ
+ * 2.ɾ³ý at_obj_destroy½Ó¿Ú
+ * 2021-03-21 Morro ɾ³ýat_objÖеÄat_work_ctx_tÓò,¼õÉÙÄÚ´æÊ¹ÓÃ
+ * 2021-04-08 Morro ½â¾öÖØ¸´ÊÍ·ÅÐźÅÁ¿µ¼ÖÂÃüÁî³öÏֵȴý³¬Ê±µÄÎÊÌâ
+ * 2021-05-15 Morro ÓÅ»¯URCÆ¥Åä³ÌÐò
+ * 2021-07-20 Morro Ôö¼ÓËø,½â¾öÖ´ÐÐat_do_workʱurc²¿·ÖÊý¾Ý¶ªÊ§ÎÊÌâ
+ ******************************************************************************
+ */
+#include "at.h"
+#include <stdarg.h>
+#include <string.h>
+#include <stdio.h>
+#include <stddef.h>
+
+#define MAX_AT_LOCK_TIME 60 * 1000 //ATÃüÁîËø¶¨Ê±¼ä
+
+static void urc_recv_process(at_obj_t *at, const char *buf, unsigned int size);
+
+/**
+ * @brief ĬÈϵ÷ÊÔ½Ó¿Ú
+ */
+static void nop_dbg(const char *fmt, ...){}
+
+
+/**
+ * @brief Êä³ö×Ö·û´®
+ */
+static void put_string(at_obj_t *at, const char *s)
+{
+ while (*s != '\0')
+ at->adap.write(s++, 1);
+}
+
+/**
+ * @brief Êä³ö×Ö·û´®(´ø»»ÐÐ)
+ */
+static void put_line(at_obj_t *at, const char *s)
+{
+ put_string(at, s);
+ put_string(at, "\r\n");
+ at->adap.debug("->\r\n%s\r\n", s);
+}
+
+
+/**
+ * @brief ×÷Òµ¶Á²Ù×÷
+ */
+static unsigned int at_work_read(struct at_work_ctx *e, void *buf, unsigned int len)
+{
+ at_obj_t *at = e->at;
+ len = at->adap.read(buf, len);
+ urc_recv_process(at, buf, len); //µÝ½»µ½URC½øÐд¦Àí
+ return len;
+}
+
+/**
+ * @brief ×÷ҵд²Ù×÷
+ */
+static unsigned int at_work_write(struct at_work_ctx *e, const void *buf, unsigned int len)
+{
+ return e->at->adap.write(buf, len);
+}
+
+//´òÓ¡Êä³ö
+static void at_work_print(struct at_work_ctx *e, const char *cmd, ...)
+{
+ char buf[MAX_AT_CMD_LEN];
+ va_list args;
+ va_start(args, cmd);
+ vsnprintf(buf, sizeof(buf), cmd, args);
+ put_line(e->at, buf);
+ va_end(args);
+}
+
+//µÈ´ýATÃüÁîÏìÓ¦
+static at_return wait_resp(at_obj_t *at, at_respond_t *r)
+{
+ at->ret = AT_RET_TIMEOUT;
+ at->resp_timer = at_get_ms();
+ at->rcv_cnt = 0; //Çå¿Õ½ÓÊÕ»º´æ
+ at->resp = r;
+ at_sem_wait(at->completed, r->timeout);
+ at->adap.debug("<-\r\n%s\r\n", r->recvbuf);
+ at->resp = NULL;
+ return at->ret;
+}
+
+/**
+ * @brief µÈ´ý½ÓÊÕµ½Ö¸¶¨´®
+ * @param[in] resp - ÆÚ´ý´ý½ÓÊÕ´®(Èç"OK",">")
+ * @param[in] timeout - µÈ´ý³¬Ê±Ê±¼ä
+ */
+at_return wait_recv(struct at_work_ctx *e, const char *resp, unsigned int timeout)
+{
+ char buf[64];
+ int cnt = 0, len;
+ at_obj_t *at = e->at;
+ at_return ret = AT_RET_TIMEOUT;
+ unsigned int timer = at_get_ms();
+ while (at_get_ms() - timer < timeout) {
+ len = e->read(e, &buf[cnt], sizeof(buf) - cnt);
+ if (len > 0) {
+ cnt += len;
+ buf[cnt] = '\0';
+ if (strstr(buf, resp)) {
+ ret = AT_RET_OK;
+ break;
+ } else if (strstr(buf, "ERROR")) {
+ ret = AT_RET_ERROR;
+ break;
+ }
+ } else
+ at_delay(1);
+ }
+ at->adap.debug("%s\r\n", buf);
+ return ret;
+}
+
+/**
+ * @brief ´´½¨AT¿ØÖÆÆ÷
+ * @param[in] adap - AT½Ó¿ÚÊÊÅäÆ÷
+ */
+void at_obj_init(at_obj_t *at, const at_adapter_t *adap)
+{
+ at->adap = *adap;
+ at->rcv_cnt = 0;
+ at->send_lock = at_sem_new(1);
+ at->recv_lock = at_sem_new(1);
+ at->completed = at_sem_new(0);
+ at->urc_item = NULL;
+ if (at->adap.debug == NULL)
+ at->adap.debug = nop_dbg;
+
+}
+
+/**
+ * @brief Ö´ÐÐÃüÁî
+ * @param[in] fmt - ¸ñʽ»¯Êä³ö
+ * @param[in] r - ÏìÓ¦²ÎÊý,Èç¹ûÌîNULL, ĬÈÏ·µ»ØOK±íʾ³É¹¦,µÈ´ý5s
+ * @param[in] args - Èç±ä²ÎÊýÁбí
+ */
+at_return at_do_cmd(at_obj_t *at, at_respond_t *r, const char *cmd)
+{
+ at_return ret;
+ char defbuf[64];
+ at_respond_t default_resp = {"OK", NULL, sizeof(defbuf), 5000};
+ default_resp.recvbuf = defbuf;
+
+ if (r == NULL) {
+ r = &default_resp; //ĬÈÏÏìÓ¦
+ }
+ if (!at_sem_wait(at->send_lock, r->timeout)) {
+ return AT_RET_TIMEOUT;
+ }
+ at->busy = true;
+
+ while (at->urc_cnt) {
+ at_delay(10);
+ }
+ put_line(at, cmd);
+ ret = wait_resp(at, r);
+ at_sem_post(at->send_lock);
+ at->busy = false;
+ return ret;
+}
+
+/**
+ * @brief Ö´ÐÐAT×÷Òµ
+ * @param[in] at - AT¿ØÖÆÆ÷
+ * @param[in] work - ×÷ÒµÈë¿Úº¯Êý(ÀàÐÍ - int (*)(at_work_ctx_t *))
+ * @param[in] params- ×÷Òµ²ÎÊý
+ * @return ÒÀÀµÓÚworkµÄ·µ»ØÖµ
+ */
+int at_do_work(at_obj_t *at, at_work work, void *params)
+{
+ at_work_ctx_t ctx;
+ int ret;
+ if (!at_sem_wait(at->send_lock, MAX_AT_LOCK_TIME)) {
+ return AT_RET_TIMEOUT;
+ }
+ if (!at_sem_wait(at->recv_lock, MAX_AT_LOCK_TIME))
+ return AT_RET_TIMEOUT;
+
+ at->busy = true;
+ //¹¹Ôìat_work_ctx_t
+ ctx.params = params;
+ ctx.printf = at_work_print;
+ ctx.read = at_work_read;
+ ctx.write = at_work_write;
+ ctx.wait_resp = wait_recv;
+ ctx.at = at;
+
+ at->rcv_cnt = 0;
+ ret = work(&ctx);
+ at_sem_post(at->recv_lock);
+ at_sem_post(at->send_lock);
+ at->busy = false;
+ return ret;
+}
+
+/**
+ * @brief ·Ö¸îÏìÓ¦ÐÐ
+ * @param[in] recvbuf - ½ÓÊÕ»º³åÇø
+ * @param[out] lines - ÏìÓ¦ÐÐÊý×é
+ * @param[in] separator- ·Ö¸î·û(, \n)
+ * @return ÐÐÊý
+ */
+int at_split_respond_lines(char *recvbuf, char *lines[], int count, char separator)
+{
+ char *s = recvbuf;
+ size_t i = 0;
+ if (s == NULL || lines == NULL)
+ return 0;
+
+ lines[i++] = s;
+ while(*s && i < count) {
+ if (*s == ',') {
+ *s = '\0';
+ lines[i++] = s + 1; /*Ö¸ÏòÏÂÒ»¸ö×Ó´®*/
+ }
+ s++;
+ }
+ return i;
+}
+/**
+ * @brief ´ÓURC»º³åÇøÖвéѯURCÏî
+ * @param[in] urcline - URCÐÐ
+ * @return true - Õý³£Ê¶±ð²¢´¦Àí, false - δʶ±ðURC
+ */
+const urc_item_t *find_urc_item(at_obj_t *at, char *urc_buf, unsigned int size)
+{
+ const urc_item_t *tbl = at->adap.utc_tbl;
+ int i;
+ if (size < 2)
+ return NULL;
+ for (i = 0; i < at->adap.urc_tbl_count; i++) {
+ if (strstr(urc_buf, tbl->prefix))
+ return tbl;
+ tbl++;
+ }
+ return NULL;
+}
+
+/**
+ * @brief urc ´¦Àí×ÜÈë¿Ú
+ * @param[in] urcline - URCÐÐ
+ */
+static void urc_handler_entry(at_obj_t *at, char *urcline, unsigned int size)
+{
+ at_urc_ctx_t context; // = { NULL, NULL, at->adap.urc_bufsize, size};
+ context.read = at->adap.read;
+ context.buf = urcline;
+ context.bufsize = at->adap.urc_bufsize;
+ context.recvlen = size;
+
+ at->adap.debug("<=\r\n%s\r\n", urcline);
+ at->urc_item->handler(&context); /* µÝ½»µ½Éϲ㴦Àí */
+}
+
+/**
+ * @brief urc ½ÓÊÕ´¦Àí
+ * @param[in] ch - ½ÓÊÕ×Ö·û
+ * @return none
+ */
+static void urc_recv_process(at_obj_t *at, const char *buf, unsigned int size)
+{
+ register char *urc_buf;
+ int ch;
+ urc_buf = at->adap.urc_buf;
+
+ //½ÓÊÕ³¬Ê±´¦Àí,ĬÈÏMAX_URC_RECV_TIMEOUT
+ if (at->urc_cnt > 0 && at_istimeout(at->urc_timer, MAX_URC_RECV_TIMEOUT)) {
+ urc_buf[at->urc_cnt] = '\0';
+ if (at->urc_cnt > 2)
+ at->adap.debug("urc recv timeout=>%s\r\n", urc_buf);
+ at->urc_cnt = 0;
+ at->urc_item = NULL;
+ }
+
+ while (size--) {
+ at->urc_timer = at_get_ms();
+ ch = *buf++;
+ urc_buf[at->urc_cnt++] = ch;
+ //todo: ADD TCP RX -- "+IPD,0,4:test"
+
+ if (strchr(SPEC_URC_END_MARKS, ch) || ch == '\0') { //½áÊø±ê¼ÇÁбí
+ urc_buf[at->urc_cnt] = '\0';
+ if (at->urc_item == NULL)
+ at->urc_item = find_urc_item(at, urc_buf, at->urc_cnt);
+ if (at->urc_item != NULL){
+ if (strchr(at->urc_item->end_mark, ch)) { //Æ¥Åä½áÊø±ê¼Ç
+ urc_handler_entry(at, urc_buf, at->urc_cnt);
+ at->urc_cnt = 0;
+ at->urc_item = NULL;
+ }
+ } else if (ch == '\r' || ch == '\n'|| ch == '\0') {
+ if (at->urc_cnt > 2 && !at->busy) {
+ at->adap.debug("%s\r\n", urc_buf); //δʶ±ðµ½µÄURC
+ }
+ at->urc_cnt = 0;
+ }
+ } else if (at->urc_cnt >= at->adap.urc_bufsize) { //Òç³ö´¦Àí
+ at->urc_cnt = 0;
+ at->urc_item = NULL;
+ }
+ }
+}
+
+/**
+ * @brief ÃüÁîÏìӦ֪ͨ
+ * @return none
+ */
+static void resp_notification(at_obj_t *at, at_return ret)
+{
+ at->ret = ret;
+ at_sem_post(at->completed);
+}
+
+/**
+ * @brief Ö¸ÁîÏìÓ¦½ÓÊÕ´¦Àí
+ * @param[in] buf - ½ÓÊÕ»º³åÇø
+ * @param[in] size - »º³åÇøÊý¾Ý³¤¶È
+ * @return none
+ */
+static void resp_recv_process(at_obj_t *at, const char *buf, unsigned int size)
+{
+ char *rcv_buf;
+ unsigned short rcv_size;
+ at_respond_t *resp = at->resp;
+
+ if (resp == NULL) //ÎÞÃüÁîÇëÇó
+ return;
+
+ if (size) {
+ rcv_buf = (char *)resp->recvbuf;
+ rcv_size = resp->bufsize;
+
+ if (at->rcv_cnt + size >= rcv_size) { //½ÓÊÕÒç³ö
+ at->rcv_cnt = 0;
+ at->adap.debug("Receive overflow:%s", rcv_buf);
+ }
+ /*½«½ÓÊÕµ½µÄÊý¾Ý·ÅÖÁrcv_bufÖÐ ---------------------------------------------*/
+ memcpy(rcv_buf + at->rcv_cnt, buf, size);
+ at->rcv_cnt += size;
+ rcv_buf[at->rcv_cnt] = '\0';
+
+ if (strstr(rcv_buf, resp->matcher)) { //½ÓÊÕÆ¥Åä
+ resp_notification(at, AT_RET_OK);
+ return;
+ } else if (strstr(rcv_buf, "ERROR")) {
+ resp_notification(at, AT_RET_ERROR);
+ return;
+ }
+ }
+
+ if (at_istimeout(at->resp_timer, resp->timeout)) //½ÓÊÕ³¬Ê±
+ resp_notification(at, AT_RET_TIMEOUT);
+ else if (at->suspend) //Ç¿ÖÆÖÕÖ¹
+ resp_notification(at, AT_RET_ABORT);
+
+}
+
+/**
+ * @brief ATæÅжÏ
+ * @return true - ÓÐATÖ¸Áî»òÕßÈÎÎñÕýÔÚÖ´ÐÐÖÐ
+ */
+bool at_obj_busy(at_obj_t *at)
+{
+ return !at->busy && at_istimeout(at->urc_timer, 2000);
+}
+
+/**
+ * @brief ¹ÒÆðAT×÷Òµ
+ * @return none
+ */
+void at_suspend(at_obj_t *at)
+{
+ at->suspend = 1;
+}
+
+/**
+ * @brief »Ö¸´AT×÷Òµ
+ * @return none
+ */
+void at_resume(at_obj_t *at)
+{
+ at->suspend = 0;
+}
+
+/**
+ * @brief AT´¦Àí
+ * @return none
+ */
+void at_process(at_obj_t *at)
+{
+ char buf[16];
+ unsigned int len;
+ if (!at_sem_wait(at->recv_lock, MAX_AT_LOCK_TIME))
+ return;
+ do {
+ len = at->adap.read(buf, sizeof(buf));
+ urc_recv_process(at, buf,len);
+ resp_recv_process(at, buf, len);
+ } while (len);
+
+ at_sem_post(at->recv_lock);
+}
+
--- /dev/null
+/******************************************************************************
+ * @brief ATÃüÁîͨÐŹÜÀí(OS°æ±¾)
+ *
+ * Copyright (c) 2020, <morro_luo@163.com>
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2020-01-02 Morro Initial version.
+ * 2021-02-01 Morro Ö§³ÖURC»Øµ÷ÖнÓÊÕÊý¾Ý.
+ * 2021-02-05 Morro 1.ÐÞ¸Ästruct at_obj,È¥³ýÁ´±í¹ÜÀí»úÖÆ
+ * 2.ɾ³ý at_obj_destroy½Ó¿Ú
+ * 2021-03-21 Morro ɾ³ýat_objÖеÄat_work_ctx_tÓò,¼õÉÙÄÚ´æÊ¹ÓÃ
+ * 2021-04-08 Morro ½â¾öÖØ¸´ÊÍ·ÅÐźÅÁ¿µ¼ÖÂÃüÁî³öÏֵȴý³¬Ê±µÄÎÊÌâ
+ * 2021-05-15 Morro ÓÅ»¯URCÆ¥Åä³ÌÐò
+ * 2021-07-20 Morro Ôö¼Ó½ÓÊÕËø,½â¾öÖ´ÐÐat_do_workʱurc²¿·ÖÊý¾Ý¶ªÊ§ÎÊÌâ
+ ******************************************************************************/
+
+#ifndef _AT_H_
+#define _AT_H_
+
+#include "at_util.h"
+#include <stdbool.h>
+
+/* µ¥ÐÐ×î´óÃüÁ¶È */
+#define MAX_AT_CMD_LEN 128
+
+/* µ¥ÐÐurc½ÓÊÕ³¬Ê±Ê±¼ä*/
+#define MAX_URC_RECV_TIMEOUT 300
+
+/* Ö¸¶¨µÄURC ½áÊø±ê¼ÇÁбí */
+#define SPEC_URC_END_MARKS ":,\r\n"
+
+struct at_obj; /* AT¶ÔÏó*/
+
+/*ATÃüÁîÏìÓ¦Âë ---------------------------------------------------------------*/
+typedef enum {
+ AT_RET_OK = 0, /* Ö´Ðгɹ¦*/
+ AT_RET_ERROR, /* Ö´ÐдíÎó*/
+ AT_RET_TIMEOUT, /* ÏìÓ¦³¬Ê±*/
+ AT_RET_ABORT, /* δ֪´íÎó*/
+}at_return;
+
+/**
+ * @brief URC ÉÏÏÂÎÄ(Context) ¶¨Òå
+ */
+typedef struct {
+ /**
+ * @brief Êý¾Ý¶ÁÈ¡½Ó¿Ú
+ * @params buf - »º³åÇø
+ * @params len - »º³åÇø´óС
+ */
+ unsigned int (*read)(void *buf, unsigned int len);
+ char *buf; /* Êý¾Ý»º³åÇø */
+ int bufsize; /* »º³åÇø´óС */
+ int recvlen; /* ÒѽÓÊÕÊý¾Ý³¤¶È*/
+} at_urc_ctx_t;
+
+/*(Unsolicited Result Codes (URCs))´¦ÀíÏî ------------------------------------*/
+typedef struct {
+ /**
+ * @brief urc ǰ׺(Èç"+CSQ: ")
+ */
+ const char *prefix;
+ /**
+ * @brief urc Ö¸¶¨½áÊø×Ö·û±ê¼Ç(²Î¿¼DEF_URC_END_MARKSÁÐ±í£¬Èç¹û²»Ö¸¶¨ÔòĬÈÏ"\n")
+ * @note
+ */
+ const char *end_mark;
+ /**
+ * @brief urc´¦Àí³ÌÐò
+ * @params ctx - URC ÉÏÏÂÎÄ
+ */
+ void (*handler)(at_urc_ctx_t *ctx);
+}urc_item_t;
+
+/**
+ * @brief AT½Ó¿ÚÊÊÅäÆ÷(ÓÃÓÚ³õʼ»¯AT¿ØÖÆÆ÷)
+ */
+typedef struct {
+
+ /**
+ * @brief Êý¾Ýд½Ó¿Ú
+ */
+ unsigned int (*write)(const void *buf, unsigned int len);
+
+ /**
+ * @brief Êý¾Ý¶Á½Ó¿Ú
+ */
+ unsigned int (*read)(void *buf, unsigned int len);
+ /**
+ * @brief µ÷ÊÔ´òÓ¡Êä³ö½Ó¿Ú,Èç¹û²»ÐèÒªÔòÌîNULL
+ */
+ void (*debug)(const char *fmt, ...);
+
+ /**
+ * @brief urc ´¦Àíº¯Êý±í,Èç¹û²»ÐèÒªÔòÌîNULL
+ */
+ urc_item_t *utc_tbl;
+ /**
+ * @brief urc½ÓÊÕ»º³åÇø,Èç¹û²»ÐèÒªÔòÌîNULL
+ */
+ char *urc_buf;
+ /**
+ * @brief utc_tblÏî¸öÊý,Èç¹û²»ÐèÒªÔòÌî0
+ */
+ unsigned short urc_tbl_count;
+ /**
+ * @brief urc»º³åÇø´óС,Èç¹û²»ÐèÒªÔòÌî0
+ */
+ unsigned short urc_bufsize;
+}at_adapter_t;
+
+/**
+ * @brief ATÃüÁîÏìÓ¦²ÎÊý
+ */
+typedef struct {
+ const char *matcher; /* ½ÓÊÕÆ¥Åä´® */
+ char *recvbuf; /* ½ÓÊÕ»º³åÇø */
+ unsigned short bufsize; /* »º³åÇø³¤¶È */
+ unsigned int timeout; /* ×î´ó³¬Ê±Ê±¼ä */
+} at_respond_t;
+
+/** work context ------------------------------------------------------------*/
+/**
+ * @brief AT×÷ÒµÉÏÏÂÎÄ(Work Context) ¶¨Òå
+ */
+typedef struct at_work_ctx {
+ struct at_obj *at;
+
+ //×÷Òµ²ÎÊý,ÓÉat_do_work½Ó¿Ú´«Èë
+ void *params;
+ /**
+ * @brief Êý¾Ýд²Ù×÷
+ */
+ unsigned int (*write)(struct at_work_ctx *self, const void *buf, unsigned int len);
+ /**
+ * @brief Êý¾Ý¶Á²Ù×÷
+ */
+ unsigned int (*read)(struct at_work_ctx *self, void *buf, unsigned int len);
+
+ /**
+ * @brief ¸ñʽ»¯´òÓ¡Êä³ö
+ */
+ void (*printf)(struct at_work_ctx *self, const char *fmt, ...);
+ /*
+ * @brief µÈ´ý»Ø¸´
+ * @param[in] prefix - ÆÚ´ý´ý½ÓÊÕǰ׺(Èç"OK",">")
+ * @param[in] timeout - µÈ´ý³¬Ê±Ê±¼ä
+ */
+ at_return (*wait_resp)(struct at_work_ctx *self, const char *prefix,
+ unsigned int timeout);
+}at_work_ctx_t;
+
+
+/*AT¶ÔÏó ---------------------------------------------------------------------*/
+typedef struct at_obj {
+ at_adapter_t adap; /* ½Ó¿ÚÊÊÅäÆ÷*/
+ at_sem_t send_lock; /* ·¢ËÍËø*/
+ at_sem_t recv_lock; /* ½ÓÊÕËø*/
+ at_sem_t completed; /* ÃüÁîÍê³ÉÐźÅ*/
+ at_respond_t *resp; /* ATÓ¦´ðÐÅÏ¢*/
+ const urc_item_t *urc_item; /* µ±Ç°URCÏî*/
+ unsigned int resp_timer; /* ÏìÓ¦½ÓÊÕ¶¨Ê±Æ÷*/
+ unsigned int urc_timer; /* URC¶¨Ê±Æ÷ */
+ at_return ret; /* ÃüÁîÖ´Ðнá¹û*/
+ unsigned short urc_cnt, rcv_cnt; /* ½ÓÊÕ¼ÆÊýÆ÷*/
+ unsigned char busy : 1;
+ unsigned char suspend: 1;
+ unsigned char dowork : 1;
+}at_obj_t;
+
+typedef int (*at_work)(at_work_ctx_t *);
+
+void at_obj_init(at_obj_t *at, const at_adapter_t *adap); /* AT³õʼ»¯*/
+
+bool at_obj_busy(at_obj_t *at);
+
+void at_suspend(at_obj_t *at); /* ¹ÒÆð*/
+
+void at_resume(at_obj_t *at); /* »Ö¸´*/
+
+at_return at_do_cmd(at_obj_t *at, at_respond_t *r, const char *cmd);
+
+int at_split_respond_lines(char *recvbuf, char *lines[], int count, char separator);
+
+int at_do_work(at_obj_t *at, at_work work, void *params); /* ×Ô¶¨ÒåAT×÷Òµ*/
+
+
+void at_process(at_obj_t *at); /* AT½ÓÊÕ´¦Àí*/
+
+#endif
--- /dev/null
+/******************************************************************************
+ * @brief atÄ£¿éOSÏà¹ØÒÆÖ²½Ó¿Ú
+ *
+ * Copyright (c) 2020, <morro_luo@163.com>
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2020-01-02 Morro ³õ°æ
+ ******************************************************************************/
+
+#ifndef _ATUTIL_H_
+#define _ATUTIL_H_
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <stddef.h>
+
+#include "FreeRTOS.h"
+#include "task.h"
+#include "queue.h"
+#include "semphr.h"
+
+typedef void *at_sem_t; /*ÐźÅÁ¿*/
+
+/*
+ * @brief »ñÈ¡µ±Ç°ÏµÍ³ºÁÃëÊý
+ */
+static unsigned int at_get_ms(void)
+{
+ return (xTaskGetTickCount() / portTICK_PERIOD_MS);
+}
+/*
+ * @brief ³¬Ê±ÅжÏ
+ * @retval true | false
+ */
+static bool at_istimeout(unsigned int start_time, unsigned int timeout)
+{
+ return at_get_ms() - start_time > timeout;
+}
+
+/*
+ * @brief ºÁÃëÑÓʱ
+ * @retval none
+ */
+static void at_delay(uint32_t ms)
+{
+ vTaskDelay(ms*portTICK_PERIOD_MS);
+}
+/*
+ * @brief ´´½¨ÐźÅ
+ * @retval none
+ */
+static at_sem_t at_sem_new(int value)
+{
+
+ /*
+#define vSemaphoreCreateBinary( xSemaphore ) \
+{ \
+( xSemaphore ) = xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE ); \
+if( ( xSemaphore ) != NULL ) \
+{ \
+( void ) xSemaphoreGive( ( xSemaphore ) ); \
+} \
+}
+ ***/
+at_sem_t sem;
+
+vSemaphoreCreateBinary(sem);
+if( sem && value==0 )
+{
+ ( void ) xSemaphoreTake( ( sem ), 0 );
+}
+
+return sem;
+}
+/*
+ * @brief µÈ´ýÐźÅ
+ * @retval none
+ */
+static bool at_sem_wait(at_sem_t s, uint32_t timeout)
+{
+ //1 pdPASS( pdTRUE ) / 0 pdFAIL( pdFALSE )
+ return xSemaphoreTake( s, pdMS_TO_TICKS(timeout) );
+}
+
+/*
+ * @brief ÊÍ·ÅÐźÅ
+ * @retval none
+ */
+static void at_sem_post(at_sem_t s)
+{
+ xSemaphoreGive(s);
+}
+
+/*
+ * @brief Ïú»ÙµÄÐźÅ
+ * @retval none
+ */
+static void at_sem_free(at_sem_t s)
+{
+ if( s )
+ vQueueDelete( s );
+}
+
+#endif
#include "uppercomputer.h"
#include "eventmonitoring.h"
#include "batterytesting.h"
+#include "wifi.h"
+
#define DATA_INFO 0
xTaskCreate(ControllerPower_task,"°âÊÖÆô¶¯¿ØÖÆÈÎÎñ", 240, 0, 10, &ControllerPowerTask_Handler);//°âÊÖÆô¶¯¿ØÖÆÈÎÎñ (ÓÅÏȼ¶´ýÈ·ÈÏ\¶ÑÕ»´óС´ýÈ·ÈÏ)
//xTaskCreate(AnglePower_task,"½Ç¶Èģʽ¿ØÖÆÈÎÎñ", 120, 0, 2, &AnglePowerTask_Handler); //°âÊֽǶÈģʽ¿ØÖÆÈÎÎñ (ÓÅÏȼ¶´ýÈ·ÈÏ\¶ÑÕ»´óС´ýÈ·ÈÏ)
xTaskCreate(EventMonitoring_task,"½çÃæ²ÎÊý´¢´æÈÎÎñ", 120, 0, 1, &EventMonitoringTask_Handler);//½çÃæ²ÎÊý´¢´æÈÎÎñ (ÓÅÏȼ¶´ýÈ·ÈÏ\¶ÑÕ»´óС´ýÈ·ÈÏ)
+
+ xTaskCreate(wifi_task, "Wifi", (512), 0, 1, &WifiTask_Handler); //WifiÈÎÎñº¯Êý
+
taskEXIT_CRITICAL(); //Í˳öÁÙ½çÇø
}
--- /dev/null
+#include "wifi.h"
+#include "datastore.h"
+#include "eventmonitoring.h"
+#include "taskmanage.h"
+#include "uishow.h"
+#include "adc.h"
+#include "led.h"
+#include "Exti.h"
+#include "adc.h"
+
+#include "at.h"
+
+
+TaskHandle_t WifiTask_Handler = NULL;
+TaskHandle_t AtTask_Handler = NULL;
+
+
+#define WIFI_RBUF_LEN (512)
+
+static char wifi_urc_buf[256];
+
+uint8_t wifi_rbuf[WIFI_RBUF_LEN]; //½ÓÊÕ»º³å,×î´óUSART_REC_LEN¸ö×Ö½Ú.
+uint32_t wifi_data_read_ptr;
+uint32_t wifi_data_write_ptr;
+
+int32_t is_wifi_rbuf_full( void )
+{
+ if( ((wifi_data_write_ptr+1)%sizeof(wifi_rbuf)) == (wifi_data_read_ptr%sizeof(wifi_rbuf)) )
+ return 1;
+ else
+ return 0;
+}
+int32_t is_wifi_rbuf_empty( void )
+{
+ if( wifi_data_write_ptr == wifi_data_read_ptr )
+ return 1;
+ else
+ return 0;
+}
+
+int32_t wifi_rbuf_push_byte( uint8_t byte )
+{
+ if( is_wifi_rbuf_full() )
+ return -1;
+ wifi_rbuf[wifi_data_write_ptr] = byte;
+ wifi_data_write_ptr = (wifi_data_write_ptr +1)%sizeof( wifi_rbuf);
+ return 0;
+}
+
+int32_t wifi_rbuf_pop_byte( uint8_t *out_byte )
+{
+ if( !out_byte || is_wifi_rbuf_empty() )
+ return -1;
+ *out_byte = wifi_rbuf[wifi_data_read_ptr];
+ wifi_data_read_ptr = (wifi_data_read_ptr+1)%sizeof( wifi_rbuf );
+
+ return 0;
+}
+
+void Uart5Write(u8 *buf ,u16 len);
+
+void wifi_uart_rx_byte_cb( uint8_t byte )
+{
+ wifi_rbuf_push_byte( byte );
+}
+
+static uint32_t wifi_write(const void *buf, unsigned int len)
+{
+ Uart5Write( (uint8_t*)buf, len );
+ return 0;
+}
+
+static uint32_t wifi_read(void *buf, unsigned int len)
+{
+ int i;
+ uint8_t *p = (uint8_t *)buf;
+ for( i=0; i<len; i++, p++ ){
+ if( is_wifi_rbuf_empty() )
+ break;
+ wifi_rbuf_pop_byte( p );
+ }
+ return i;
+}
+
+static void wifi_debug(const char *fmt, ...)
+{
+ return;
+}
+
+void ipd_handler(at_urc_ctx_t *ctx)
+{
+ int i;
+ int ch;
+ int len;
+ char *p;
+
+ printf( "utc handler, ctx:%p\n", ctx );
+ if( sscanf( ctx->buf, "+IPD,%d,%d:", &ch, &len ) == 2 ){
+ printf( "TCP CH:%d, DATA Len:%d\n", ch, len );
+ p = strstr( ctx->buf, ":" );
+ if( p ){
+ p++;
+ printf( "DATA:[" );
+ for( i=0; i<len; i++, p++ ){
+ printf( "%c", *p );
+ }
+ printf( "]\n" );
+ }
+ }
+
+}
+
+urc_item_t wifi_utc_tbl[]={
+ {
+
+ "+IPD", "\r\n", ipd_handler
+ },
+};
+
+
+at_obj_t wifi_at;
+
+const at_adapter_t wifi_adap = {
+ wifi_write,
+ wifi_read,
+ wifi_debug,
+ wifi_utc_tbl,
+ wifi_urc_buf,
+ sizeof(wifi_utc_tbl)/sizeof(wifi_utc_tbl[0]),
+ sizeof(wifi_urc_buf)
+};
+
+int read_wifi_mode(at_obj_t *at )
+{
+ char *p;
+ //½ÓÊÕ»º³åÇø
+ char recvbuf[128];
+ int mode = 0;
+ //ATÓ¦´ð
+ at_respond_t r = {"OK", NULL, sizeof(recvbuf), 3000};
+ r.recvbuf = recvbuf;
+ //
+ if (at_do_cmd(at, &r, "AT+CWMODE?") == AT_RET_OK){
+ //ÌáÈ¡³öÏìÓ¦Êý¾Ý
+ p = strstr( recvbuf, "+CWMODE:" );
+ if( p && sscanf(p, "+CWMODE:%d", &mode) == 1){
+ printf( "read wifi mode:%d\n", mode );
+ //return 0;
+ }
+ }
+
+ if (at_do_cmd(at, &r, "AT+CIPAP?") == AT_RET_OK){
+ p = strstr( recvbuf, "+CIPAP:" );
+ if( p ){
+ printf( "read wifi cipap:[%s]\n", p );
+ }
+ }
+ return 0;
+}
+/*
+ * @brief Êý¾Ý·¢ËÍ´¦Àí
+ * @retval none
+ */
+typedef struct{
+ char *data;
+ uint32_t len;
+}tcp_tx_data_t;
+
+static bool tcp_send_handler(at_work_ctx_t *e)
+{
+
+ tcp_tx_data_t *p_tx_data = (tcp_tx_data_t *)e->params;
+
+
+ e->printf( e, "AT+CIPSEND=%d,%d\r\n", 0, p_tx_data->len );
+
+ if (e->wait_resp(e, ">", 5000) != AT_RET_OK) { //µÈ´ýÌáʾ·û
+ goto Error;
+ }
+ e->write(e, p_tx_data->data, p_tx_data->len );
+
+ if (e->wait_resp(e, "SEND OK", 5000) != AT_RET_OK) { //µÈ´ýÌáʾ·û
+ goto Error;
+ }
+ return true;
+Error:
+ return false;
+}
+
+/**
+ * @brief socket Êý¾Ý·¢ËÍ
+ * @param[in] s - socket
+ * @param[in] buf - Êý¾Ý»º³åÇø
+ * @param[in] len - »º³åÇø³¤¶È
+ */
+
+static bool wifi_tcp_send( const void *buf, unsigned int len)
+{
+ tcp_tx_data_t tx_data;
+ tx_data.data = (char *)buf;
+ tx_data.len = len;
+
+ if (len == 0)
+ return false;
+ return at_do_work(&wifi_at, (at_work)tcp_send_handler, &tx_data);
+}
+
+int wifi_init(at_obj_t *at )
+{
+ //½ÓÊÕ»º³åÇø
+ char recvbuf[32];
+ //ATÓ¦´ð
+ at_respond_t r = {"OK", NULL, sizeof(recvbuf), 3000};
+ r.recvbuf = recvbuf;
+ //
+ if (at_do_cmd(at, &r, "AT+CWMODE=2") != AT_RET_OK)
+ return -1;
+
+ else if (at_do_cmd(at, &r, "AT+CIPMUX=1") != AT_RET_OK)
+ return -1;
+ else if (at_do_cmd(at, &r, "AT+CIPAP=\"192.168.4.1\"") != AT_RET_OK)
+ return -1; //AT+CIPAP=<"ip">[,<"gateway">,<"netmask">]
+ else if (at_do_cmd(at, &r, "AT+CWDHCPS=1,2880,\"192.168.4.4\",\"192.168.4.10\"" ) != AT_RET_OK)
+ return -1; //AT+CWDHCPS=<enable>,<lease time>,<start IP>,<end IP>
+ else if (at_do_cmd(at, &r, "AT+CWSAP=\"ESP32_softAP\",\"12345678\",5,3") != AT_RET_OK)
+ return -1;
+ else if (at_do_cmd(at, &r, "AT+CIPSERVER=1,8888") != AT_RET_OK)
+ return -1; //AT+CIPSERVER=1
+
+ printf( "wifi init ok\n" );
+ return 0;
+}
+
+void at_task(void *pvParameters)
+{
+ at_obj_init( &wifi_at, &wifi_adap );
+ while(1){
+ at_process(&wifi_at);
+ vTaskDelay(10);
+
+ }
+}
+
+void at_task_start( void )
+{
+ xTaskCreate(at_task, "AT", (128), 0, 1, &AtTask_Handler); //WifiÈÎÎñº¯Êý
+}
+
+void wifi_task(void *pvParameters)
+{
+ at_task_start();
+ vTaskDelay(2000);
+
+ wifi_init( &wifi_at);
+
+ while(1){
+ vTaskDelay(10000);
+ //read_wifi_mode(&wifi_at);
+ printf( "wifi test\n" );
+ wifi_tcp_send( "ABCDE-12345", 11 );
+ }
+}
--- /dev/null
+#ifndef __WIFI_H
+#define __WIFI_H
+
+#include "FreeRTOS.h"
+#include "task.h"
+
+
+//µç³ØµçÁ¿¼ì²âÈÎÎñ¾ä±ú
+extern TaskHandle_t WifiTask_Handler;
+void wifi_task(void *pvParameters);
+
+#endif
/* FreeRTOSÓëÄÚ´æÉêÇëÓйØÅäÖÃÑ¡Ïî */
/***************************************************************************************************************/
#define configSUPPORT_DYNAMIC_ALLOCATION 1 //Ö§³Ö¶¯Ì¬ÄÚ´æÉêÇë
-#define configTOTAL_HEAP_SIZE ((size_t)(20*1024)) //ϵͳËùÓÐ×ܵĶѴóС
+#define configTOTAL_HEAP_SIZE ((size_t)(26*1024)) //ϵͳËùÓÐ×ܵĶѴóС
/***************************************************************************************************************/
/* FreeRTOSÓë¹³×Óº¯ÊýÓйصÄÅäÖÃÑ¡Ïî */
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB1Periph_UART5|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE); //ʹÄÜUSART5£¬GPIOC, GPIODʱÖÓ
-
- //USART5_TX GPIOC.12
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //PC.12
+ RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
+ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
+ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
+ //UART5_TX GPIOC.12
+ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
- GPIO_Init(GPIOC, &GPIO_InitStructure);//³õʼ»¯GPIOC.12
+ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
+ GPIO_Init(GPIOC, &GPIO_InitStructure);
- //USART5_RX GPIOD.2³õʼ»¯
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;//PD.2
+ //UART5_RX GPIOD.2³õʼ»¯
+ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë
- GPIO_Init(GPIOD, &GPIO_InitStructure);//³õʼ»¯GPIOD.2
+ GPIO_Init(GPIOD, &GPIO_InitStructure);
- //Usart5 NVIC ÅäÖÃ
+ //Uart5 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//ÇÀÕ¼ÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×ÓÓÅÏȼ¶3
}
}
-//USART5½ÓÊÕµ¥×Ö½Úº¯Êý
-u8 USART5_RecvByte(void)
-{
- u8 data;
- //µÈ´ý¿ÉÒÔ½ÓÊÕÊý¾Ý
- while( SET != USART_GetFlagStatus (UART5, USART_FLAG_RXNE) );
- //½ÓÊÕ±¾´ÎÊý¾Ý
- data = USART_ReceiveData (UART5);
- return data;
-}
-
-//USART5½ÓÊÕ×Ö·û´®º¯Êý
-void USART5_RecvStr(u8 *buf)
-{
- u8 data;
- do
- {
- data = USART5_RecvByte();
- *buf = data;
- buf ++;
- }
- while(data != '\0' && data != '\n');
- *buf = '\0';
-}
-
-void USART5_IRQHandler(void) //´®¿Ú5ÖжϷþÎñ³ÌÐò
+extern void wifi_uart_rx_byte_cb( uint8_t byte );
+void UART5_IRQHandler(void) //´®¿Ú5ÖжϷþÎñ³ÌÐò
{
u8 Res;
//ÅжÏÊÇÒòΪ½ÓÊÕ¶ø½øÈëÖжÏ
if((USART_GetFlagStatus(UART5,USART_FLAG_RXNE)) == !RESET)
{
- Res=USART_ReceiveData(UART5);
- if(UART5_RX_CNT<sizeof(UART5_RX_BUF)) //Ò»´ÎÖ»ÄܽÓÊÕ64¸ö×Ö½Ú£¬ÈËΪÉ趨£¬¿ÉÒÔ¸ü´ó£¬µ«ÀË·Ñʱ¼ä
- {
- UART5_RX_BUF[UART5_RX_CNT]=Res; //¼Ç¼½ÓÊÕµ½µÄÖµ
- UART5_RX_CNT++; //ʹÊÕÊý¾ÝÔö¼Ó1
- }
- }
-}
-
-//¼ÆËã½ÓÊÕµÄÊý¾Ý³¤¶È£¬²¢ÇÒ½«Êý¾Ý·Åµ½*bufÊý×éÖÐ
-u16 Uart5Read(u8 *buf, u16 len)
-{
- u16 i;
- if(len>UART5_RX_CNT) //Ö¸¶¨¶ÁÈ¡³¤¶È´óÓÚʵ¼Ê½ÓÊÕµ½µÄÊý¾Ý³¤¶Èʱ
- {
- len=UART5_RX_CNT; //¶ÁÈ¡³¤¶ÈÉèÖÃΪʵ¼Ê½ÓÊÕµ½µÄÊý¾Ý³¤¶È
- }
- for(i=0;i<len;i++) //¿½±´½ÓÊÕµ½µÄÊý¾Ýµ½½ÓÊÕÖ¸ÕëÖÐ
- {
- *buf=UART5_RX_BUF[i]; //½«Êý¾Ý¸´ÖƵ½bufÖÐ
- buf++;
+ Res=USART_ReceiveData(UART5);
+ wifi_uart_rx_byte_cb( Res );
}
- UART5_RX_CNT=0; //½ÓÊÕ¼ÆÊýÆ÷ÇåÁã
- return len; //·µ»ØÊµ¼Ê¶ÁÈ¡³¤¶È
}
//¼ÆËã·¢Ë͵ÄÊý¾Ý³¤¶È£¬²¢ÇÒ½«Êý¾Ý·¢ËÍ
void Uart1Write(u8 *buf ,u16 len) ; //´®¿Ú·¢ËÍÊý¾Ý
u16 Uart1Read(u8 *buf, u16 len) ; //´®¿Ú½ÓÊÕÊý¾Ý
-extern u8 UART5_RX_BUF[USART_REC_LEN]; //½ÓÊÕ»º³å,×î´óUSART_REC_LEN¸ö×Ö½Ú.Ä©×Ö½ÚΪ»»Ðзû
-extern u16 UART5_RX_STA; //½ÓÊÕ״̬±ê¼Ç
-extern u16 UART5_RX_CNT; //½ÓÊÕ×Ö½Ú¼ÆÊýÆ÷
void uart5_init(u32 bound);
void Uart5Write(u8 *buf ,u16 len) ; //´®¿Ú·¢ËÍÊý¾Ý
-u16 Uart5Read(u8 *buf, u16 len) ; //´®¿Ú½ÓÊÕÊý¾Ý
#endif
</File>
</Files>
</Group>
+ <Group>
+ <GroupName>APP_WIFI</GroupName>
+ <Files>
+ <File>
+ <FileName>wifi.c</FileName>
+ <FileType>1</FileType>
+ <FilePath>..\APP\wifi.c</FilePath>
+ </File>
+ <File>
+ <FileName>at.c</FileName>
+ <FileType>1</FileType>
+ <FilePath>..\APP\at.c</FilePath>
+ </File>
+ </Files>
+ </Group>
</Groups>
</Target>
</Targets>
#include "Exti.h"
#include "datastore.h"
+extern void wifi_uart_init( u32 br );
int main(void)
{
-
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//ÉèÖÃϵͳÖжÏÓÅÏȼ¶·Ö×é4
delay_init(); //³õʼ»¯ ÑÓʱº¯Êý
TIM3_Int_Init(1000-1,72-1); //³õʼ»¯ ¶¨Ê±Æ÷£¨1ms£©
--- /dev/null
+## 接触式传输数据协议
+
+### 一、 基本参数
+
+#### 1、 命令状态
+
+| 名称 | 值 | 描述 |
+| ------------- | --- | -------------- |
+| OK | 0 | 正常 |
+| InvalidJob | 1 | 无效的Job |
+| InvalidPset | 2 | 无效的Pset |
+| NoResult | 3 | 无拧紧数据 |
+| NoGraph | 4 | 无拧紧曲线 |
+| PullResult | 5 | 正在拧紧 |
+| PushResult | 6 | 正在推送数据 |
+| CRC | 7 | 校验和错误 |
+| InvalidData | 8 | 无效的数据 |
+| CmdNotFounded | 9 | 错误命令 |
+| ReadError | 10 | 读错误 |
+| WriteError | 11 | 写错误 |
+| TimeOut | 12 | 超时 |
+
+### 二、 协议
+
+#### 1、 读信息
+
+##### 1)、请求命令
+
+| 名称 | 值 | 描述 |
+|------|-----------|--------------------------|
+| MID | RSVI | 命令,长度4 |
+| VER | 01-99 | 版本,长度2 |
+| SN | 070035 | 序列号,长度10,此处可以空 |
+| TG | 01-99 | 组,长度2 |
+
+##### 2)、回应
+
+| 名称 | 值 | 描述 |
+|-----------------|-----------|------------------------------------------------|
+| MID | ASVI | 命令,长度4 |
+| VER | 01-99 | 版本,长度2 |
+| SN | 0700035 | 序列号,最大长度10 |
+| TG | 01-99 | 组,长度2 |
+| STATUS | OK | 命令状态 |
+| ToolName | SuperWrench | 工具名称 |
+| ToolType | 1 | 工具类型 SuperWrench:1, MasterWrench:2ClickWrench:3LightWrench:4 |
+| FirmWare | 1.1.1 | 固件版本 |
+| Span | 120 | 工具范围 |
+| TighteingingID | 10 | 拧紧编号 |
+
+示例:
+RSVI;1;;1;\r\n
+ASVI;1;0700035;1;OK;SuperWrench; 1;1.1.1;120;10;\r\n
+
+#### 2、 读状态
+
+##### 1)、请求命令
+
+| 名称 | 值 | 描述 |
+|------|-----------|------------|
+| MID | RSTS | 命令,长度4 |
+| VER | 01-99 | 版本,长度2|
+| SN | 0700035 | 序列号,长度10|
+| TG | 01-99 | 组,长度2 |
+
+##### 2)、回应
+
+| 名称 | 值 | 描述 |
+|---------------|-----|--------------------------------------|
+| MID | ASTS| 命令,长度4 |
+| VER | 01-99| 版本,长度2 |
+| SN | 0700035| 序列号,长度10 |
+| TG | 01-99| 组,长度2 |
+| STATUS | 0 | 命令状态 |
+| WrenchStatus | 0 | 工作状态 0:PowerOn 1:Pset 2: Job 3:Finish |
+| WrenchEnable | 0 | 使能状态0:禁用 1:使能 |
+| HaveResult | 0 | 1:有数据,0:无数据 |
+
+示例:
+RSTS;1; 0700035;1;\r\n
+ASTS;1; 0700035;0;1;0;1;1;\r\n
+
+#### 3、 读数据
+
+##### 1)、请求数据
+
+| 名称 | 值 | 描述 |
+|------|-----------|------------|
+| MID | RSTD | 命令,长度4 |
+| VER | 01-99 | 版本,长度2|
+| SN | 0700035 | 序列号,长度10|
+| TG | 01-99 | 组,长度2 |
+
+##### 2)、上传数据
+
+| 名称 | 值 | 描述 |
+|-----------------|------------------------|----------------------------------|
+| MID | ASTD | 命令,长度4 |
+| VER | 01-99 | 版本,长度2 |
+| SN | 0700035 | 序列号,长度10 |
+| TG | 01-99 | 组,长度2 |
+| STATUS | 0 | 命令状态 |
+| Reserve | | 保留 |
+| TighteningID | 1 | 拧紧数据编号,10位 |
+| VIN | 1234 | 条码 |
+| TimeStamp | 2019-10-10 12:12;12 | 时间 |
+| BatchSize | 10 | 次数 |
+| BatchCounter | 1 | 计数 |
+| PsetID | 1-250 | 参数号 |
+| PsetName | Pset1 | 参数名 |
+| TorqueMin | 2 | 扭矩最小值 |
+| TorqueTarget | 3 | 扭矩目标值 |
+| TorqueMax | 4 | 扭矩最大值 |
+| AngleMin | 0 | 角度最小值 |
+| AngleTarget | 0 | 角度目标值 |
+| AngleMax | 90 | 角度最大值 |
+| Status | 1 | 拧紧状态 0:NOK 1:OK |
+| TorqueStatus | 1 | 扭矩状态 0:Low 1:OK 2:High |
+| AngleStatus | 1 | 角度状态 0:Low 1:OK 2:High |
+| Torque | 3.5 | 扭矩 |
+| Angle | 60 | 角度 |
+
+示例:
+RSTD;1; 0700035;1;0;\r\n
+ASTD;1; 0700035;1;………..;\r\n
+
+#### 4、 选Job
+
+##### 1)、请求命令
+
+| 名称 | 值 | 描述 |
+|--------------|-----------|--------------------------------------|
+| MID | RSSP | 命令,长度4 |
+| VER | 01-99 | 版本,长度2 |
+| SN | 0700035 | 序列号,长度10 |
+| TG | 01-99 | 组,长度2 |
+| JobID | 01-250 | Job编号 |
+| JobName | Pset | Job名字 |
+| Strategy | 0 | 拧紧策略:TC:0 TC_AM:1 AC_TM:2 |
+| Rotation | 0 | 方向:CW:0 CCW:1 Auto:2 |
+| StartTorque | 1 | 起始扭矩 |
+| TorqueMin | 3 | 扭矩下限 |
+| TorqueTarget | 4 | 目标扭矩 |
+| TorqueMax | 5 | 扭矩上限 |
+| StartAngle | 2 | 扭矩门槛,开始计角度 |
+| AngleMin | 0 | 角度下限 |
+| AngleTarget | 0 | 角度目标值 |
+| AngleMax | 90 | 角度上限 |
+| BatchSize | 10 | 次数 |
+| CycleEndTime | 1 | 循环等待时间<br>0:0.1s 1:0.2s 2:0.5s 3:1s<br>4:2s 5:5s 6:10s 7:20s<br>8:30s 9:60s |
+| Distance | 0 | 补偿长度 mm |
+| Counter | 1 | 计数模式:0:OK 1:OKNOK |
+| Repeat | 0 | 模式 0:Lock,1:Repeat |
+| Repair | 0 | 返修次数:0-9 |
+
+##### 2)、回应
+
+| 名称 | 值 | 描述 |
+|--------|-----------|------------|
+| MID | ASSP | 命令,长度4 |
+| VER | 01-99 | 版本,长度2|
+| SN | 0700035 | 序列号,长度10|
+| TG | 01-99 | 组,长度2 |
+| STATUS | 0 | 命令状态 |
+
+示例:
+RSSP;1;0000000000;1;……………;\r\n
+ASSP;1;0000000000;1;0;\r\n
+
+#### 5、 使能工具
+
+##### 1)、请求命令
+
+| 名称 | 值 | 描述 |
+|--------|-----------|--------------------------|
+| MID | RSEN | 命令,长度4 |
+| VER | 01-99 | 版本,长度2 |
+| SN | 0700035 | 序列号,长度10 |
+| TG | 01-99 | 组,长度2 |
+| Enable | 0 | 0:禁用工具,1:使能工具 |
+
+##### 2)、回应
+
+| 名称 | 值 | 描述 |
+|--------|-----------|------------|
+| MID | ASEN | 命令,长度4 |
+| VER | 01-99 | 版本,长度2|
+| SN | 0700035 | 序列号,长度10|
+| TG | 01-99 | 组,长度2 |
+| STATUS | 0 | 命令状态 |
+
+示例:
+RSEN;1;0000000000;0;1;\r\n
+ASEN;1;0000000000;0;0;\r\n
+
+#### 6、 读取曲线
+
+##### 1)、请求数据
+
+| 名称 | 值 | 描述 |
+|------|-----------|------------|
+| MID | RSTG | 命令,长度4 |
+| VER | 01-99 | 版本,长度2|
+| SN | 0700035 | 序列号,长度10|
+| TG | 01-99 | 组,长度2 |
+
+##### 2)、上传数据
+
+| 名称 | 值 | 描述 |
+|-----------------|------------------------|--------------------------------------|
+| MID | ASTG | 命令,长度4 |
+| VER | 01-99 | 版本,长度2 |
+| SN | 0700035 | 序列号,长度10 |
+| TG | 01-99 | 组,长度2 |
+| STATUS | 0 | 命令状态 |
+| Reserve | | 保留 |
+| TighteningID | 1 | 拧紧数据编号,10位 |
+| VIN | 1234 | 条码 |
+| TimeStamp | 2019-10-10 12:12;12 | 时间 |
+| BatchSize | 10 | 次数 |
+| BatchCounter | 1 | 计数 |
+| PsetID | 1-250 | 参数号 |
+| PsetName | Pset1 | 参数名 |
+| TorqueMin | 2 | 扭矩最小值 |
+| TorqueTarget | 3 | 扭矩目标值 |
+| TorqueMax | 4 | 扭矩最大值 |
+| AngleMin | 0 | 角度最小值 |
+| AngleTarget | 0 | 角度目标值 |
+| AngleMax | 90 | 角度最大值 |
+| Status | 1 | 拧紧状态 0:NOK 1:OK |
+| TorqueStatus | 1 | 扭矩状态 0:Low 1:OK 2:High |
+| AngleStatus | 1 | 角度状态 0:Low 1:OK 2:High |
+| Torque | 3.5 | 扭矩 |
+| Angle | 60 | 角度 |
+| Mode | 0 | 曲线模式:0:无曲线;1:扭矩曲线2:角度曲线;3:扭矩+角度曲线 |
+| Samples | 500 | 采样数 |
+| Frequency | 1 | 采样频率 ms |
+| Torque Point | 2 | 扭矩小数位数 |
+| Points | | 曲线数据 |
+
+在基本参数(\r\n)后发送曲线数据(16进制),格式如下:
+扭矩+角度:TH TL AH AL………..
+扭矩:TH TL………..
+角度: AH AL………..
+无:省略
+TH:扭矩高位
+TL:扭矩低位
+AH:角度高位
+AL:角度低位
+