Advertisement
Guest User

Untitled

a guest
May 25th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. /*
  2. Pi_Serial_test.cpp - SerialProtocol library - demo
  3. Copyright (c) 2014 NicoHood. All right reserved.
  4. Program to test serial communication
  5.  
  6. Compile with:
  7. sudo gcc -o Pi_Serial_Test.o Pi_Serial_Test.cpp -lwiringPi -DRaspberryPi -pedantic -Wall
  8. sudo ./Pi_Serial_Test.o
  9. */
  10.  
  11. // just that the Arduino IDE doesnt compile these files.
  12. #ifdef RaspberryPi
  13.  
  14. //include system librarys
  15. #include <stdio.h> //for printf
  16. #include <stdint.h> //uint8_t definitions
  17. #include <stdlib.h> //for exit(int);
  18. #include <string.h> //for errno
  19. #include <errno.h> //error output
  20. #include <stdarg.h> // va_*
  21.  
  22.  
  23. //wiring Pi
  24. #include <wiringPi.h>
  25. #include <wiringSerial.h>
  26.  
  27. // Find Serial device on Raspberry with ~ls /dev/tty*
  28. // ARDUINO_UNO "/dev/ttyACM0"
  29. // FTDI_PROGRAMMER "/dev/ttyUSB0"
  30. // HARDWARE_UART "/dev/ttyAMA0"
  31. char device[]= "/dev/ttyAMA0";
  32. // filedescriptor
  33. int fd;
  34. unsigned long baud = 115200;
  35. unsigned long time=0;
  36.  
  37. //prototypes
  38. int main(void);
  39. void loop(void);
  40. void setup(void);
  41.  
  42. void setup(){
  43.  
  44. printf("%s \n", "Raspberry Startup!");
  45. fflush(stdout);
  46.  
  47. //get filedescriptor
  48. if ((fd = serialOpen (device, baud)) < 0){
  49. fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
  50. exit(1); //error
  51. }
  52.  
  53. //setup GPIO in wiringPi mode
  54. if (wiringPiSetup () == -1){
  55. fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
  56. exit(1); //error
  57. }
  58.  
  59. }
  60.  
  61. char* concat(int count, ...)
  62. {
  63. va_list ap;
  64. int i;
  65.  
  66. // Find required length to store merged string
  67. int len = 1; // room for NULL
  68. va_start(ap, count);
  69. for(i=0 ; i<count ; i++)
  70. len += strlen(va_arg(ap, char*));
  71. va_end(ap);
  72.  
  73. // Allocate memory to concat strings
  74. char *merged = calloc(sizeof(char),len);
  75. int null_pos = 0;
  76.  
  77. // Actually concatenate strings
  78. va_start(ap, count);
  79. for(i=0 ; i<count ; i++)
  80. {
  81. char *s = va_arg(ap, char*);
  82. strcpy(merged+null_pos, s);
  83. null_pos += strlen(s);
  84. }
  85. va_end(ap);
  86.  
  87. return merged;
  88. }
  89.  
  90. char array[20];
  91. int count = 1;
  92. int tagRSSI = 0;
  93. int tagNumber =0;
  94.  
  95.  
  96. void loop(){
  97. // Pong every 3 seconds
  98. if(millis()-time>=3000){
  99. serialPuts (fd, "Pong!\n");
  100. // you can also write data from 0-255
  101. // 65 is in ASCII 'A'
  102. serialPutchar (fd, 65);
  103. time=millis();
  104. }
  105.  
  106. // read signal
  107. if(serialDataAvail (fd)){
  108. char newChar = serialGetchar (fd);
  109.  
  110. if(count == 20 && (unsigned int)newChar == 68)
  111. {
  112. tagRSSI = (unsigned int)array[5];
  113. char* wow = concat(3,array[8],array[9],array[10]);
  114. int number = (int)strtol(wow, NULL, 16);
  115. printf("tagRSSI = %d, tagNumber = %d", tagRSSI, number);
  116. count = 1;
  117. }
  118.  
  119. array[count] = newChar;
  120. //printf("%d\n", array[count]);
  121. count++;
  122. fflush(stdout);
  123. }
  124.  
  125. }
  126.  
  127. // main function for normal c++ programs on Raspberry
  128. int main(){
  129. setup();
  130. while(1) loop();
  131. return 0;
  132. }
  133.  
  134. #endif //#ifdef RaspberryPi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement