Advertisement
MostafaKareem

CSS with Simplified AX25

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