serial.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2016-17, Universidad de Almeria
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*Beginning of Auto generated code by Atmel studio */
36 #include <Arduino.h>
37 /*End of auto generated code by Atmel studio */
38 
39 #include "arduinodaq2pc-structs.h"
41 
42 // Frame format: see README.md
43 uint8_t rx_buf_len = 0;
44 uint8_t rx_buf[30];
45 
47 {
48  rx_buf_len = 0;
49 }
50 
52 {
53  while (Serial.available())
54  {
55  const uint8_t b = Serial.read();
56 
57  // sanity:
58  if (rx_buf_len==0)
59  if (b!=FRAME_START_FLAG) {
60  reset_rx_buf();
62  continue;
63  }
64 
65  // store:
66  rx_buf[rx_buf_len++] = b;
67 
68  //char buf[10];
69  //sprintf(buf,"rx: 0x%02X\n",b);
70  //Serial.print(buf);
71 
72  if (rx_buf_len==5+rx_buf[2])
73  {
74  // Check if we have a full frame:
76  reset_rx_buf();
78  continue;
79  }
80  const uint8_t opcode = rx_buf[1];
81  const uint8_t datalen = rx_buf[2];
82  const uint8_t *data = rx_buf+3;
83 
84  // chksum:
85  uint8_t chksum = 0;
86  for (uint8_t i=0;i<datalen;i++) chksum+=data[i];
87  if (rx_buf[rx_buf_len-2]!=chksum) {
88  reset_rx_buf();
90  continue;
91  }
92 
93  // 100% sure: we have a valid frame: dispatch it:
94  process_command(opcode,datalen,data);
95 
96  reset_rx_buf();
97  }
98  }
99 }
100 
void process_command(const uint8_t opcode, const uint8_t datalen, const uint8_t *data)
uint8_t rx_buf_len
Definition: serial.cpp:43
void send_simple_opcode_frame(const uint8_t op)
int const JOCTET unsigned int datalen
uint8_t rx_buf[30]
Definition: serial.cpp:44
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
#define FRAME_START_FLAG
void processIncommingPkts()
Definition: serial.cpp:51
GLdouble GLdouble GLdouble b
#define FRAME_END_FLAG
void reset_rx_buf()
Definition: serial.cpp:46


arduino_daq
Author(s):
autogenerated on Mon Jun 10 2019 12:46:03