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.
Dependencies: MessageQueue ModbusSlaveRTU SerialPortHandler mbed-rtos mbed
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 #include "ModbusSlaveRTU.h" 00004 #include "MessageQueue.h" 00005 #include "SerialPortHandler.h" 00006 00007 DigitalOut led(LED1); 00008 Serial pc(SERIAL_TX, SERIAL_RX); 00009 MessageQueue<uint8_t> txQueue(64); 00010 MessageQueue<uint8_t> rxQueue(64); 00011 SerialPortHandler sph(&pc, &txQueue, &rxQueue); 00012 Timer receiveTimer; 00013 bool isReceiveTimerInitialized = false; 00014 ModbusSlaveRTU* mbus; 00015 Thread* modbus_process_thread; 00016 Thread* data_send_thread; 00017 Thread* receive_timer_check_thread; 00018 00019 uint8_t cRegs[] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0}; 00020 uint8_t inRegs[] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0}; 00021 uint8_t hRegs[] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0}; 00022 00023 ThreadSafeArray_t hR; 00024 ThreadSafeArray_t cR; 00025 ThreadSafeArray_t iR; 00026 00027 void rx_interrupt(void); 00028 00029 void modbus_process (void const *args) { 00030 00031 while (true) { 00032 mbus->FSM(); 00033 Thread::wait(10); 00034 } 00035 } 00036 00037 void data_send_process(void const *args) 00038 { 00039 while (true) { 00040 if (!txQueue.isEmpty()) 00041 { 00042 sph.transmitPacket(); 00043 txQueue.reset(); 00044 } 00045 Thread::wait(500); 00046 } 00047 } 00048 00049 void timer_check(void const *args) 00050 { 00051 while(true) { 00052 if (receiveTimer.read_ms() >= 1) { 00053 mbus->trigger(); 00054 isReceiveTimerInitialized = false; 00055 receiveTimer.reset(); 00056 receiveTimer.stop(); 00057 } 00058 /*Waits for 5 ms*/ 00059 Thread::wait(5); 00060 } 00061 } 00062 00063 int main (void) { 00064 pc.attach(&rx_interrupt); 00065 00066 hR.length = 20; 00067 hR.data = hRegs; 00068 00069 cR.length = 20; 00070 cR.data = cRegs; 00071 00072 iR.length = 20; 00073 iR.data = inRegs; 00074 00075 mbus = new ModbusSlaveRTU(0x01, &txQueue, &rxQueue, &cR, &iR, &hR); 00076 modbus_process_thread = new Thread(modbus_process); 00077 data_send_thread= new Thread(data_send_process); 00078 receive_timer_check_thread = new Thread(timer_check); 00079 while(true) 00080 { 00081 Thread::wait(1000); 00082 led = !led; 00083 } 00084 } 00085 00086 void rx_interrupt(void) { 00087 if (!isReceiveTimerInitialized) 00088 { 00089 isReceiveTimerInitialized = true; 00090 receiveTimer.start(); 00091 } 00092 else 00093 { 00094 receiveTimer.reset(); 00095 } 00096 sph.receivePacket(); 00097 return; 00098 }
Generated on Mon Jul 25 2022 14:36:10 by
1.7.2