1 Attachment(s)
Communications Example : PC to PIC bi-directional dialogue
Judging by the emails I'm getting, folks have missed this hidden away at the end of another thread... so it's been repeated with a thread all of it's own.
Here's a sample program, thrown together for fun this lunchtime, to demonstrate bi-directional dialogue with a PC running something like Hyperterminal. It runs at 300 baud so almost any PIC will run it without an Xtal or Resonator (but best with!!!!).
The object of the program is to demostrate how commands can be passed from the PC User and acted upon by the PIC.
Yes, whatever you type on your PC is echoed back to you as feedback. The program is reasonably interactive... it tells you when you're Bad!, and tells you when you're OK. UPPERCASE and lowercase and cOmBINatIOns are handled with reasonably idiot-proof command interpretation... this is done by parsing the received command line (in a simplified manner for this example) rather than the usual way of having SERIN (or whatever) WAITing for a valid command.
You can even use the BACKSPACE Key and have the PIC perform rudimentary line editing.
PIC has five LED's which can be remote controlled in a variety of ways with commands like ON, OFF, BLINK, SWEEP and BEER. The major commands have sub-parameters which can be added to make control more interesting. Plenty of expansion for you to do your own thing.
Example:
ON Switch ON all LEDs
ON 2 Switch ON LED number 2
you can even have abnormally large gaps between the Command (eg ON) and the sub-parameters (eg 2)
ON 4 7 Switch ON LED number 4 for 7 seconds
BLINK 1 15 Blink LED number 1 15 times
OFF 1 Switch OFF LED number 1
SWEEP 25 Sequentially Sweep all LEDs 25 times
Try to break it (if you can) by typing combinations of garbage or having line lengths bigger than 10 bytes (my preset communications buffer maximum)....
Full instructions in the program comments.
Melanie
Some clarifications if you please
Hello Melanie
I have seen the code you posted. I have a couple of queries and would like to know your opinion on them.
You use a step wise approach of building the command in the buffer by reading characters one by one. Can I use a construct like this and get away with it? If not, what could be the catch?
Code:
' wait for a command terminated by CR
hserin [STR CmdBuf\16\13]
You compare commands with templates in EEPROM. Is there a way to avoid this and compare directly with templates in program space? I plan to have a strcmp function for each template I want checked. So, I might invoke it like this
gosub CheckCmd1
if Result then Command1
gosub CheckCmd2
if Result then Command2
Each CheckCmd function will check the Command Buffer against the template it has.
I am trying to do this
Command1 - TS ddmmyy hhnn <CR> Time Set
Command2 - GL <CR> Get Log
Command3 - CL <CR> Clear Log
I'd like to know what you think of this approach
Thanks
Jerson
Re: Communications Example : PC to PIC bi-directional dialogue
I know not strictly to do with PIC Basic, but if anyone is interested, I rewrote this into ProtonBasic (Crownhill) :eek: and it works just fine. I'll post the code here when I'm on my own PC.