Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using Microsoft.SPOT.Hardware;
- using Microsoft.SPOT;
- namespace test
- {
- public class Program
- {
- // ReSharper disable once InconsistentNaming
- private const string TAG = "test";
- private static void SendNak(I2CDevice i2c)
- {
- Debug.Print("!ack 7 !nak: sending nak");
- var pkgNak = I2CDevice.CreateWriteTransaction(nak);
- var written = i2c.Execute(new I2CDevice.I2CTransaction[] { pkgNak }, 100);
- Debug.Print("written: " + written);
- }
- private static void SendAck(I2CDevice i2c)
- {
- Debug.Print("sending ack");
- var pkgNak = I2CDevice.CreateWriteTransaction(ack);
- var written = i2c.Execute(new I2CDevice.I2CTransaction[] { pkgNak }, 100);
- Debug.Print("written: " + written);
- }
- static byte[] pkg = {
- 0x17, 0x13, //stx
- 0x06, 0x8e, 0x79, 0x06, //ecrRegNUm
- 0x00, 0x00, 0x37, 0x78, //sknoSerial
- 0xaa, //cmd
- 0x00, 0x25, //len
- 0x00, 0x01, //pkg cnt
- 0x00, 0x01, //pkg num
- //data
- 0x30, 0x30, 0x30, 0x30, 0x30, 0x32, 0x31, 0x36, 0x30, 0x30, 0x30, 0x30, 0x32, //ecrSerial (13 bytes)
- 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0xca, 0xe0, 0xf1, 0xe1, 0xe8, 0x2d, 0x30, 0x33, 0xcc, 0xd4, //ecr model (20 bytes)
- 0x05, 0xf5, 0xe8, 0x57, //unp (4 bytes)
- 0xb7, 0x6d, //crc
- 0x17, 0x05 //etx
- };
- static byte[] req = { 0x17, 0x33 };
- byte[] soh = { 0x17, 0x02 };
- static byte[] eot = { 0x17, 0x04 };
- static byte[] stx = { 0x17, 0x13 };
- static byte[] etx = { 0x17, 0x05 };
- static byte[] ack = { 0x17, 0xAA };
- static byte[] nak = { 0x17, 0x07 };
- public static void Main()
- {
- try
- {
- Debug.Print("Test initialized!");
- //outputs: //TODO: check for 0x37 byte
- var addr = (byte)0x02;
- var rate = 100; //kHz
- var pkgTimeout = 100; //ms
- var transactionTimeout = 10; //?
- Debug.Print("Sleeping 10 seconds - waiting for SKNO initialization");
- Thread.Sleep(10000);
- while (true)
- {
- Debug.Print("Main cycle started.");
- var i2c = new I2CDevice(new I2CDevice.Configuration(addr, rate));
- //wait init device
- Thread.Sleep(2000);
- var pkgOut = new byte[2];
- while (pkgOut[0] == 0x00)
- {
- Debug.Print("Sending identification package");
- var pkgTx = I2CDevice.CreateWriteTransaction(pkg);
- var written = i2c.Execute(new I2CDevice.I2CTransaction[] { pkgTx }, 100);
- Debug.Print("written: " + written);
- Thread.Sleep(100);
- Debug.Print("Trying to read answer");
- var pkgRx = I2CDevice.CreateReadTransaction(pkgOut);
- var read = i2c.Execute(new I2CDevice.I2CTransaction[] { pkgRx }, 100);
- Debug.Print("readCnt: " + read);
- Debug.Print("result: " + GetStr(pkgOut));
- }
- //NACK?
- if (pkgOut[0] == nak[0] && pkgOut[1] == nak[1])
- {
- Debug.Print("nak");
- SendNak(i2c);
- Debug.Print("restart");
- continue;
- }
- //ACK?
- if (pkgOut[0] != ack[0] && pkgOut[1] != ack[1])
- {
- Debug.Print("bus fuckup");
- SendNak(i2c);
- Debug.Print("restart");
- continue;
- }
- Debug.Print("ACK. We have a winner! Let's wait 2 secs and send REQ to check state");
- Thread.Sleep(2000);
- //REQ
- var reqOut = new byte[6];
- while (reqOut[0] == 0x00)
- {
- Debug.Print("Sending REQ");
- var pkgTx = I2CDevice.CreateWriteTransaction(req);
- var written = i2c.Execute(new I2CDevice.I2CTransaction[] { pkgTx }, 100);
- Debug.Print("written: " + written);
- Thread.Sleep(10);
- var pkgRx = I2CDevice.CreateReadTransaction(reqOut);
- var read = i2c.Execute(new I2CDevice.I2CTransaction[] { pkgRx }, 100);
- Debug.Print("readCnt: " + read);
- Thread.Sleep(20);
- Debug.Print("result: " + GetStr(reqOut));
- if (reqOut[4] == etx[0] && reqOut[5] == etx[1])
- {
- Debug.Print("etx");
- reqOut[0] = 0x00; //отправка заново
- }
- }
- Debug.Print("Some answer acquired, checking for STX");
- if (reqOut[4] == eot[0] && reqOut[5] == eot[1])
- {
- Debug.Print("eot");
- Thread.Sleep(1000);
- SendNak(i2c);
- Debug.Print("restart");
- continue;
- }
- //send req again
- if (reqOut[4] == stx[0] && reqOut[5] == stx[1])
- {
- Debug.Print("STX received");
- SendAck(i2c);
- var reqFullOut = new byte[27]; //6+21
- while (reqFullOut[0] == 0x00)
- {
- Debug.Print("Trying to read 27 bytes - 6 status bytes + 21 package bytes");
- var pkgTx = I2CDevice.CreateWriteTransaction(req);
- var written = i2c.Execute(new I2CDevice.I2CTransaction[] { pkgTx }, 100);
- Debug.Print("written: " + written);
- Thread.Sleep(10);
- var pkgRx = I2CDevice.CreateReadTransaction(reqFullOut);
- var read = i2c.Execute(new I2CDevice.I2CTransaction[] { pkgRx }, 100);
- Debug.Print("readCnt: " + read);
- Thread.Sleep(20);
- Debug.Print("result: " + GetStr(reqFullOut));
- if (reqFullOut[4] == etx[0] && reqFullOut[5] == etx[1])
- {
- Debug.Print("etx. retransmit");
- reqFullOut[0] = 0x00; //отправка заново
- }
- }
- SendAck(i2c);
- Debug.Print("Test succesfully completed!");
- }
- Thread.Sleep(1000);
- }
- }
- catch (Exception e)
- {
- Thread.Sleep(1000);
- Debug.Print("Exception caught! " + e.Message);
- Debug.Print("Exception trace: " + e.StackTrace);
- }
- }
- private static string GetStr(byte[] array)
- {
- var res = "";
- for (var i = 0; i < array.Length; i++)
- res += array[i].ToString("X2") + " ";
- return res;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement