|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
Best way to use RS232 from pc to pic, protocol... |
Posted: Sun May 03, 2009 1:01 am |
|
|
Hi
I want to implement some control from a pc to a pic system. The communication is both way.
I am not sure what the best way is?
If the pc want to send some command to the pic, it can be done like:
b1 'c' tell that it is a command
b2 data
b3 data
b4 crc8 of byte b1,b2,b3.
__
Not the pic must reply back it understand it? If so it can be done like:
b1 'r' tell it is a reply
b2 data (0x01 ok/ 0xff err)
b3 data
b4 crc8 of byte b1,b2,b3,b4
__
The PC is only sending max 2 byte command each time. I plane that every time must be send 4byte and received 4 byte - both way?
But is it a good way to handle it?
What I want here is some hints and help to make the system talking the best way? |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Sun May 03, 2009 6:32 am |
|
|
There are a number of different things to consider.
Flexible protocols (ones tjat readily support extension in the future) include a length parameter to support variable length commands/data.
Command Length Data Checksum/CRC
Additionally. some mechanism is required to synchronize the two ends. This is often done with a preamble (start key) and post amble (end key) although in your case a valid CRC would meet the post amble requirement. With binary data a false key match is to be expected so you need to select a key that is not likely to be present in the data and expect to deal with the occasional false key however, once the two ends are in sync this issue would occur rarely unless in a particularly noisy environment.
An alternative, well suited to your application, is to convert the binary data including Cecksum/Crc to ASCII. This way you could use standard start and end delimiters that are guaranteed not to be present in the data and you have a readable message that could be viewed on a terminal for debugging purposes. Although this method potentially doubles the message size it is easy and simple to use. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
Steve H Guest
|
|
Posted: Sun May 03, 2009 10:35 am |
|
|
Many systems use very simple communications by just signifying the end of a command by sensing a new line or line feed.
Many years ago I wrote an article on a simple data acquisition device based on a PIC that talked to a PC via RS232.
You can download the CCS C firmware from this site,
http://www.geocities.com/hagtronics/pic_das/
Look toward the bottom of the page for the download (item 7 under software).
CRC is nice and if I was using a Modem I would implement it - but if you are just using a direct connection to a PC a few feet away with a simple device - it may be overkill. It seems unlikely that you would be having data transmission issues with a setup like this.
RS232 or RS485 looped around a building with looooong cable runs is a different issue - implement CRC in these cases - you never know what the cables may be run by or next to.
On the other hand if this was some device that could cause serious damage or harm to a person then CRC is a safety measure that should not be overlooked.
My 2 cents worth...
HTH - Steve H. |
|
|
Guest
|
|
Posted: Mon May 04, 2009 12:37 am |
|
|
Thanks for both reply:-) |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|