Advertisement
redsees

Untitled

Mar 26th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.49 KB | None | 0 0
  1. /*
  2.  *  AX.25 protocol frame structure:
  3.  *  -------------------------------
  4.  *
  5.  *  Flag Field: 8 bits (1 byte)
  6.  *  Header Field: 24 bits (3 bytes)
  7.  *      {
  8.  *    Destination Address: 8 bits (1 bytes) *
  9.  *    Source      Address: 8 bits (1 bytes) *
  10.  *    Control Bits       : 8  bits (1 byte ) * Control Bits will be  (length of data-1) .
  11.  *  }
  12.  *  Data Field: 32-2048 bits (4-256 bytes)
  13.  *  Frame Checksum Field: 16 bits (2 bytes)
  14.  *  Flag Field: 8 bits (1 byte)
  15.  *
  16.  */
  17.  
  18. #include <Crc16.h>
  19. Crc16 crc;
  20.  
  21. unsigned char AX25_Flag = 0x7E;    // flag = 0b01111110;
  22. 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
  23. unsigned char AX25_Data[256]   = {0};
  24. unsigned char AX25_CheckSum[2] = {0};
  25.  
  26. int inByte = 0;
  27. int command= 0;
  28. unsigned short value = 0;
  29.  
  30. void setup()
  31. {
  32.   // Serial1 : TX pin 18, RX pin 19
  33.   // Serial2 : TX pin 16, RX pin 17
  34.   // Serial1 will receive data from a device and resend it on Serial2 using AX25 protocol format
  35.  
  36.   Serial1.begin(9600);
  37.   Serial2.begin(9600);
  38.  
  39. }
  40.  
  41. void loop()
  42. {
  43.  
  44.   // Wait until Command is received on Serial2 port
  45.   while ( !(Serial2.available()) );
  46.   // Read received data
  47.   command = Serial2.read();
  48.   // Wait until data is received on Serial2 port
  49.   delay(2000);
  50.   while ( !(Serial1.available()) );
  51.   // Read received data
  52.   Serial1.write(command);
  53.  
  54.   delay(500);
  55.   while(!(Serial1.available()));
  56.   inByte = Serial1.read();
  57.  
  58.   //calculate CRC
  59.   crc.clearCrc();
  60.   crc.updateCrc(inByte);
  61.   value = crc.getCrc();
  62.  
  63.   // "value" is 2 bytes long
  64.   // Get the lower byte and put it into AX25_CheckSum[1]
  65.   // Get the higher byte and put it into AX25_CheckSum[0]
  66.   AX25_CheckSum[0] = ( value >> 8 ) & 0xFF;
  67.   AX25_CheckSum[1] = value & 0xFF;
  68.   AX25_Header[2] = sizeof(inByte)/sizeof(unsigned char);
  69.   AX25_Data[0] = inByte;
  70.  
  71.   // Send the data using AX.25 protocol format
  72.   Serial2.write(AX25_Flag);
  73.   Serial2.write(AX25_Header, 16);
  74.   Serial2.write(AX25_Data, 256);
  75.   Serial2.write(AX25_CheckSum, 2);
  76.   Serial2.write(AX25_Flag);
  77.  
  78.   delay(500);
  79.  
  80.     while(true);
  81. }
  82. //Check routine taken from
  83. //http://web.mit.edu/6.115/www/miscfiles/amulet/amulet-help/xmodem.htm
  84. int calcrc(char *ptr, int count)
  85. {
  86.     int  crc;
  87.     char i;
  88.     crc = 0;
  89.     while (--count >= 0)
  90.     {
  91.         crc = crc ^ (int) *ptr++ << 8;
  92.         i = 8;
  93.         do
  94.         {
  95.             if (crc & 0x8000)
  96.                 crc = crc << 1 ^ 0x1021;
  97.             else
  98.                 crc = crc << 1;
  99.         } while(--i);
  100.     }
  101.     return (crc);
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement