Advertisement
Guest User

rs485 for maple..

a guest
Dec 2nd, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.43 KB | None | 0 0
  1. #include <itoa.h>
  2.  
  3. #define pinRTS 27
  4.  
  5. /* Phone number ordering:  
  6.  0 = master
  7.  32 = relay board
  8.  99 = unset number
  9.  
  10.  0 = master
  11.  z = relay board (32)
  12.  x = unset number (99)
  13.  */
  14.  
  15. /* AT language set structure
  16.  
  17.  +++A T | COMMAND / PARAMETER | VALUE | #
  18.  
  19.  */
  20.  
  21. /* Examples:
  22.  Dialing 1:  +++ATD11### //Call 1
  23.  Dialing 99: +++ATD9999### // Call 11
  24.  Hangup:     +++ATHH###
  25.  Test if we are talking: +++ATZZ#
  26.  Set phone number to 02: +++ATSETPHONE22#
  27.  or: +++ATSETPHONE0022#
  28.  Set phone number to 25: +++ATSETPHONE2255#
  29.  Get values; like current phone number: +++ATGETPHONE
  30.  */
  31.  
  32. byte phoneNO;
  33. boolean connected;
  34. byte conState; /* 0 = listening; 1 = receivingCommand; 2 = executing; 3 = filetransfer */
  35. byte chCue[4];
  36. char receivedCom[32];
  37. boolean recError;
  38.  
  39. void setup() {
  40.   pinMode(pinRTS, OUTPUT);
  41.   digitalWrite(pinRTS, LOW);
  42.   Serial1.begin(115200);
  43.  
  44.   phoneNO = 99; //  Initial phoneNo is 99
  45.   receivedCom[0] = 0;
  46.   conState = 0;
  47.   connected = false;
  48.   recError = false;
  49.   chCue = {
  50.     0, 0, 0, 0  };
  51.  
  52.   while ( (USART1_BASE -> SR & USART_SR_TC) == 0);
  53.   delay(1200); // Settle for awhile
  54.   Serial1.flush();
  55.   outputMSG("Ready\r\n");
  56.   SerialUSB.println("Ready\r\n");
  57. }
  58.  
  59. void loop() {
  60.  
  61.   if ( Serial1.available() > 0 ) {
  62.     addChCue();
  63.     chCue[3] = Serial1.read();
  64.     /*  
  65.      SerialUSB.print("Something in buffer.. '");
  66.      SerialUSB.write(chCue[3]);
  67.      SerialUSB.println(" '");
  68.      */
  69.     if (( conState == 1 ) && ( chCue[3] == '#' )) // Command recording ends
  70.       conState = 2;
  71.  
  72.     if ( conState == 1 ) { // Record command
  73.       receivedCom[0]++;
  74.       if (( chCue[3] > 96 ) && ( chCue[3] < 123 ))
  75.         receivedCom[receivedCom[0]] = chCue[3] - 32;
  76.       else
  77.         receivedCom[receivedCom[0]] = chCue[3];
  78.  
  79.       if ( receivedCom[0] > 28 ) { // Command too long, we have error somewhere
  80.         receivedCom[0]--;
  81.         recError = true;
  82.         if ( !connected )
  83.           conState = 0;
  84.         SerialUSB.println("Error: Too long command.");
  85.       }
  86.  
  87.     }
  88.  
  89.     if ( conState == 2 ) { // Command recording ended - answering time
  90.       SerialUSB.println("Command recording ended..");
  91.       parseCommand();
  92.       conState = 0;
  93.       recError = false;
  94.       receivedCom[0] = 0;
  95.     }
  96.  
  97.     if (( chCue[0] && chCue[1] && chCue[2] == '+' ) && (( chCue[3] == 'A' ) || ( chCue[3] == 'a' ))) {
  98.       conState = 1;
  99.       receivedCom[0] = 1;
  100.       receivedCom[1] = 'A';
  101.       recError = false;
  102.       SerialUSB.println("Starting to record a command..");
  103.     }
  104.  
  105.   }
  106.  
  107. }
  108.  
  109. void addChCue() {
  110.   chCue[0] = chCue[1];
  111.   chCue[1] = chCue[2];
  112.   chCue[2] = chCue[3];
  113. }
  114.  
  115. byte getCommandValue(byte commandLen) {
  116.   if (( receivedCom[0] > commandLen + 4 ) || ( receivedCom[0] < commandLen + 2 )) {
  117.     recError = true;
  118.     return 255; // Garbage on command or command has no value
  119.   }
  120.  
  121.   if ( receivedCom[commandLen + 1] != receivedCom[commandLen + 2] ) {
  122.     recError = true;
  123.     return 254; // Mismatch
  124.   }
  125.  
  126.   if ( ( receivedCom[0] > commandLen + 2 ) && ( receivedCom[commandLen + 3] != receivedCom[commandLen + 4] )) {
  127.     recError = true;
  128.     return 254; // Mismatch
  129.   }
  130.  
  131.   if ( receivedCom[0] > commandLen + 2 )
  132.     return ((( receivedCom[commandLen + 1] - 48 ) * 10 ) + (receivedCom[commandLen + 3] - 48));
  133.  
  134.   return (receivedCom[commandLen + 1] - 48);
  135.  
  136. }
  137.  
  138. void parseCommand() {
  139.  
  140.   if (( receivedCom[0] == 0 ) || ( conState == 0 )) // Extra '#' to make sure command end mark is sent..
  141.     return;
  142.  
  143.   if (( receivedCom[0] >= 5 ) && ( receivedCom[0] < 8 ) && ( commandBegins("ATD")) && ( !recError ) && ( getCommandValue(3) == phoneNO )) {
  144.     connected = true;
  145.     outputMSG("OK#\r\n");
  146.     return;
  147.   }
  148.  
  149.   if ( !connected ) // We answer to the next commands only if we are connected
  150.       return;
  151.  
  152.   if ( recError ) {
  153.     outputMSG("ERR#\r\n");
  154.     return;
  155.   }
  156.  
  157.   SerialUSB.println(receivedCom);
  158.  
  159.   if (( receivedCom[0] >= 5 ) && ( receivedCom[0] < 8 ) && ( commandBegins("ATD")) && ( getCommandValue(3) != phoneNO )) {
  160.     connected = false; // master called somewhere else and forgot to hangup.. Let's try to be smart..
  161.     return;
  162.   }
  163.  
  164.   if (( receivedCom[0] == 4 ) && ( commandBegins("ATZZ"))) { // Always return OK to ATZZ, suites to test if we are connected
  165.     outputMSG("OK#\r\n");
  166.     return;
  167.   }
  168.  
  169.   if (( receivedCom[0] == 4 ) && ( commandBegins("ATHH"))) { // Hangup
  170.     SerialUSB.println("Hangup..");
  171.  
  172.     outputMSG("OK#\r\n");
  173.     connected = false;
  174.     return;
  175.   }
  176.  
  177.   if (( receivedCom[0] > 7 ) && ( commandBegins("ATSET"))) {
  178.     setCommand();
  179.     return;
  180.   }
  181.  
  182.   if (( receivedCom[0] > 7 ) && ( commandBegins("ATGET"))) {
  183.     getCommand();
  184.     return;
  185.   }
  186.  
  187.   outputMSG("UNK#\r\n"); // Unknown command
  188.  
  189. }
  190.  
  191. void setCommand() {
  192.  
  193.   SerialUSB.println("Setting values..");
  194.  
  195.   if (( receivedCom[0] >= 12 ) && ( receivedCom[0] < 15 ) && ( commandBegins("ATSETPHONE"))) { // Set new phone number
  196.     byte newNo = getCommandValue(10);
  197.  
  198.     if (( newNo < 32 ) && ( newNo > 0 )) { // Rule out relay board and master
  199.       phoneNO = newNo;
  200.       outputMSG("OK#\r\n");
  201.       return;
  202.     }
  203.  
  204.     outputMSG("ERR#\r\n");
  205.     return;
  206.   }
  207.  
  208.   outputMSG("UNK#\r\n"); // Unknown command
  209.  
  210. }
  211.  
  212. void getCommand() {
  213.  
  214.   SerialUSB.println("Getting values..");
  215.  
  216.     if (( receivedCom[0] == 10 ) && ( commandBegins("ATGETPHONE"))) { // Get phone no
  217.     outputVAL(phoneNO);
  218.     connected = false;
  219.     return;
  220.   }
  221.  
  222.   outputMSG("UNK#\r\n"); // Unknown command
  223.  
  224. }
  225.  
  226. void outputVAL(byte VAL) {
  227.   char buffer[4];
  228.   itoa(VAL, buffer, 10);
  229.  
  230.   digitalWrite(pinRTS,HIGH); // Set rs485 control pin high to enable writing to rs485  
  231.   Serial1.print("VAL:");
  232.   Serial1.print(buffer[0]); Serial1.print(buffer[0]);
  233.   if ( buffer[1] != 0 ) {
  234.     Serial1.print(buffer[1]); Serial1.print(buffer[1]);
  235.   }
  236.   Serial1.print("#\r\n");
  237.  
  238.   while ( (USART1_BASE -> SR & USART_SR_TC) == 0);
  239.   digitalWrite(pinRTS,LOW);
  240. }
  241.  
  242.  
  243. void outputMSG(char *MSG) {
  244.   byte c = 0;
  245.  
  246.   digitalWrite(pinRTS,HIGH); // Set rs485 control pin high to enable writing to rs485
  247.   while ( MSG[c] != 0 ) {
  248.     Serial1.write(MSG[c]);
  249.     c++;
  250.   }
  251.  
  252.   while ( (USART1_BASE -> SR & USART_SR_TC) == 0);
  253.   digitalWrite(pinRTS,LOW);
  254. }
  255.  
  256. boolean commandBegins(char *comparison)
  257. {
  258.  
  259.   byte c = 0;
  260.     byte b1 = receivedCom[0];
  261.   while ( comparison[c] != 0 )
  262.   {
  263.     if (( receivedCom[0] < c ) || ( receivedCom[c+1] != comparison[c] ))
  264.       return false;
  265.     c++;
  266.   }
  267.    
  268.   return true;
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement