Пример #1
0
    /// <summary>
    /// Queues a packet to send.
    /// </summary>
    /// <param name="datum">
    /// A pre-created packet of command, arg, and callback.
    /// </param>
    public void SendPacket(TxPacket datum)
    {
        GanglionCommand aCommand = datum.aCmd;
        int             anArg    = datum.anArgument;

        if (aCommand == GanglionCommand.Value)
        {
            m_logFile.Write(anArg);
            m_charsWritten += anArg.ToString().Length + 1;
        }
        else
        {
            m_logFile.Write(Ganglion.kCmdToStr[aCommand]);
            m_charsWritten += aCommand.ToString().Length + 1;
        }

        if (aCommand == GanglionCommand.Step ||
            aCommand == GanglionCommand.Heat ||
            aCommand == GanglionCommand.StepRate ||
            aCommand == GanglionCommand.Stop ||
            aCommand == GanglionCommand.StepRateStep ||
            m_charsWritten > 60)
        {
            m_logFile.WriteLine();
            m_charsWritten = 0;
        }
        else
        {
            m_logFile.Write(" ");
        }
    }
Пример #2
0
            private void Test()
            {
                //var str = \"{\\"stat\\":{\\"time\\":\\"2019-10-25 07:05:33 UTC\\",\\"lati\\":\\"31.231013\\",\\"long\\":\\"121.200607\\",\\"alti\\":\\"30.200000\\",\\"rxnb\\":0,\\"rxok\\":0,\\"rxfw\\":0,\\"ackr\\":100.0,\\"dwnb\\":0,\\"txnb\\":0,\\"batt\\":0,\\"poe\\":0,\\"net\\":1,\\"traffic\\":780539002,\\"ver\\":\\"V3.0.864.862.868_Release\\"}}\";
                //var str = "{\"rxpk\":[{\"tmst\":196287580,\"chan\":5,\"rfch\":1,\"freq\":474.100000,\"stat\":1,\"modu\":\"LORA\",\"datr\":\"SF12BW125\",\"codr\":\"4/5\",\"lsnr\":-12.5,\"rssi\":-124,\"size\":50,\"data\":\"gHMAEHCADwsB3P7NADg1Rsj2FLImBtz/9e3hVNzniwoMGUhlyC4KI8Lsvt1VKSuSyVM=\"}]}";
                var str = "{\"rxpk\":[{\"tmst\":438505452,\"chan\":2,\"rfch\":0,\"freq\":473.500000,\"stat\":1,\"modu\":\"LORA\",\"datr\":\"SF12BW125\",\"codr\":\"4/5\",\"lsnr\":-14.0,\"rssi\":-119,\"size\":50,\"data\":\"gGEAEHCACwABRaWG3UZsomFqt4sxJmt0JGNFCS3PWweysY1Vi+94PmFTmoycmDxCviA=\"}]}";
                //var str = "{\"txpk\":{\"imme\":true,\"freq\":864.123456,\"rfch\":0,\"powe\":14,\"modu\":\"LORA\",\"datr\":\"SF11BW125\",\"codr\":\"4/6\",\"ipol\":false,\"size\":32,\"data\":\"H3P3N2i9qc4yt7rK7ldqoeCVJGBybzPY5h1Dd7P7p8v\"}}";
                var js = new JsonParser(str).Decode() as IDictionary <String, Object>;
                //var st = JsonHelper.Convert<StatModel>(js[\"stat\"]);

                //Console.WriteLine(st.ToJson(true));

                var st = StatModel.Read(js["stat"]);

                if (st != null)
                {
                    Console.WriteLine(st.ToJson(true));
                }

                var dt = RxPacket.Read(js["rxpk"]);

                if (dt.Length > 0)
                {
                    Console.WriteLine(dt.ToJson(true));

                    var dp = dt[0];
                    if (!dp.Data.IsNullOrEmpty())
                    {
                        Packet pk = dp.Data.ToBase64();
                        Console.WriteLine(pk.ToHex(64));
                        Console.WriteLine(pk.ToStr());

                        var pm = new PHYMessage();
                        pm.Read(pk.GetStream(), null);

                        Console.WriteLine(pm.ToJson(true));

                        var nwkSkey = "4B463EFED018F099FE3F05108618FDDA".ToHex();
                        //var appSkey = "19E52095515EBD0C2FD596DD96FD0833".ToHex();
                        var appSkey = "778960777F7B4CBAC857C06DEE818844".ToHex();
                        var buf     = pm.Decrypt(nwkSkey, appSkey);
                        Console.WriteLine(buf.ToHex());
                        Console.WriteLine(buf.ToStr());
                    }
                }

                var tx = TxPacket.Read(js["txpk"]);

                if (tx != null)
                {
                    Console.WriteLine(tx.ToJson(true));
                }
            }
Пример #3
0
    void TransmitData()
    {
        byte[] txBuffer       = new byte[Printer.kRxBufferSize];
        int    bytesAvailable = 0;

        TxLog("Starting tx loop.");
        while (m_threadsActive)
        {
            try {
                while (m_threadsActive && bytesAvailable < kMinRxBufferSpaceRequired)
                {
                    // Ensure that the RX buffer updates.
                    Thread.Sleep(kSecToMs * 2);

                    // Update our bytes free before sending data.
                    m_dispatchSemaphore.WaitOne();
                    bytesAvailable        = m_lastBufferAvailable;
                    m_lastBufferAvailable = 0;
                    m_dispatchSemaphore.Release();
                }

                TxLog("Bytes available: {0}.", bytesAvailable);
                while (m_threadsActive && m_dataBeingTransmitted.Count < 1)
                {
                    bool swapped = false;
                    m_packetSemaphore.WaitOne();
                    if (m_dataToTransmit.Count > 0)
                    {
                        TxLog("Acquired new tx buffer.");
                        swapped = true;
                        Queue <TxPacket> temp = m_dataToTransmit;
                        m_dataToTransmit       = m_dataBeingTransmitted;
                        m_dataBeingTransmitted = temp;
                    }
                    m_packetSemaphore.Release();

                    // NOTE(kevin): This is where we'll spend most of our
                    // time waiting for commands. 200ms is approx. the
                    // speed of a double click.
                    if (!swapped)
                    {
                        Thread.Sleep(200);
                    }
                }

                int numTxBytes = 0;
                TxLog("Packets pending: {0}", m_dataBeingTransmitted.Count);
                while (m_threadsActive &&
                       m_dataBeingTransmitted.Count > 0 &&
                       bytesAvailable >= kMinRxBufferSpaceRequired)
                {
                    TxPacket aPacket = m_dataBeingTransmitted.Dequeue();

                    // Figure out what we need to send.
                    switch (aPacket.aCmd)
                    {
                    case GanglionCommand.Value:
                        int aValue = aPacket.anArgument;
                        if (aValue <= 127)
                        {
                            aValue += 128;
                            txBuffer[numTxBytes++] = (byte)aValue;
                            bytesAvailable--;
                        }
                        else if (aValue <= 0xFF)
                        {
                            txBuffer[numTxBytes++] = (byte)GanglionCommand.Value1;
                            txBuffer[numTxBytes++] = (byte)(aValue & 0xFF);
                            bytesAvailable        -= 2;
                        }
                        else if (aValue <= 0xFFFF)
                        {
                            txBuffer[numTxBytes++] = (byte)GanglionCommand.Value2;
                            txBuffer[numTxBytes++] = (byte)(aValue & 0xFF);
                            txBuffer[numTxBytes++] = (byte)((aValue >> 8) & 0xFF);
                            bytesAvailable        -= 3;
                        }
                        else if (aValue <= 0xFFFFFF)
                        {
                            txBuffer[numTxBytes++] = (byte)GanglionCommand.Value3;
                            txBuffer[numTxBytes++] = (byte)(aValue & 0xFF);
                            txBuffer[numTxBytes++] = (byte)((aValue >> 8) & 0xFF);
                            txBuffer[numTxBytes++] = (byte)((aValue >> 16) & 0xFF);
                            bytesAvailable        -= 4;
                        }
                        else
                        {
                            txBuffer[numTxBytes++] = (byte)GanglionCommand.Value4;
                            txBuffer[numTxBytes++] = (byte)(aValue & 0xFF);
                            txBuffer[numTxBytes++] = (byte)((aValue >> 8) & 0xFF);
                            txBuffer[numTxBytes++] = (byte)((aValue >> 16) & 0xFF);
                            txBuffer[numTxBytes++] = (byte)((aValue >> 24) & 0xFF);
                            bytesAvailable        -= 5;
                        }
                        break;

                    default:
                        if (aPacket.aCmd == GanglionCommand.Error)
                        {
                            TxLog("ERROR: Sending GanglionCommand.Error.");
                        }
                        txBuffer[numTxBytes++] = (byte)aPacket.aCmd;
                        bytesAvailable--;
                        break;
                    }
                }

                TxLog("Bytes to send: {0}", numTxBytes);
                if (numTxBytes > 0)
                {
                    int bytesSent = m_serialPort.Write(txBuffer, numTxBytes);
                    if (bytesSent >= 0)
                    {
                        TxLog("Bytes sent: {0}.\n", bytesSent);
                    }
                    else
                    {
                        TxLog("Write error: {0}.\n", m_serialPort.error);
                    }
                }
            }
            catch (System.Threading.ThreadAbortException) {
                break;
            }
            catch (System.Exception e) {
                TxLog(e.ToString());
            }
        }
        TxLog("Closing.");
    }
Пример #4
0
 /// <summary>
 /// Sends a pre-made packet.
 /// </summary>
 /// <param name='datum'>
 /// Datum.
 /// </param>
 public void SendPacket(TxPacket datum)
 {
     Contract.Assert(m_lockAcquired, @"Lock not acquired to write data; call BeginSendingPackets and EndSendingPackets.");
     m_dataToTransmit.Enqueue(datum);
 }
Пример #5
0
 /// <summary>
 /// Queues a packet to send.
 /// </summary>
 /// <param name="datum">
 /// A pre-created packet of command, arg, and callback.
 /// </param>
 public void SendPacket(TxPacket datum)
 {
     SendPacket(datum.aCmd, datum.anArgument);
 }
Пример #6
0
    public int UpdateState(TickProfile fromProfile, List <TxPacket> forList)
    {
        int          targetMotorId = fromProfile.motor.id;
        PrinterMotor targetMotor   = targetMotorId < kBaseMotors
                        ? GetBaseMotor(targetMotorId) : extruders[targetMotorId - kBaseMotors];

        Contract.Assert(targetMotor.id == targetMotorId, @"Found motor {0} but id isn't {1}.",
                        targetMotor, targetMotorId);

        bool shouldSendDirection = targetMotor.stepDirection != fromProfile.direction;
        bool shouldSendStepSize  = targetMotor.stepSize != fromProfile.size;
        bool shouldSendStepRate  = targetMotor.stepRate != fromProfile.stepRate;

        if (shouldSendDirection || shouldSendStepSize || shouldSendStepRate)
        {
            if (currentMotorId != targetMotorId)
            {
                // Select the motor; note that we don't change the stack.
                if (lastEnqueuedPacket != null && lastEnqueuedPacket.aCmd == GanglionCommand.StepRate)
                {
                    lastEnqueuedPacket.aCmd = kStepRateMotor[targetMotorId];
                }
                else
                {
                    lastEnqueuedPacket = new TxPacket(kMotorToCommand[targetMotorId]);
                    forList.Add(lastEnqueuedPacket);
                }
                currentMotorId = targetMotorId;
            }
            Contract.Assert(currentMotorId == targetMotor.id,
                            @"Motor id mismatch: {0} != {1}", currentMotorId, targetMotor.id);

            if (shouldSendDirection)
            {
                lastEnqueuedPacket = new TxPacket(fromProfile.direction == StepDirection.Ccw
                                                                  ? GanglionCommand.CounterClockwise
                                                                  : GanglionCommand.Clockwise);
                forList.Add(lastEnqueuedPacket);
                targetMotor.stepDirection = fromProfile.direction;
            }
            if (shouldSendStepSize)
            {
                int ganglionSize = kStepConversion[fromProfile.size];
                lastEnqueuedPacket = new TxPacket(GanglionCommand.Value, ganglionSize);
                forList.Add(lastEnqueuedPacket);

                lastEnqueuedPacket = new TxPacket(GanglionCommand.StepSize);
                forList.Add(lastEnqueuedPacket);
                targetMotor.stepSize = fromProfile.size;

                m_lastNumberSent = ganglionSize;
            }
            if (shouldSendStepRate)
            {
                if (fromProfile.stepRate == 0)
                {
                    if (lastEnqueuedPacket != null && kMotorStop.ContainsKey(lastEnqueuedPacket.aCmd))
                    {
                        lastEnqueuedPacket.aCmd = kMotorStop[lastEnqueuedPacket.aCmd];
                    }
                    else
                    {
                        lastEnqueuedPacket = new TxPacket(GanglionCommand.Stop);
                        forList.Add(lastEnqueuedPacket);
                    }
                }
                else
                {
                    if (m_lastNumberSent != fromProfile.stepRate)
                    {
                        lastEnqueuedPacket = new TxPacket(GanglionCommand.Value, fromProfile.stepRate);
                        forList.Add(lastEnqueuedPacket);
                        m_lastNumberSent = fromProfile.stepRate;
                    }
                    lastEnqueuedPacket = new TxPacket(GanglionCommand.StepRate);
                    forList.Add(lastEnqueuedPacket);
                }
                targetMotor.stepRate = fromProfile.stepRate;
            }
        }

        Contract.Assert(targetMotor.stepDirection == fromProfile.direction,
                        @"Direction mismatch: {0} != {1}.", targetMotor.stepDirection, fromProfile.direction);
        Contract.Assert(targetMotor.stepSize == fromProfile.size,
                        @"Step size mismatch: {0} != {1}", targetMotor.stepSize, fromProfile.size);
        Contract.Assert(targetMotor.stepRate == fromProfile.stepRate,
                        @"Step rate mismatch: {0} != {1}.", targetMotor.stepRate, fromProfile.stepRate);

        return(m_lastNumberSent);
    }