کد:
/*
# Be name Khoda!
# A Mirf example to test the latency between two Ardunio.
# Company : AftabRayaneh
# Website : http://shop.aftabrayaneh.com
# Developers : Reza Najafi, Elham Rasekh
# Date : 09.09.2013
# Version : 3.0
* Pins:
* Hardware SPI:
* MISO -> 12
* MOSI -> 11
* SCK -> 13
*
* Configurable:
* CE -> 8
* CSN -> 7
*/
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
const int MAX_BUFFER_LEN = 16;
//byte Address[5]={0x43,0x34,0x10,0x11,0x11};
//String Receive_Address = (String *) Address; //A name that is dedicated to this station; MAX is 5 Char.
//String Send_Address = Address; //A name that is dedicated to another station; MAX is 5 Char.
String strMirfBuffer="";
byte Transfer_Lock=0;
void setup()
{
Serial.begin(9600);
Setup_Mirf(); // initializing overall needed...
sendData("TALK");
}
void loop()
{
if (!Mirf.isSending())
check_MirfBuffer();
read_SerialBuffer();
}
//This function checks mirf data availability. if data is available, put it in the buffer and generates an event.
void check_MirfBuffer()
{
if (Mirf.dataReady())
{
while(Mirf.dataReady())
{
int tmp_index=0;
byte tmp_buffer[4]={0};
Mirf.getData(tmp_buffer);
while(tmp_buffer[tmp_index] && tmp_index<4 )
{
strMirfBuffer = strMirfBuffer + char(tmp_buffer[tmp_index]);
tmp_index++;
}
if (strMirfBuffer.length() >= MAX_BUFFER_LEN )
buffer_ready();
if (!Mirf.dataReady())
delay(2);
}
}
else if (strMirfBuffer.length()>0 )
buffer_ready();
}
//This function sends a 4 byte packet
void sendPacket(char packet[5])
{
Mirf.send((byte *)packet);
while(Mirf.isSending());
}
//This function sends data
void sendData(String strData)
{
int StrLen=0;
String tmp_Str="";
Serial.print("sending data: ");
Serial.print(strData);
Transfer_Lock=1;
StrLen=strData.length();
Serial.print(" : ");
Serial.println(StrLen);
//parsing the data to 4 character packets (byte *)packet
while (StrLen>0)
{
char tmp_Char[5]={0};
if(StrLen>4)
{
tmp_Str=strData.substring(0,4);
strData=strData.substring(4);
}
else
{
tmp_Str=strData;
strData="";
}
tmp_Str.toCharArray(tmp_Char, 5);;
sendPacket(tmp_Char);
StrLen=strData.length();
}
Transfer_Lock=0;
}
//This function parses received commands from another nrfs.
void buffer_ready()
{
if (strMirfBuffer == "TALK")
{
sendData("HURRAY !");
Serial.println("Talking...");
}
//and parse other commands so on ...
else
{
Serial.print("<");
Serial.print(strMirfBuffer);
Serial.println(">");
}
clear_buffer();
}
//This function forms a buffer of serial data and send it
void read_SerialBuffer()
{
if(Serial.available())
{
char CharData;
String StrSerialBuffer="";
while (Serial.available() && StrSerialBuffer.length() < MAX_BUFFER_LEN )
{
CharData=Serial.read();
StrSerialBuffer=StrSerialBuffer + CharData;
if (!Serial.available())
delay(1);
}
while (Transfer_Lock); //wait for transfer process
sendData(StrSerialBuffer);
}
}
//This function initializes overall needed for wireless communication
void Setup_Mirf()
{
char Receive_Adr_Char[6]={0x43,0x34,0x10,0x11,0x11,0x00};
char Send_Adr_Char[6]={0x43,0x34,0x10,0x11,0x11,0x00};
//Receive_Address.toCharArray(Receive_Adr_Char, 6);
//Send_Address.toCharArray(Send_Adr_Char, 6);
Mirf.spi = &MirfHardwareSpi;
Mirf.init();
Mirf.setRADDR((byte *)Receive_Adr_Char);
Mirf.setTADDR((byte *)Send_Adr_Char);
Mirf.payload = sizeof(unsigned long);
Mirf.channel = 0x28;
Mirf.configRegister(RF_SETUP, 0x07);
Mirf.config();
//reg - Start
byte rf_setup = 0;
Mirf.readRegister( RF_SETUP, &rf_setup, sizeof(rf_setup) );
if (rf_setup != 0)
Serial.println("Beginning ... ");
else
{
Serial.println( "Wireless did not initialize!" );
while (rf_setup==0);
}
delay(100);
}
//This function clears the buffer
void clear_buffer ()
{
strMirfBuffer="";
}
خروجیArduino برای ارسال کارکترهای b,c,d,e,f از کامپیوتر (البته بعد از چندین بار ارسال!)