![]() ![]() ![]() ![]() ![]() Alan numitron clock Clapclap 2313/1386 SNES Pi Webserver USB Volume/USB toys Smokey amp Laser cutter WordClock ardReveil v3 SNES Arcade cabinet Game boy projects cameleon Home Presence Detector ![]() AlanFromJapan ![]() ![]() ![]() Akizukidenshi Elec-lab Rand Nerd Tut EEVblog SpritesMods AvrFreaks Gameboy Dev FLOZz' blog Switch-science Sparkfun Suzusho Datasheet Lib Reddit Elec Ermicro Carnet du maker (fr) |
arcpoLast update: Thu Jun 5 22:25:40 2025
PurposePropose a simple way to communicate 2 ways between PC and Arduino. I want also to abstract from low level stuff focus on the high level. I also don't want to care about serial communication tricks and caveats, how to make sure packet has arrived, etc... so let's write this once and for all.This works with fixed size packets.
General ideaA PackageCommunication is done by sending packages on the serial port. You can ask for a receipt acknowledgement, the library will take care of retrying a few times if missed. You just make the packet and post it. You can focus on what your software/piece of electronic is supposed to do and not waste time again and again with serial communication stuffs.Each packet is sent with a header "$*" and the it goes until end of message/fixed length. Package is defined as follow: //Content member max length #define CONTENT_LENGTH 56 //length WITHOUT the header 2 chars #define FULL_PACKET_LENGTH CONTENT_LENGTH %20 4 //Each packet is sent with that signature $*ITEContent.... //A packet is 64 in length, internal serial buffer is 128 according documentation, which is a maximum if you dont //want to loose too much packet I assume. typedef struct { char ID; //default undefined char Type; char SubType; char ExpectAcknowledge; char Content[CONTENT_LENGTH]; } ARCPOPacket; IMPORTANT NOTA BENE : none of this field should worth 0 ! Serial communication interprets it as a "hey, it's finished, nothing more to read". Don't know why, maybe it's me, but to avoid this, make sure to put something else that 0 everywhere. Field description:
Arduino sideEasy classes and library to include, that can read/write a packet and send to the pc without much trouble.#include "ARCPO_Lib.h" void ProcessPacket (ARCPOPacket& pPacket) { switch (pPacket.Type){ case PACKET_TYPE_TEXT: ProcessPacketText (pPacket); break; case PACKET_TYPE_TIME: ProcessPacketTime (pPacket); break; default: //nothing break; } } //Make a packet and send. Set every member to something != 0 !! void SampleSendPacket(){ ARCPOPacket vP; vP.ID = 1; vP.Type = 98; vP.SubType = 128; strcpy(vP.Content, "hello monde"); writePacket (vP); } void setup() { Serial.begin(9600); } void loop() { ARCPOPacket vPacket; vPacket = readPacket(); if (readPacketSuccess()){ ProcessPacket (vPacket); } delay(50); } PC sideC# multithreaded message reception plus integrated acknowledgement of sent messages.Just instanciate a ARCPO_Connector for sending. If you want to received messages also, attache the PacketReceived event and then (in that order please) set PollMessages to true. private ARCPO_Connector mConnector = null; private void Form1_Load(object sender, EventArgs e) { mConnector = new ARCPO_Connector(9600, "COM8"); this.mConnector.PacketReceived %20= new EventHandler<ARCPO_ReceivedEventArgs>(Connector_PacketReceived); this.mConnector.PollMessages = true; } private void Connector_PacketReceived(object sender, ARCPO_ReceivedEventArgs e) { textBox1.Text %20= ">>RECEIVED unexpected message " %20 e.Packet.mID %20 " : "; textBox1.Text %20= e.Packet.ContentString; textBox1.Text %20= "\r\n"; } private void SendPacket(string pContent, byte pType, byte pSubType) { ARCPO_Packet vP = new ARCPO_Packet(); int vPacketId = mPacketCounter; vP.mType = pType; vP.mID = (byte)(mPacketCounter%20%20 % 256); vP.mSubType = pSubType; vP.mExpectAcknowledge = ckbAck.Checked; ; vP.ContentString = pContent; mConnector.SendPacket(vP); } |
|||||||||||||||
All content on this site is shared under the MIT licence (do what u want, don't sue me, hat tip appreciated) electrogeek.tokyo ~ Formerly known as Kalshagar.wikispaces.com and electrogeek.cc (AlanFromJapan [2009 - 2025]) |