private void button1_Click(object sender, EventArgs ev) { Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); s.Connect("192.168.16.170", 10000); byte[] buf; buf = new byte[] { 0x00, 0x06, 0x10, 0x12, 0x00, 0x00 }; s.Send(buf); byte[] rec = new byte[128]; int r = 0; s.ReceiveTimeout = 1000; try { r = s.Receive(rec); Debug.WriteLine(FUNC.BytesToString(rec, 0, r)); r = s.Receive(rec); Debug.WriteLine(FUNC.BytesToString(rec, 0, r)); } catch (Exception e) { Debug.WriteLine(e.Message); } s.Disconnect(false); }
static void ExcuteCmd(byte addr, byte cmd) { byte[] buf = new byte[15]; buf[0] = Convert.ToByte('w'); buf[1] = Convert.ToByte('a'); buf[2] = Convert.ToByte('t'); buf[3] = addr; for (int i = 4; i < 14; i++) { //buf[i] = (byte)FUNC.Random.Next(256); buf[i] = 0; } if (!list.Keys.Contains(addr)) { CardData d = new CardData(); Random r = new Random(); d.a = (Int16)r.Next(1000); d.b = (Int16)r.Next(2000); d.last = DateTime.Now; list.Add(addr, d); } TimeSpan t = new TimeSpan(DateTime.Now.Ticks - list[addr].last.Ticks); if (t.TotalSeconds >= 3) { list[addr].a++; list[addr].b++; list[addr].last = DateTime.Now; } buf[4] = addr; buf[7] = (byte)((cmd == 0x10)? 1 : 0); buf[10] = (byte)(list[addr].a / 256); buf[11] = (byte)(list[addr].a % 256); buf[12] = (byte)(list[addr].b / 256); buf[13] = (byte)(list[addr].b % 256); buf[14] = FUNC.CRC8(buf, 0, 14); //com.Write(buf, 3, 2); com.Write(buf, 0, 15); WriteLine("send: " + FUNC.BytesToString(buf, 0, 15)); WriteLine(""); }
static void Main(string[] args) { String port = "COM9"; if (args.Length > 0) { port = args[0]; } if (args.Length > 1) { Process.Start(args[1]); } try { com = new SerialPort(port, 57600, Parity.None, 8, StopBits.One); } catch (Exception e) { Console.WriteLine(e.Message); Console.ReadKey(false); return; } com.Open(); while (true) { byte[] buf = new byte[10]; com.ReadTimeout = Timeout.Infinite; int n = com.Read(buf, 0, 10); //FUNC.Sleep(1000); //int n = 10; Write("recv: " + FUNC.BytesToString(buf, 0, n)); byte c = FUNC.CRC8(buf, 0, n - 1); if ((n == 10) && (c == buf[9])) { WriteLine(" [OK]"); ExcuteCmd(buf[3], buf[4]); } else { WriteLine(" [Failed]"); } FUNC.Sleep(30); } }