Custom Python Serial Monitor

Recently I was developing arang framework for robotics club and found out that storing fixed serial string is kinda waste of resource although it is stored in flash memory. To fix this, I discussed with some of the robotics club member and found out the best way to change the communication between computer and mCore to short binary protocol instead of raw large ASCII characters stream. But, I encountered some problem when I was trying this solution.

Problem when implement custom binary protocol

  • No serial monitor support it (e.g. Putty)
  • If develop custom serial monitor, it has to be cross-platform

Solution

After some consideration, I decided to develop my own serial monitor because there is some feature that is not available in others serial monitor such as ping echo and file transfering that is necessary for this project. Then, I need to make the serial monitor become cross-platform because the rest of robotics club member are all using Windows (sad Linux noise). To do that, I choose Python to make the serial monitor. Then after some quick Google search, I found a incredible python library, pySerial. It can let Python program access serial port on any platform.

Protocol design

I want the protocol transmit data in 5 bytes long packet except file transfering packet.

Packet design

Normal packet

1 2 3 4 5
command data(D1) data(D2) data(D3) Ending byte (0x69)

file transfering and ASCII stream

n
Raw bytes

Command design

Command Sending side D1 D2 D3 Result
‘A’ PC n/a n/a n/a return packet that contains number of flash memory usage in D1, number of program in D2 and error code in D3 with command ‘M’
‘B’ PC/mCore x n/a n/a acknowledge peer the length of incoming ASCII stream in D1
‘C’ PC n/a n/a n/a show detailed information of all the program
‘D’ PC x n/a n/a request to delete program with index in D1
‘E’ PC x n/a n/a set program with index in D1 as default program onstart
‘F’ PC n/a n/a n/a request to reset payload table in EEPROM
‘G’ PC x n/a n/a acknowledge incoming payload binary with flash length in D1
‘M’ PC/mCore x x x send packet that contains information in each data byte
‘X’ mCore x x x acknowledge PC that mCore is ready for commands
‘Y’ PC x x x synchronize mCore to PC
‘P’ mCore n/a n/a n/a mCore ping to PC. If no respond, mCore run default program
0x69 PC n/a n/a n/a ping responce from PC to mCore