Very simple test program that transmits a message at regular intervals over an RS485 datalink. Turns off the transmitter 600 microseconds after the last byte in the transmission is put into the TX buffer. This may need adjusting. A much better solution would be to read the register that tells you when the transmitter is empty but I haven't figured this out yet.

Dependencies:   mbed

Fork of RS485Test by Richard Ellingworth

Revision:
1:0e3453d2d0cc
Parent:
0:0db8165396ae
--- a/main.cpp	Mon Nov 05 12:27:11 2012 +0000
+++ b/main.cpp	Mon Nov 05 20:57:14 2012 +0000
@@ -3,8 +3,9 @@
  *
  * Test program to see if a 485 communications link is working.
  * This is designed to run on the LPC11U24 which only has a single
- * serial port which uses pin 9 for TX and pin 10 for RX. Note that
- * this is the same serial port that transmits over the USB cable to the PC.
+ * serial port. This program uses pin 9 for TX and pin 10 for RX.
+ * There is only one serial port on the LPC11U24 so you cannot use
+ * the USB serial port to the development PC at the same time.
  *
  */
  
@@ -20,13 +21,13 @@
     // and simultaneously disable the received.
     DigitalOut txEnable( p11 );
     txEnable = 0;
-    // Serial port to PC and also to pins 9 and 10.
-    Serial pc( USBTX, USBRX );
+    // Serial port on pins 9 (TX) and 10 (RX).
+    Serial port485( p9, p10 );
     // Set baud rate and protocol.
-    pc.baud( 38400 );
-    pc.format( 8, Serial::None, 1 );
+    port485.baud( 38400 );
+    port485.format( 8, Serial::None, 1 );
     // Message to send.
-    char message[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n";
+    char message[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
     // Do following until power goes off.
     while( true ) {
         // Toggle state of LED.
@@ -36,12 +37,12 @@
         // Transmit a message.
         char *ptr = message;
         while( *ptr != 0 ) {
-            if( pc.writeable() ) {
-                pc.putc( *ptr++ );
+            if( port485.writeable() ) {
+                port485.putc( *ptr++ );
             }
         }
-        // Wait 300 micro seconds to let last byte transmit.
-        wait_us( 300 );
+        // Wait a bit to let last byte transmit.
+        wait_us( 600 );
         // Turn off the transmitter and enable the receiver.
         txEnable = 0;
         // Wait 1 second.