Surasak Nasuriwong
/
Nucleo_Serial3
Remain content and fix $ed $rd buffer do not calear.
main.cpp@0:6b725c5afe52, 2016-09-14 (annotated)
- Committer:
- nsrwsurasak
- Date:
- Wed Sep 14 08:36:27 2016 +0000
- Revision:
- 0:6b725c5afe52
Remain Content
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nsrwsurasak | 0:6b725c5afe52 | 1 | #include "mbed.h" |
nsrwsurasak | 0:6b725c5afe52 | 2 | #include <string.h> |
nsrwsurasak | 0:6b725c5afe52 | 3 | |
nsrwsurasak | 0:6b725c5afe52 | 4 | #define MSG_BUF_SIZE 25 |
nsrwsurasak | 0:6b725c5afe52 | 5 | #define MSG2CMD 2 |
nsrwsurasak | 0:6b725c5afe52 | 6 | #define MSG2FILENAME (MSG_BUF_SIZE - MSG2CMD) |
nsrwsurasak | 0:6b725c5afe52 | 7 | #define BPS9600 9600 |
nsrwsurasak | 0:6b725c5afe52 | 8 | |
nsrwsurasak | 0:6b725c5afe52 | 9 | |
nsrwsurasak | 0:6b725c5afe52 | 10 | char Msg_RxBuf[MSG_BUF_SIZE+1]; // Reading Cmd Buffer |
nsrwsurasak | 0:6b725c5afe52 | 11 | int Msg_index; // An Cmd index |
nsrwsurasak | 0:6b725c5afe52 | 12 | |
nsrwsurasak | 0:6b725c5afe52 | 13 | char FileName[MSG2FILENAME]; |
nsrwsurasak | 0:6b725c5afe52 | 14 | |
nsrwsurasak | 0:6b725c5afe52 | 15 | bool RxInterruptEvent; |
nsrwsurasak | 0:6b725c5afe52 | 16 | |
nsrwsurasak | 0:6b725c5afe52 | 17 | DigitalOut led1(LED1); |
nsrwsurasak | 0:6b725c5afe52 | 18 | Serial serial_device(SERIAL_TX, SERIAL_RX); |
nsrwsurasak | 0:6b725c5afe52 | 19 | |
nsrwsurasak | 0:6b725c5afe52 | 20 | void Init_SerialDevice(); |
nsrwsurasak | 0:6b725c5afe52 | 21 | void RxMsgInterrupt(); |
nsrwsurasak | 0:6b725c5afe52 | 22 | void CmdCallback(); |
nsrwsurasak | 0:6b725c5afe52 | 23 | void GetListFileCmd(); |
nsrwsurasak | 0:6b725c5afe52 | 24 | void CheckReadEditCmd(); |
nsrwsurasak | 0:6b725c5afe52 | 25 | void ReadCmdParaNotEnough(); |
nsrwsurasak | 0:6b725c5afe52 | 26 | void EditCmdParaNotEnough(); |
nsrwsurasak | 0:6b725c5afe52 | 27 | |
nsrwsurasak | 0:6b725c5afe52 | 28 | int main() |
nsrwsurasak | 0:6b725c5afe52 | 29 | { |
nsrwsurasak | 0:6b725c5afe52 | 30 | RxInterruptEvent = 0; |
nsrwsurasak | 0:6b725c5afe52 | 31 | Init_SerialDevice(); |
nsrwsurasak | 0:6b725c5afe52 | 32 | RxMsgInterrupt(); |
nsrwsurasak | 0:6b725c5afe52 | 33 | |
nsrwsurasak | 0:6b725c5afe52 | 34 | while (1) { |
nsrwsurasak | 0:6b725c5afe52 | 35 | led1 = !led1; |
nsrwsurasak | 0:6b725c5afe52 | 36 | wait(0.5); |
nsrwsurasak | 0:6b725c5afe52 | 37 | } |
nsrwsurasak | 0:6b725c5afe52 | 38 | } |
nsrwsurasak | 0:6b725c5afe52 | 39 | |
nsrwsurasak | 0:6b725c5afe52 | 40 | /** |
nsrwsurasak | 0:6b725c5afe52 | 41 | * @brief |
nsrwsurasak | 0:6b725c5afe52 | 42 | * @note |
nsrwsurasak | 0:6b725c5afe52 | 43 | * @retval |
nsrwsurasak | 0:6b725c5afe52 | 44 | */ |
nsrwsurasak | 0:6b725c5afe52 | 45 | void Init_SerialDevice() |
nsrwsurasak | 0:6b725c5afe52 | 46 | { |
nsrwsurasak | 0:6b725c5afe52 | 47 | serial_device.baud(BPS9600); |
nsrwsurasak | 0:6b725c5afe52 | 48 | |
nsrwsurasak | 0:6b725c5afe52 | 49 | } |
nsrwsurasak | 0:6b725c5afe52 | 50 | |
nsrwsurasak | 0:6b725c5afe52 | 51 | /** |
nsrwsurasak | 0:6b725c5afe52 | 52 | * @brief |
nsrwsurasak | 0:6b725c5afe52 | 53 | * @note |
nsrwsurasak | 0:6b725c5afe52 | 54 | * @retval |
nsrwsurasak | 0:6b725c5afe52 | 55 | */ |
nsrwsurasak | 0:6b725c5afe52 | 56 | void RxMsgInterrupt() |
nsrwsurasak | 0:6b725c5afe52 | 57 | { |
nsrwsurasak | 0:6b725c5afe52 | 58 | /* Start Rx interrupt */ |
nsrwsurasak | 0:6b725c5afe52 | 59 | serial_device.attach(&CmdCallback); |
nsrwsurasak | 0:6b725c5afe52 | 60 | } |
nsrwsurasak | 0:6b725c5afe52 | 61 | |
nsrwsurasak | 0:6b725c5afe52 | 62 | /** |
nsrwsurasak | 0:6b725c5afe52 | 63 | * @brief |
nsrwsurasak | 0:6b725c5afe52 | 64 | * @note |
nsrwsurasak | 0:6b725c5afe52 | 65 | * @retval |
nsrwsurasak | 0:6b725c5afe52 | 66 | */ |
nsrwsurasak | 0:6b725c5afe52 | 67 | void CmdCallback() |
nsrwsurasak | 0:6b725c5afe52 | 68 | { |
nsrwsurasak | 0:6b725c5afe52 | 69 | |
nsrwsurasak | 0:6b725c5afe52 | 70 | // Note: you need to actually read from the serial to clear the RX interrupt |
nsrwsurasak | 0:6b725c5afe52 | 71 | Msg_RxBuf[Msg_index] = serial_device.getc(); |
nsrwsurasak | 0:6b725c5afe52 | 72 | //serial_device.putc(Msg_RxBuf[Msg_index]); |
nsrwsurasak | 0:6b725c5afe52 | 73 | Msg_index++; |
nsrwsurasak | 0:6b725c5afe52 | 74 | |
nsrwsurasak | 0:6b725c5afe52 | 75 | if(strstr(Msg_RxBuf,"ls")) |
nsrwsurasak | 0:6b725c5afe52 | 76 | { |
nsrwsurasak | 0:6b725c5afe52 | 77 | GetListFileCmd(); |
nsrwsurasak | 0:6b725c5afe52 | 78 | } |
nsrwsurasak | 0:6b725c5afe52 | 79 | |
nsrwsurasak | 0:6b725c5afe52 | 80 | if(strstr(Msg_RxBuf,".csv")) |
nsrwsurasak | 0:6b725c5afe52 | 81 | { |
nsrwsurasak | 0:6b725c5afe52 | 82 | CheckReadEditCmd(); |
nsrwsurasak | 0:6b725c5afe52 | 83 | } |
nsrwsurasak | 0:6b725c5afe52 | 84 | |
nsrwsurasak | 0:6b725c5afe52 | 85 | if(strstr(Msg_RxBuf,".xml")) |
nsrwsurasak | 0:6b725c5afe52 | 86 | { |
nsrwsurasak | 0:6b725c5afe52 | 87 | CheckReadEditCmd(); |
nsrwsurasak | 0:6b725c5afe52 | 88 | } |
nsrwsurasak | 0:6b725c5afe52 | 89 | |
nsrwsurasak | 0:6b725c5afe52 | 90 | if(strstr(Msg_RxBuf,".log")) |
nsrwsurasak | 0:6b725c5afe52 | 91 | { |
nsrwsurasak | 0:6b725c5afe52 | 92 | CheckReadEditCmd(); |
nsrwsurasak | 0:6b725c5afe52 | 93 | } |
nsrwsurasak | 0:6b725c5afe52 | 94 | |
nsrwsurasak | 0:6b725c5afe52 | 95 | if(Msg_index >= 2) |
nsrwsurasak | 0:6b725c5afe52 | 96 | { |
nsrwsurasak | 0:6b725c5afe52 | 97 | if( !((strstr(Msg_RxBuf,"ls")) || (strstr(Msg_RxBuf,"rd")) || (strstr(Msg_RxBuf,"ed")))) |
nsrwsurasak | 0:6b725c5afe52 | 98 | { |
nsrwsurasak | 0:6b725c5afe52 | 99 | serial_device.printf("$?? -- Command Error\n",Msg_RxBuf+1); |
nsrwsurasak | 0:6b725c5afe52 | 100 | Msg_index = 0; |
nsrwsurasak | 0:6b725c5afe52 | 101 | memset(Msg_RxBuf,' ',MSG_BUF_SIZE); |
nsrwsurasak | 0:6b725c5afe52 | 102 | } |
nsrwsurasak | 0:6b725c5afe52 | 103 | } |
nsrwsurasak | 0:6b725c5afe52 | 104 | } |
nsrwsurasak | 0:6b725c5afe52 | 105 | |
nsrwsurasak | 0:6b725c5afe52 | 106 | /** |
nsrwsurasak | 0:6b725c5afe52 | 107 | * @brief |
nsrwsurasak | 0:6b725c5afe52 | 108 | * @note |
nsrwsurasak | 0:6b725c5afe52 | 109 | * @retval |
nsrwsurasak | 0:6b725c5afe52 | 110 | */ |
nsrwsurasak | 0:6b725c5afe52 | 111 | void GetListFileCmd() |
nsrwsurasak | 0:6b725c5afe52 | 112 | { |
nsrwsurasak | 0:6b725c5afe52 | 113 | serial_device.printf("$ls -- list file command\n"); |
nsrwsurasak | 0:6b725c5afe52 | 114 | Msg_index = 0; |
nsrwsurasak | 0:6b725c5afe52 | 115 | memset(Msg_RxBuf,' ',MSG_BUF_SIZE); |
nsrwsurasak | 0:6b725c5afe52 | 116 | } |
nsrwsurasak | 0:6b725c5afe52 | 117 | |
nsrwsurasak | 0:6b725c5afe52 | 118 | /** |
nsrwsurasak | 0:6b725c5afe52 | 119 | * @brief |
nsrwsurasak | 0:6b725c5afe52 | 120 | * @note |
nsrwsurasak | 0:6b725c5afe52 | 121 | * @retval |
nsrwsurasak | 0:6b725c5afe52 | 122 | */ |
nsrwsurasak | 0:6b725c5afe52 | 123 | void CheckReadEditCmd() |
nsrwsurasak | 0:6b725c5afe52 | 124 | { |
nsrwsurasak | 0:6b725c5afe52 | 125 | if(strstr(Msg_RxBuf,"rd")) |
nsrwsurasak | 0:6b725c5afe52 | 126 | { |
nsrwsurasak | 0:6b725c5afe52 | 127 | serial_device.printf("$rd -- read command\n"); |
nsrwsurasak | 0:6b725c5afe52 | 128 | memcpy(FileName,&Msg_RxBuf[3],(Msg_index - 3)); |
nsrwsurasak | 0:6b725c5afe52 | 129 | serial_device.printf("FileName -- %s \n",FileName); |
nsrwsurasak | 0:6b725c5afe52 | 130 | Msg_index = 0; |
nsrwsurasak | 0:6b725c5afe52 | 131 | memset(Msg_RxBuf,' ',MSG_BUF_SIZE); |
nsrwsurasak | 0:6b725c5afe52 | 132 | } |
nsrwsurasak | 0:6b725c5afe52 | 133 | else |
nsrwsurasak | 0:6b725c5afe52 | 134 | { |
nsrwsurasak | 0:6b725c5afe52 | 135 | Msg_index = 0; |
nsrwsurasak | 0:6b725c5afe52 | 136 | memset(Msg_RxBuf,' ',MSG_BUF_SIZE); |
nsrwsurasak | 0:6b725c5afe52 | 137 | } |
nsrwsurasak | 0:6b725c5afe52 | 138 | |
nsrwsurasak | 0:6b725c5afe52 | 139 | if(strstr(Msg_RxBuf,"ed")) |
nsrwsurasak | 0:6b725c5afe52 | 140 | { |
nsrwsurasak | 0:6b725c5afe52 | 141 | serial_device.printf("$ed -- edit command\n"); |
nsrwsurasak | 0:6b725c5afe52 | 142 | memcpy(FileName,&Msg_RxBuf[3],Msg_index - 3); |
nsrwsurasak | 0:6b725c5afe52 | 143 | serial_device.printf("FileName -- %s \n",FileName); |
nsrwsurasak | 0:6b725c5afe52 | 144 | Msg_index = 0; |
nsrwsurasak | 0:6b725c5afe52 | 145 | memset(Msg_RxBuf,' ',MSG_BUF_SIZE); |
nsrwsurasak | 0:6b725c5afe52 | 146 | } |
nsrwsurasak | 0:6b725c5afe52 | 147 | else |
nsrwsurasak | 0:6b725c5afe52 | 148 | { |
nsrwsurasak | 0:6b725c5afe52 | 149 | Msg_index = 0; |
nsrwsurasak | 0:6b725c5afe52 | 150 | memset(Msg_RxBuf,' ',MSG_BUF_SIZE); |
nsrwsurasak | 0:6b725c5afe52 | 151 | } |
nsrwsurasak | 0:6b725c5afe52 | 152 | } |
nsrwsurasak | 0:6b725c5afe52 | 153 | |
nsrwsurasak | 0:6b725c5afe52 | 154 | /** |
nsrwsurasak | 0:6b725c5afe52 | 155 | * @brief |
nsrwsurasak | 0:6b725c5afe52 | 156 | * @note |
nsrwsurasak | 0:6b725c5afe52 | 157 | * @retval |
nsrwsurasak | 0:6b725c5afe52 | 158 | */ |
nsrwsurasak | 0:6b725c5afe52 | 159 | void ReadCmdParaNotEnough() |
nsrwsurasak | 0:6b725c5afe52 | 160 | { |
nsrwsurasak | 0:6b725c5afe52 | 161 | serial_device.printf("$rd -- Paramiters not enough\n"); |
nsrwsurasak | 0:6b725c5afe52 | 162 | Msg_index = 0; |
nsrwsurasak | 0:6b725c5afe52 | 163 | memset(Msg_RxBuf,' ',MSG_BUF_SIZE); |
nsrwsurasak | 0:6b725c5afe52 | 164 | } |
nsrwsurasak | 0:6b725c5afe52 | 165 | |
nsrwsurasak | 0:6b725c5afe52 | 166 | |
nsrwsurasak | 0:6b725c5afe52 | 167 | /** |
nsrwsurasak | 0:6b725c5afe52 | 168 | * @brief |
nsrwsurasak | 0:6b725c5afe52 | 169 | * @note |
nsrwsurasak | 0:6b725c5afe52 | 170 | * @retval |
nsrwsurasak | 0:6b725c5afe52 | 171 | */ |
nsrwsurasak | 0:6b725c5afe52 | 172 | void EditCmdParaNotEnough() |
nsrwsurasak | 0:6b725c5afe52 | 173 | { |
nsrwsurasak | 0:6b725c5afe52 | 174 | serial_device.printf("$ed -- Paramiters not enough\n"); |
nsrwsurasak | 0:6b725c5afe52 | 175 | Msg_index = 0; |
nsrwsurasak | 0:6b725c5afe52 | 176 | memset(Msg_RxBuf,' ',MSG_BUF_SIZE); |
nsrwsurasak | 0:6b725c5afe52 | 177 | } |