XBEE or Peer Serial IO Control Protocol Example
hi all,
i've finished writing 2-byte / 4-byte serial io control protocol. been testing , debugging on arduino mega. plan implement xbee @ serial control project although works 2-serially connected arduiono's. i'd highly suggest using mega ( due multiple ports allowing debug messages ).
anyways, wanted share in case else looking same thing. fresh , may still have bugs - please mention them if find them.
below quick code snippet of attached code.
(update) i've uploaded https://github.com/tgit23/peerioserialcontrol library
i've finished writing 2-byte / 4-byte serial io control protocol. been testing , debugging on arduino mega. plan implement xbee @ serial control project although works 2-serially connected arduiono's. i'd highly suggest using mega ( due multiple ports allowing debug messages ).
anyways, wanted share in case else looking same thing. fresh , may still have bugs - please mention them if find them.
below quick code snippet of attached code.
(update) i've uploaded https://github.com/tgit23/peerioserialcontrol library
code: [select]
/**************************************************************************************************
arduino sketch peer i/o control of arduino boards through serial.
- sketch uses byte level protocol; not serial monitor text control
usage:
- connect serial communications between multiple arduino's
- set 'arduinoid' variable (line #63) below unique id each arduino
- upload same sketch every arduino connected
[[[ serial control communication protocol ]]]
byte | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | - bit
0 | | d/a | r/w | s/r | id3 | id2 | id1 | id0 | - arduinoid
1 | | h/l | pin5 | pin4 | pin3 | pin2 | pin1 | pin0 | - pin number (6d/7a-bits)
2 | | av6 | av5 | av4 | av3 | av2 | av1 | av0 | - analog value (0-6)
3 | 1 | av13 | av12 | av11 | av10 | av9 | av8 | av7 | - analog value (7-13)
using (4) bytes represent possible scenario's follows:
- d/a = digital or analog flag
- r/w = read or write flag
- h/l = high or low flag ( used on digital; set d/a flag )
- id(4-bits) = arduino board identifier ( range 0-15 )
- s/r = send or reply flag
- pin(6-bits) = arduino pin number ( range 0-63 digital or 0-127 analog )
- av(14-bits) = analog value ( range 0-16,382 )
- end of packet flag 'end_bit' bit-7 on of 4-bytes.
byte data 7-bit unless end_bit flags end of packet
- when not using analog values last 2 bytes omitted ( 2-byte communications )
*****************************************************************************************************/
#define id_mask 0x0f // bytes[0] [0000 1111] arduinoid ( 0-15 )
#define reply_bit 4 // bytes[0] [0001 0000] reply-1, send-0
#define rw_bit 5 // bytes[0] [0010 0000] read-1, write-0
#define da_bit 6 // bytes[0] [0100 0000] digital-1, analog-0
#define dpin_mask 0x3f // bytes[1] [0011 1111] digital pins ( 0 - 63 )
#define apin_mask 0x7f // bytes[1] [0111 1111] analog pins ( 0 - 127 )
#define hl_bit 6 // bytes[1] [0100 0000] high-1, low-0
#define end_bit 7 // bytes[?} [1000 0000] set 8th bit flags end-of-packet
#define read 1
#define write 0
#define digital 1
#define analog 0
//-----------------------------------------------------------------------------------------------------
// -----------------------[ settings ]-----------------------------------------------------------
//-----------------------------------------------------------------------------------------------------
#define debug 1 // set 1 debug messages ( multi-serial port arduinos )
#if debug>0
#define dbl(x) serial.println x
#define db(x) serial.print x
#define dbc serial.print(",")
#else
#define dbl(x)
#define db(x)
#define dbc
#endif
int arduinoid = 12; // range = 0 15
//-----------------------------------------------------------------------------------------------------
int bytes[4] = { 0,0,0,0 };
struct ubuffer {
byte cbytes[4];
int creply = -1;
} buff[10];
int bi = 0; // command buffer index
int idx = 0; // byte counter
//-----------------------------------------------------------------------------------------------------
// sendbytes()
//-----------------------------------------------------------------------------------------------------
void sendbytes() {
db(("sendbytes( "));
serial1.write(bytes[0]);
serial1.write(bytes[1]);
if ( bytes[2] != 0 ) serial1.write(bytes[2]);
if ( bytes[3] != 0 ) serial1.write(bytes[3]);
db((bytes[0],hex));dbc;db((bytes[1],hex));dbc;db((bytes[2],hex));dbc;db((bytes[3],hex));dbl((" )"));
for(int i=0;i<4;i++) buff[bi].cbytes[i]=bytes[i];
}
//-----------------------------------------------------------------------------------------------------
// serialevent() - event driven functions
//-----------------------------------------------------------------------------------------------------
void serialevent() {
if (serial.available()) {
char rbyte = serial.read();
if ( rbyte == 'a' ) {
sendcommand(13,digital,read,3);
}
if ( rbyte == 's' ) {
sendcommand(13,digital,write,3,high);
}
}
}
void serialevent1() {
// receive bytes
if (serial1.available()) {
bytes[idx] = serial1.read();
if ( bytes[idx] != -1 ) {
//dbl((bytes[idx],hex));
if ( bitread(bytes[idx],end_bit) ) {
dbl(("-----------------------------------------------------------"));
db(("packet received @ size: "));dbl((idx+1));
idx = 0;
processpacket();
} else {
idx++;
}
}
}
}
//-----------------------------------------------------------------------------------------------------
// setup() & loop()
//-----------------------------------------------------------------------------------------------------
void setup() {
serial.begin(9600);
serial1.begin(9600);
}
void loop () {
if (buff[bi].creply != -1) {
db(("reply received : "));dbl((buff[bi].creply));
buff[bi].creply = -1;
}
}
quote
- pin(6-bits) = arduino pin number ( range 0-63 digital or 0-127 analog )useful arduinos have 127 analog pins. models they?
but, i'm curious how got 127 discrete values in 6 bits. and, i'm curious how distinguish, in 6 bits, whether pin analog or digital.
Arduino Forum > Using Arduino > Networking, Protocols, and Devices (Moderator: fabioc84) > XBEE or Peer Serial IO Control Protocol Example
arduino
Comments
Post a Comment