Beispiel #1
0
 static public void Event()
 {
     if (SerialManager.serialPort.IsOpen && portConnected)
     {
         if (eventState == EventState.WAITCONNECT)
         {
             eventContext = HOST_STREAM_CONNECT_REQ;
             int _length = BuildStream(streamBuffer, eventContext);
             streamRecieved  = false;
             streamBufferPos = 0;
             streamState     = StreamState.WAITFORHEAD;
             SerialManager.SetISRLength(1);
             SerialManager.SendStream(streamBuffer, _length);
             eventState = EventState.WAITRESP;
         }
         else if (eventState == EventState.SEND)
         {
             int _length = BuildStream(streamBuffer, eventContext);
             streamRecieved  = false;
             streamBufferPos = 0;
             streamState     = StreamState.WAITFORHEAD;
             SerialManager.SetISRLength(1);
             SerialManager.SendStream(streamBuffer, _length);
             eventState = EventState.WAITRESP;
         }
         else if (eventState == EventState.WAITRESP)
         {
             if (streamRecieved)
             {
                 if (hostState == HostState.CONNECTED || eventContext == HOST_STREAM_CONNECT_REQ)
                 {
                     ProcessStream(streamBuffer, eventContext);
                     if (eventContext == HOST_STREAM_CONNECT_REQ && hostState == HostState.CONNECTED)
                     {
                         eventContext = HOST_STREAM_SPEED_REQ;
                         eventState   = EventState.SEND;
                     }
                     else if (eventContext != HOST_STREAM_CONNECT_REQ)
                     {
                         eventContext = GetNextContext(eventContext);
                         eventState   = EventState.SEND;
                     }
                 }
             }
         }
     }
     else
     {
         hostState       = HostState.DISCONNECTED;
         eventState      = EventState.WAITCONNECT;
         eventContext    = HOST_STREAM_CONNECT_REQ;
         streamRecieved  = false;
         streamBufferPos = 0;
         streamState     = StreamState.WAITFORHEAD;
         SerialManager.SetISRLength(1);
     }
 }
Beispiel #2
0
        static public bool StartCommunicaiton(string _com, int _baud)
        {
            bool retValue = false;

            try
            {
                SerialManager.Connect(_com, _baud);
                if (SerialManager.serialPort.IsOpen)
                {
                    portConnected = true;
                    SystemTimer.InitTimer();
                    retValue = true;
                }
            }
            catch (Exception) { };
            return(retValue);
        }
Beispiel #3
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;
     }
 }