static public void SendStream(byte[] _stream, int _len)
 {
     try
     {
         if (serialPort.IsOpen)
         {
             ConsoleData.AddInfo(_stream, _len, true);
             serialPort.Write(_stream, 0, _len);
         }
     }
     catch (Exception) { };
 }
示例#2
0
 static public void DataHandle(object sender, SerialDataReceivedEventArgs e)
 {
     dataCount++;
     if (streamState == StreamState.WAITFORHEAD)
     {
         if (SerialManager.serialPort.BytesToRead >= 1)
         {
             SerialManager.serialPort.Read(streamBuffer, streamBufferPos, 1);
             if (streamBuffer[0] == HOST_STREAM_HEAD)
             {
                 streamBufferPos = 1;
                 streamState     = StreamState.WAITFORLENGTH;
                 SerialManager.SetISRLength(2);
             }
         }
     }
     else if (streamState == StreamState.WAITFORLENGTH)
     {
         if (SerialManager.serialPort.BytesToRead >= 2)
         {
             SerialManager.serialPort.Read(streamBuffer, streamBufferPos, 2);
             streamBufferPos = 3;
             streamState     = StreamState.WAITFORPAYLOAD;
             SerialManager.SetISRLength(
                 (int)StreamBuilder.GetUnsignedInt(streamBuffer, 1, StreamBuilder.BuilderType.INT16_BYTE));
         }
     }
     else if (streamState == StreamState.WAITFORPAYLOAD)
     {
         int _length = (int)StreamBuilder.GetUnsignedInt(streamBuffer, 1, StreamBuilder.BuilderType.INT16_BYTE);
         if (SerialManager.serialPort.BytesToRead >= _length)
         {
             streamLength = _length + 3;
             SerialManager.serialPort.Read(streamBuffer, streamBufferPos, _length);
             ConsoleData.AddInfo(streamBuffer, streamLength, false);
             if (StreamBuilder.VerifyCheckSum(streamBuffer, 0, (UInt16)(streamLength - 2), streamBuffer[streamLength - 1])
                 == StreamBuilder.ChecksumResult.CHECKSUM_OK)
             {
                 streamRecieved = true;
             }
         }
         streamBufferPos = 0;
         streamState     = StreamState.WAITFORHEAD;
     }
 }