Dynamixel controller with CAN interface

Dependencies:   AX12_final MX106_not_working comunication_1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialHalfDuplex.h Source File

SerialHalfDuplex.h

00001 
00002 #ifndef MBED_SERIALHALFDUPLEX_H
00003 #define MBED_SERIALHALFDUPLEX_H
00004  
00005 #include "device.h"
00006  
00007 #if DEVICE_SERIAL
00008  
00009 #include "Serial.h"
00010 #include "PinNames.h"
00011 #include "PeripheralNames.h"
00012  
00013 namespace mbed {
00014  
00015 /* Class: SerialHalfDuplex
00016  * A serial port (UART) for communication with other devices using  
00017  * Half-Duplex, allowing transmit and receive on a single
00018  * shared transmit and receive line. Only one end should be transmitting 
00019  * at a time.
00020  * 
00021  * Both the tx and rx pin should be defined, and wired together. 
00022  * This is in addition to them being wired to the other serial 
00023  * device to allow both read and write functions to operate.
00024  *
00025  *  Example:
00026  *  > // Send a byte to a second HalfDuplex device, and read the response
00027  *  >
00028  *  > #include "mbed.h"
00029  *  >
00030  *  > // p9 and p10 should be wired together to form "a"
00031  *  > // p28 and p27 should be wired together to form "b"
00032  *  > // p9/p10 should be wired to p28/p27 as the Half Duplex connection
00033  *  >
00034  *  > SerialHalfDuplex a(p9, p10);
00035  *  > SerialHalfDuplex b(p28, p27);
00036  *  >
00037  *  > void b_rx() { // second device response
00038  *  >     b.putc(b.getc() + 4);
00039  *  > }
00040  *  >   
00041  *  > int main() {
00042  *  >     b.attach(&b_rx);
00043  *  >     for(int c = 'A'; c < 'Z'; c++) {
00044  *  >         a.putc(c);
00045  *  >         printf("sent [%c]\n", c);
00046  *  >         wait(0.5);   // b should respond
00047  *  >         if(a.readable()) {
00048  *  >             printf("received [%c]\n", a.getc());
00049  *  >         }
00050  *  >     }
00051  *  > }
00052  * 
00053  * For Simplex and Full-Duplex Serial communication, see <Serial>
00054  */
00055 class SerialHalfDuplex : public Serial {
00056  
00057 public:
00058     /* Constructor: SerialHalfDuplex
00059      * Create a half-duplex serial port, connected to the specified transmit
00060      * and receive pins.
00061      *
00062      * These pins should be wired together, as well as to the target device
00063      *
00064      * Variables:
00065      *  tx - Transmit pin
00066      *  rx - Receive pin
00067      */
00068     SerialHalfDuplex(PinName tx, PinName rx, const char *name = NULL);
00069  
00070 #if 0       // Inherited from Serial class, for documentation
00071     /* Function: baud
00072      *  Set the baud rate of the serial port
00073      *
00074      * Variables:
00075      *  baudrate - The baudrate of the serial port (default = 9600).
00076      */
00077     void baud(int baudrate);
00078  
00079     enum Parity {
00080         None = 0
00081         , Odd
00082         , Even
00083         , Forced1
00084         , Forced0
00085     };
00086  
00087     /* Function: format
00088      *  Set the transmission format used by the Serial port
00089      *
00090      * Variables:
00091      *  bits - The number of bits in a word (5-8; default = 8)
00092      *  parity - The parity used (Serial::None, Serial::Odd, 
00093 Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None)
00094      *  stop - The number of stop bits (1 or 2; default = 1)
00095      */
00096     void format(int bits = 8, Parity parity = Serial::None, int stop_bits 
00097 = 1);
00098  
00099     /* Function: putc
00100      *  Write a character
00101      *
00102      * Variables:
00103      *  c - The character to write to the serial port
00104      */
00105     int putc(int c);
00106  
00107     /* Function: getc
00108      *  Read a character
00109      *
00110      * Read a character from the serial port. This call will block
00111      * until a character is available. For testing if a character is
00112      * available for reading, see <readable>.
00113      *
00114      * Variables:
00115      *  returns - The character read from the serial port
00116      */
00117     int getc();
00118  
00119     /* Function: printf
00120      *  Write a formated string
00121      *
00122      * Variables:
00123      *  format - A printf-style format string, followed by the
00124      *      variables to use in formating the string.
00125      */
00126     int printf(const char* format, ...);
00127  
00128     /* Function: scanf
00129      *  Read a formated string
00130      *
00131      * Variables:
00132      *  format - A scanf-style format string,
00133      *      followed by the pointers to variables to store the results.
00134      */
00135     int scanf(const char* format, ...);
00136  
00137     /* Function: readable
00138      *  Determine if there is a character available to read
00139      *
00140      * Variables:
00141      *  returns - 1 if there is a character available to read, else 0
00142      */
00143     int readable();
00144  
00145     /* Function: writeable
00146      *  Determine if there is space available to write a character
00147      *
00148      * Variables:
00149      *  returns - 1 if there is space to write a character, else 0
00150      */
00151     int writeable();
00152  
00153     /* Function: attach
00154      *  Attach a function to call whenever a serial interrupt is generated
00155      *
00156      * Variables:
00157      *  fptr - A pointer to a void function, or 0 to set as none
00158      */
00159     void attach(void (*fptr)(void));
00160  
00161     /* Function: attach
00162      *  Attach a member function to call whenever a serial interrupt is generated
00163      *
00164      * Variables:
00165      *  tptr - pointer to the object to call the member function on
00166      *  mptr - pointer to the member function to be called
00167      */
00168     template<typename T>
00169     void attach(T* tptr, void (T::*mptr)(void));
00170  
00171 #endif
00172  
00173 protected:
00174     PinName     _txpin;
00175  
00176     virtual int _putc(int c);
00177     virtual int _getc(void);
00178  
00179 }; // End class SerialHalfDuplex
00180  
00181 } // End namespace
00182  
00183 #endif
00184  
00185 #endif