Protocols used to talk between an embedded CPU and a PC

前端 未结 8 1938
梦谈多话
梦谈多话 2021-01-31 20:21

I am building a small device with its own CPU (AVR Mega8) that is supposed to connect to a PC. Assuming that the physical connection and passing of bytes has been accomplished,

8条回答
  •  半阙折子戏
    2021-01-31 20:41

    There's a lot to be said for client-server architecture and synchronous protocols. Simplicity and robustness, to start. If speed isn't an issue, you might consider a compact, human-readable protocol to help with debugging. I'm thinking along the lines of modem AT commands: a "wakeup" sequence followed by a set/get command, followed by a terminator.

    Host -->  [V02?]      // Request voltage #2
    AVR  -->  [V02=2.34]  // Reply with voltage #2
    Host -->  [V06=3.12]  // Set voltage #6
    AVR  -->  [V06=3.15]  // Reply with voltage #6
    

    Each side might time out if it doesn't see the closing bracket, and they'd re-synchronize on the next open bracket, which cannot appear within the message itself.

    Depending on speed and reliability requirements, you might encode the commands into one or two bytes and add a checksum.

    It's always a good idea to reply with the actual voltage, rather than simply echoing the command, as it saves a subsequent read operation.

    Also helpful to define error messages, in case you need to debug.

提交回复
热议问题