// clear the serial buffer public void flushSerialBuffer(SerialBuffer serialport) { serialport.Clear(); }
// processData. processData assembles the frames and calls to transitionAction public void processData(SerialBuffer sBuffer) { int i; int numBytesToRead = sBuffer.bytesAvailable(); // let's see what we 've got... do { if (remainingBytes == 0) // previous frame was fully read { if (numBytesToRead >= 6) // nead at least 6 bytes to read frame header // get the first 6 bytes (starter(1)+sender-receiverid(2)+frame type(1)+payload length (2)) // reading first 6 bytes into FrameHead { sBuffer.readBytes(FrameHead, 6); // now cheking for framestarter character mismatch if (FrameHead[0] != framestarter) { flushSerialBuffer(sBuffer); } else // reamaining bytes should be the payload length plus the terminator and two CRC bytes { remainingBytes = FrameHead[4] + FrameHead[5] * 256 + 3; } } } else if (numBytesToRead >= remainingBytes) { // it's time to get the remaining frame(s) int totalBytes = remainingBytes + 6; // calculate total length byte[] buffer = new byte[totalBytes]; byte[] remBuffer = new byte[remainingBytes]; // now reading remaining bytes as estimated using the frame header // going altogether sBuffer.readBytes(remBuffer, remainingBytes); // tailoring bufhead and rembuffer into buffer for (i = 0; i < totalBytes; i++) { if (i < 6) { buffer[i] = FrameHead[i]; } else { buffer[i] = remBuffer[i - 6]; } } // now handling the message... // checking terminator and CRC ushort CRC = (ushort)(buffer[totalBytes - 2] + 256 * buffer[totalBytes - 1]); if ((buffer[totalBytes - 3] == frameterminator) && (checkCRC(buffer, totalBytes - 2, CRC))) { EZFrame frame = bytes2Frame(buffer); // done if (frame.ReceiverID == NodeID) // packet addressed to this node { transitionAction(frame); // change internal state and act } } // clearing remaining bytes remainingBytes = 0; } numBytesToRead = sBuffer.bytesAvailable(); } while ((numBytesToRead >= remainingBytes) && (remainingBytes > 0)); }
// Constructor public EZRoboNetDevice(byte node_id, byte node_type) { NodeID = node_id; DevNodeType = node_type; // initializing the CommPort and CommPortBuffer CommPortBuffer = new SerialBuffer(); CommPort = new System.IO.Ports.SerialPort("COM1"); CommPort.BaudRate = 19200; CommPort.WriteBufferSize = 10000; CommPort.ReadBufferSize = 10000; CommPort.Parity = System.IO.Ports.Parity.None; CommPort.Handshake = System.IO.Ports.Handshake.None; CommPort.StopBits = System.IO.Ports.StopBits.One; CommPort.ReceivedBytesThreshold = 2; CommPort.DataReceived += this.SerialDataReceived; // Initializing the Timer SyncTimer = new System.Windows.Forms.Timer(); // 10 msec interval SyncTimer.Interval = 30; // 10 msec interval SyncTimer.Tick += SyncTimerTick; // initializing state state = stIdle; // flags PacketSendTimeout = false; PacketReceiveTimeout = false; PacketQueueOverflow = false; // incoming data processing globals remainingBytes = 0; FrameHead = new byte[6]; // initializing array of incomplete packets IncompletePackets = new EZStationPacketFSM[MAX_RECEIVED_PACKETS_NUM]; // null-ing all entries int i; for (i = 0; i < MAX_RECEIVED_PACKETS_NUM; i++) { IncompletePackets[i] = null; } IncompletePacketsNum = 0; // initializing array of received packets ReceivedPackets = new EZPacket[MAX_RECEIVED_PACKETS_NUM]; ReceivedPacketsNum = 0; // initializing array of outbound packets OutboundPackets = new EZPacket[MAX_OUTBOUND_PACKETS_NUM]; OutboundPacketsNum = 0; // initializing entity list. No entities registered yet EntityList = new RoboEntity[MAX_ENTITY_NUM]; for (i = 0; i < MAX_ENTITY_NUM; i++) { EntityList[i] = null; } EntityNum = 0; // starting the NetDevice (opening serial port and starting timer) CommPort.Open(); SyncTimer.Start(); }