Advertisement
redsees

Untitled

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