CHE-WEI LIN
/
modbus_test
share
Diff: main.cpp
- Revision:
- 1:17ea42de4a8a
- Parent:
- 0:6fd21c22a0c9
- Child:
- 2:9497b91d341a
diff -r 6fd21c22a0c9 -r 17ea42de4a8a main.cpp --- a/main.cpp Tue Jul 16 14:42:16 2019 +0000 +++ b/main.cpp Thu Aug 22 07:17:07 2019 +0000 @@ -1,8 +1,9 @@ #include "mbed.h" -Serial pc(USBTX, USBRX); -Serial modbus(PB_9, PB_8,9600);//(tx,rx,baud) -DigitalOut DE_RE(PB_0); +Serial pc(USBTX, USBRX); //((USBTX, USBRX)) +Serial modbus(PA_9, PA_10,9600);//(tx,rx,baud) +Serial evk(PC_10,PC_11,115200); //CN7 +DigitalOut DE_RE(PA_8); #define Read_Reg 0x03 int Max_Byte=8; @@ -19,13 +20,21 @@ //put PC input into modbus void PC_callback() { //modbus.putc(pc.getc()); - char incomming_CMD; - while (pc.readable()){ - incomming_CMD+=pc.getc(); + // char incomming_CMD; + /* while (pc.readable()){ + incomming_CMD+=pc.getc()-1; } - pc.printf("Received Command :%c\r\n", incomming_CMD); + pc.printf("Received Command :%c\r\n", incomming_CMD);*/ + + pc.putc(pc.getc()); } +void evk_callback() { + pc.putc('a'); + pc.putc(evk.getc()); +} + + //put modbus reply into PC void MODBUS_callback() { //pc.putc(modbus.getc()); @@ -34,6 +43,7 @@ } + //MODBUS RTU CRC uint16_t mdbRTU_CRC(uint8_t message[], int Length) { @@ -59,7 +69,8 @@ } int main() { - + pc.baud(115200); + evk.attach(&evk_callback); pc.attach(&PC_callback); modbus.attach(&MODBUS_callback); uint8_t dev_Addr; @@ -100,6 +111,7 @@ //pc.printf("RTU_CRC=%04x\r\n",RTU_CRC); hi_RTU_CRC = (uint8_t)((RTU_CRC & 0xFF00) >> 8); lo_RTU_CRC = (uint8_t)(RTU_CRC & 0x00FF); + if ((mdb_Message_buffer[data_len+3]==lo_RTU_CRC)&&(mdb_Message_buffer[data_len+4]==hi_RTU_CRC)) { for (int i=0 ; i<data_len ; i++) @@ -107,6 +119,8 @@ mdb_Data[(dev_Addr)].Data[i]=mdb_Message_buffer[i+3]; //pc.putc(mdb_Message_buffer[i]); pc.printf("mdb_Data= %02x\r\n",mdb_Data[(dev_Addr)].Data[i]); + // evk.printf("mdb_Data= %02x\r\n",mdb_Data[(dev_Addr)].Data[i]); + } }