Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
RS485.h
00001 00002 /** 00003 * @file RS485.h 00004 * @brief RS485 protocol - using half duplex method 00005 * @author Aaron Allar 00006 * @version 1.0 00007 * @see 00008 * 00009 * based on this method https://www.gammon.com.au/forum/?id=11428 00010 * 00011 * Can send from 1 to 255 bytes from one node to another with: 00012 * 00013 * Packet start indicator (STX) 00014 * Each data byte is doubled and inverted to check validity 00015 * Packet end indicator (ETX) 00016 * Packet CRC (checksum) 00017 */ 00018 00019 00020 00021 #ifndef RS485_H 00022 #define RS485_H 00023 00024 #include "BufferedSerial.h" 00025 #include "mbed.h" 00026 00027 /** RS485 Library 00028 * 00029 * Can send from 1 to 255 bytes from one node to another with: 00030 * 00031 * Packet start indicator (STX) 00032 * Each data byte is doubled and inverted to check validity 00033 * Packet end indicator (ETX) 00034 * Packet CRC (checksum) 00035 * 00036 * Using MAX485 modules or the MAX485 CSA+ 00037 * 00038 * Example: 00039 * @code 00040 * #include "mbed.h" 00041 * #include <RS485.h> 00042 * Serial pc(USBTX, USBRX); 00043 * RS485 RS485(PC_10,PC_11,PB_3); // Tx, Rx , !RE and DE MAX485 pin 00044 * 00045 * DigitalOut ho(PB_3); // this pin should be connected to !RE and DE 00046 * typedef uint8_t byte; 00047 * 00048 * byte regvalue[9]; 00049 * byte data[9] = {0x01,0x04,0x00,0x48,0x00,0x02,0xf1,0xdd};//your data 00050 * int main() 00051 * { 00052 * pc.printf("main\n"); 00053 * while(1) { 00054 * pc.printf("Starting\n"); 00055 * ho = 1; // Enable sending on MAX485 00056 * RS485.sendMsg(data,sizeof(data)); 00057 * wait_ms(600); // Must wait for all the data to be sent 00058 * ho = 0; // Enable receiving on MAX485 00059 * pc.printf("Getting data\n"); 00060 * if(RS485.readable() >0){ 00061 * memset(regvalue,0,sizeof(regvalue)); 00062 * wait_ms(200); 00063 * RS485.recvMsg(regvalue,sizeof(data),500); 00064 * wait_ms(200); 00065 * for (int count = 0; count < 9; count++) { 00066 * pc.printf("%X - ", regvalue[count]); 00067 * } 00068 * }else printf("No Data\n"); 00069 * printf("Done\n"); 00070 * wait_ms(1000); 00071 * } 00072 * } 00073 * @endcode 00074 */ 00075 00076 00077 /** 00078 * @class RS485 00079 * @communicating 00080 */ 00081 00082 class RS485 : public BufferedSerial 00083 { 00084 private: 00085 typedef unsigned int word; 00086 typedef uint8_t byte; 00087 typedef uint8_t boolean; 00088 typedef void (*voidFuncPtr)(void); 00089 00090 public: 00091 /** Create a BufferedSerial port, connected to the specified transmit and receive pins 00092 * @param tx Transmit pin 00093 * @param rx Receive pin 00094 * @param dere Enable pin, this pin should be connected to !RE and DE 00095 * @note uses BufferedSerial 00096 */ 00097 RS485(PinName tx, PinName rx, PinName dere); 00098 00099 /** calculate 8-bit CRC 00100 * cyclic redundancy check 00101 * @param addr byte pointer of information to use (typical an byte array) 00102 * @param len length of byte of information were converting 00103 * @return the CRC byte 00104 */ 00105 static byte crc8 (const byte *addr, byte len); 00106 00107 /** sendComplemented byte 00108 * send a byte complemented, repeated 00109 * only values sent would be (in hex): 00110 * 0F, 1E, 2D, 3C, 4B, 5A, 69, 78, 87, 96, A5, B4, C3, D2, E1, F0 00111 * @what the byte to complement 00112 */ 00113 void sendComplemented (const byte what); 00114 00115 /** send message 00116 * cyclic redundancy check 00117 * @param data the data to be sent through RS485 00118 * @param length length of the data 00119 * @note puts STX at start, ETX at end, and add CRC 00120 */ 00121 void sendMsg (const byte * data, const byte length); 00122 00123 /** receive message 00124 * reads serial port and populates data 00125 * @param data buffer to receive into 00126 * @param length length of the data 00127 * @param timeout clock_mseconds before timing out 00128 * @return the number of bytes received 00129 * 00130 */ 00131 byte recvMsg (byte * data, const byte length, unsigned long timeout); 00132 00133 00134 }; 00135 #endif
Generated on Tue Jul 12 2022 20:26:33 by
