Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.86 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using Microsoft.SPOT.Hardware;
  4. using Microsoft.SPOT;
  5.  
  6. namespace test
  7. {
  8.     public class Program
  9.     {
  10.         // ReSharper disable once InconsistentNaming
  11.         private const string TAG = "test";
  12.         private static void SendNak(I2CDevice i2c)
  13.         {
  14.             Debug.Print("!ack 7 !nak: sending nak");
  15.             var pkgNak = I2CDevice.CreateWriteTransaction(nak);
  16.             var written = i2c.Execute(new I2CDevice.I2CTransaction[] { pkgNak }, 100);
  17.             Debug.Print("written: " + written);
  18.         }
  19.         private static void SendAck(I2CDevice i2c)
  20.         {
  21.             Debug.Print("sending ack");
  22.             var pkgNak = I2CDevice.CreateWriteTransaction(ack);
  23.             var written = i2c.Execute(new I2CDevice.I2CTransaction[] { pkgNak }, 100);
  24.             Debug.Print("written: " + written);
  25.         }
  26.  
  27.         static byte[] pkg =  {
  28.             0x17, 0x13, //stx
  29.             0x06, 0x8e, 0x79, 0x06, //ecrRegNUm
  30.             0x00, 0x00, 0x37, 0x78, //sknoSerial
  31.             0xaa, //cmd
  32.             0x00, 0x25, //len
  33.             0x00, 0x01, //pkg cnt
  34.             0x00, 0x01, //pkg num
  35.  
  36.             //data
  37.             0x30, 0x30, 0x30, 0x30, 0x30, 0x32, 0x31, 0x36, 0x30, 0x30, 0x30, 0x30, 0x32, //ecrSerial (13 bytes)
  38.             0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0xca, 0xe0, 0xf1, 0xe1, 0xe8, 0x2d, 0x30, 0x33, 0xcc, 0xd4, //ecr model (20 bytes)
  39.             0x05, 0xf5, 0xe8, 0x57, //unp (4 bytes)
  40.  
  41.             0xb7, 0x6d, //crc
  42.             0x17, 0x05 //etx
  43.         };
  44.  
  45.         static byte[] req = { 0x17, 0x33 };
  46.         byte[] soh = { 0x17, 0x02 };
  47.         static byte[] eot = { 0x17, 0x04 };
  48.         static byte[] stx = { 0x17, 0x13 };
  49.         static byte[] etx = { 0x17, 0x05 };
  50.         static byte[] ack = { 0x17, 0xAA };
  51.         static byte[] nak = { 0x17, 0x07 };
  52.  
  53.         public static void Main()
  54.         {
  55.            
  56.             try
  57.             {
  58.                 Debug.Print("Test initialized!");
  59.                
  60.                 //outputs: //TODO: check for 0x37 byte
  61.  
  62.                 var addr = (byte)0x02;
  63.                 var rate = 100; //kHz
  64.                 var pkgTimeout = 100; //ms
  65.                 var transactionTimeout = 10; //?
  66.  
  67.                 Debug.Print("Sleeping 10 seconds - waiting for SKNO initialization");
  68.                 Thread.Sleep(10000);
  69.                 while (true)
  70.                 {
  71.                     Debug.Print("Main cycle started.");
  72.                     var i2c = new I2CDevice(new I2CDevice.Configuration(addr, rate));
  73.                     //wait init device
  74.                     Thread.Sleep(2000);
  75.  
  76.                     var pkgOut = new byte[2];
  77.                     while (pkgOut[0] == 0x00)
  78.                     {
  79.                         Debug.Print("Sending identification package");
  80.                         var pkgTx = I2CDevice.CreateWriteTransaction(pkg);
  81.                         var written = i2c.Execute(new I2CDevice.I2CTransaction[] { pkgTx }, 100);
  82.                         Debug.Print("written: " + written);
  83.                         Thread.Sleep(100);
  84.  
  85.                         Debug.Print("Trying to read answer");
  86.                         var pkgRx = I2CDevice.CreateReadTransaction(pkgOut);
  87.                         var read = i2c.Execute(new I2CDevice.I2CTransaction[] { pkgRx }, 100);
  88.                         Debug.Print("readCnt: " + read);
  89.                         Debug.Print("result: " + GetStr(pkgOut));
  90.                     }
  91.  
  92.                     //NACK?
  93.                     if (pkgOut[0] == nak[0] && pkgOut[1] == nak[1])
  94.                     {
  95.                         Debug.Print("nak");
  96.                         SendNak(i2c);
  97.                         Debug.Print("restart");
  98.                         continue;
  99.                     }
  100.  
  101.                     //ACK?
  102.                     if (pkgOut[0] != ack[0] && pkgOut[1] != ack[1])
  103.                     {
  104.                         Debug.Print("bus fuckup");
  105.                         SendNak(i2c);
  106.                         Debug.Print("restart");
  107.                         continue;
  108.                     }
  109.  
  110.                     Debug.Print("ACK. We have a winner! Let's wait 2 secs and send REQ to check state");
  111.                     Thread.Sleep(2000);
  112.  
  113.                     //REQ
  114.                     var reqOut = new byte[6];
  115.                     while (reqOut[0] == 0x00)
  116.                     {
  117.                         Debug.Print("Sending REQ");
  118.                         var pkgTx = I2CDevice.CreateWriteTransaction(req);
  119.                         var written = i2c.Execute(new I2CDevice.I2CTransaction[] { pkgTx }, 100);
  120.                         Debug.Print("written: " + written);
  121.                         Thread.Sleep(10);
  122.  
  123.                         var pkgRx = I2CDevice.CreateReadTransaction(reqOut);
  124.                         var read = i2c.Execute(new I2CDevice.I2CTransaction[] { pkgRx }, 100);
  125.                         Debug.Print("readCnt: " + read);
  126.                         Thread.Sleep(20);
  127.                         Debug.Print("result: " + GetStr(reqOut));
  128.  
  129.                         if (reqOut[4] == etx[0] && reqOut[5] == etx[1])
  130.                         {
  131.                             Debug.Print("etx");
  132.                             reqOut[0] = 0x00; //отправка заново
  133.                         }
  134.                     }
  135.  
  136.                     Debug.Print("Some answer acquired, checking for STX");
  137.                     if (reqOut[4] == eot[0] && reqOut[5] == eot[1])
  138.                     {
  139.                         Debug.Print("eot");
  140.                         Thread.Sleep(1000);
  141.                         SendNak(i2c);
  142.                         Debug.Print("restart");
  143.                         continue;
  144.                     }
  145.                    
  146.                     //send req again
  147.                     if (reqOut[4] == stx[0] && reqOut[5] == stx[1])
  148.                     {
  149.                         Debug.Print("STX received");
  150.                         SendAck(i2c);
  151.                         var reqFullOut = new byte[27]; //6+21
  152.                         while (reqFullOut[0] == 0x00)
  153.                         {
  154.                             Debug.Print("Trying to read 27 bytes - 6 status bytes + 21 package bytes");
  155.                             var pkgTx = I2CDevice.CreateWriteTransaction(req);
  156.                             var written = i2c.Execute(new I2CDevice.I2CTransaction[] { pkgTx }, 100);
  157.                             Debug.Print("written: " + written);
  158.                             Thread.Sleep(10);
  159.  
  160.                             var pkgRx = I2CDevice.CreateReadTransaction(reqFullOut);
  161.                             var read = i2c.Execute(new I2CDevice.I2CTransaction[] { pkgRx }, 100);
  162.                             Debug.Print("readCnt: " + read);
  163.                             Thread.Sleep(20);
  164.                             Debug.Print("result: " + GetStr(reqFullOut));
  165.  
  166.                             if (reqFullOut[4] == etx[0] && reqFullOut[5] == etx[1])
  167.                             {
  168.                                 Debug.Print("etx. retransmit");
  169.                                 reqFullOut[0] = 0x00; //отправка заново
  170.                             }
  171.                         }
  172.                         SendAck(i2c);
  173.                         Debug.Print("Test succesfully completed!");
  174.                     }
  175.  
  176.                     Thread.Sleep(1000);
  177.                 }
  178.             }
  179.             catch (Exception e)
  180.             {
  181.                 Thread.Sleep(1000);
  182.                 Debug.Print("Exception caught! " + e.Message);
  183.                 Debug.Print("Exception trace: " + e.StackTrace);
  184.             }
  185.         }
  186.  
  187.         private static string GetStr(byte[] array)
  188.         {
  189.             var res = "";
  190.             for (var i = 0; i < array.Length; i++)
  191.                 res += array[i].ToString("X2") + " ";
  192.             return res;
  193.         }
  194.  
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement