serialTest.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * serialTest.c:
  3. * Very simple program to test the serial port. Expects
  4. * the port to be looped back to itself
  5. *
  6. * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
  7. ***********************************************************************
  8. * This file is part of wiringPi:
  9. * https://projects.drogon.net/raspberry-pi/wiringpi/
  10. *
  11. * wiringPi is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * wiringPi is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  23. ***********************************************************************
  24. */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <wiringPi.h>
  29. #include <wiringSerial.h>
  30. int main ()
  31. {
  32. int fd ;
  33. int count ;
  34. unsigned int nextTime ;
  35. if ((fd = serialOpen ("/dev/ttyAMA0", 115200)) < 0)
  36. {
  37. fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
  38. return 1 ;
  39. }
  40. if (wiringPiSetup () == -1)
  41. {
  42. fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
  43. return 1 ;
  44. }
  45. nextTime = millis () + 300 ;
  46. for (count = 0 ; count < 256 ; )
  47. {
  48. if (millis () > nextTime)
  49. {
  50. printf ("\nOut: %3d: ", count) ;
  51. fflush (stdout) ;
  52. serialPutchar (fd, count) ;
  53. nextTime += 300 ;
  54. ++count ;
  55. }
  56. delay (3) ;
  57. while (serialDataAvail (fd))
  58. {
  59. printf (" -> %3d", serialGetchar (fd)) ;
  60. fflush (stdout) ;
  61. }
  62. }
  63. printf ("\n") ;
  64. return 0 ;
  65. }