Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * AX.25 protocol frame structure:
- * -------------------------------
- *
- * Flag Field: 8 bits (1 byte)
- * Header Field: 24 bits (3 bytes)
- * {
- * Destination Address: 8 bits (1 bytes) *
- * Source Address: 8 bits (1 bytes) *
- * Control Bits : 8 bits (1 byte ) * Control Bits will be (length of data-1) .
- * }
- * Data Field: 32-2048 bits (4-256 bytes)
- * Frame Checksum Field: 16 bits (2 bytes)
- * Flag Field: 8 bits (1 byte)
- *
- */
- #include <Crc16.h>
- Crc16 crc;
- unsigned char AX25_Flag = 0x7E; // flag = 0b01111110;
- unsigned char AX25_Header[3] = {0xAA,0xBB,0}; //0xAA is the GND Station Adress - 0xBB is the Sourse Address - the third byte will be data length-1
- unsigned char AX25_Data[256] = {0};
- unsigned char AX25_CheckSum[2] = {0};
- int inByte = 0;
- int command= 0;
- unsigned short value = 0;
- void setup()
- {
- // Serial1 : TX pin 18, RX pin 19
- // Serial2 : TX pin 16, RX pin 17
- // Serial1 will receive data from a device and resend it on Serial2 using AX25 protocol format
- Serial1.begin(9600);
- Serial2.begin(9600);
- }
- void loop()
- {
- // Wait until Command is received on Serial2 port
- while ( !(Serial2.available()) );
- // Read received data
- command = Serial2.read();
- // Wait until data is received on Serial2 port
- delay(2000);
- while ( !(Serial1.available()) );
- // Read received data
- Serial1.write(command);
- delay(500);
- while(!(Serial1.available()));
- inByte = Serial1.read();
- //calculate CRC
- crc.clearCrc();
- crc.updateCrc(inByte);
- value = crc.getCrc();
- // "value" is 2 bytes long
- // Get the lower byte and put it into AX25_CheckSum[1]
- // Get the higher byte and put it into AX25_CheckSum[0]
- AX25_CheckSum[0] = ( value >> 8 ) & 0xFF;
- AX25_CheckSum[1] = value & 0xFF;
- AX25_Header[2] = sizeof(inByte)/sizeof(unsigned char);
- AX25_Data[0] = inByte;
- // Send the data using AX.25 protocol format
- Serial2.write(AX25_Flag);
- Serial2.write(AX25_Header, 16);
- Serial2.write(AX25_Data, 256);
- Serial2.write(AX25_CheckSum, 2);
- Serial2.write(AX25_Flag);
- delay(500);
- while(true);
- }
- //Check routine taken from
- //http://web.mit.edu/6.115/www/miscfiles/amulet/amulet-help/xmodem.htm
- int calcrc(char *ptr, int count)
- {
- int crc;
- char i;
- crc = 0;
- while (--count >= 0)
- {
- crc = crc ^ (int) *ptr++ << 8;
- i = 8;
- do
- {
- if (crc & 0x8000)
- crc = crc << 1 ^ 0x1021;
- else
- crc = crc << 1;
- } while(--i);
- }
- return (crc);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement