serialTest.c 1006 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include <wiringPi.h>
  11. #include <wiringSerial.h>
  12. int main ()
  13. {
  14. int fd ;
  15. int count ;
  16. unsigned int nextTime ;
  17. if ((fd = serialOpen ("/dev/ttyAMA0", 115200)) < 0)
  18. {
  19. fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
  20. return 1 ;
  21. }
  22. if (wiringPiSetup () == -1)
  23. {
  24. fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
  25. return 1 ;
  26. }
  27. nextTime = millis () + 300 ;
  28. for (count = 0 ; count < 256 ; )
  29. {
  30. if (millis () > nextTime)
  31. {
  32. printf ("\nOut: %3d: ", count) ;
  33. fflush (stdout) ;
  34. serialPutchar (fd, count) ;
  35. nextTime += 300 ;
  36. ++count ;
  37. }
  38. delay (3) ;
  39. while (serialDataAvail (fd))
  40. {
  41. printf (" -> %3d", serialGetchar (fd)) ;
  42. fflush (stdout) ;
  43. }
  44. }
  45. printf ("\n") ;
  46. return 0 ;
  47. }