Advertisement
MostafaKareem

AX.25 1

Mar 26th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 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. inByte = Serial1.read();
  54.  
  55. //calculate CRC
  56. crc.clearCrc();
  57. crc.updateCrc(inByte);
  58. value = crc.getCrc();
  59.  
  60. // "value" is 2 bytes long
  61. // Get the lower byte and put it into AX25_CheckSum[1]
  62. // Get the higher byte and put it into AX25_CheckSum[0]
  63. AX25_CheckSum[0] = ( value >> 8 ) & 0xFF;
  64. AX25_CheckSum[1] = value & 0xFF;
  65.  
  66. AX25_Data[0] = inByte;
  67.  
  68. // Send the data using AX.25 protocol format
  69. Serial2.write(AX25_Flag);
  70. Serial2.write(AX25_Header, 16);
  71. Serial2.write(AX25_Data, 256);
  72. Serial2.write(AX25_CheckSum, 2);
  73. Serial2.write(AX25_Flag);
  74.  
  75. delay(500);
  76.  
  77. while(true);
  78. }
  79. //Check routine taken from
  80. //http://web.mit.edu/6.115/www/miscfiles/amulet/amulet-help/xmodem.htm
  81. int calcrc(char *ptr, int count)
  82. {
  83. int crc;
  84. char i;
  85. crc = 0;
  86. while (--count >= 0)
  87. {
  88. crc = crc ^ (int) *ptr++ << 8;
  89. i = 8;
  90. do
  91. {
  92. if (crc & 0x8000)
  93. crc = crc << 1 ^ 0x1021;
  94. else
  95. crc = crc << 1;
  96. } while(--i);
  97. }
  98. return (crc);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement