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