3C科技 娛樂遊戲 美食旅遊 時尚美妝 親子育兒 生活休閒 金融理財 健康運動 寰宇綜合

Zi 字媒體

2017-07-25T20:27:27+00:00
加入好友
C# TCP/IP 單晶片通訊 (CS TCP single-chip microcomputer) GITHUB: https://github.com/jash-git/CS-TCP-single-chip-microcomputer using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Sockets;// add lib using System.IO;// add lib using System.Threading; namespace V8_command_sample { public partial class Form1 : Form { //--- //add public member public TcpClient clientSocket = new System.Net.Sockets.TcpClient(); public BinaryReader br; public BinaryWriter bw; public String StrResult = ""; //---add public member public Form1() { InitializeComponent(); } private void SendCommand(byte []Command,int iResult) { NetworkStream clientStream = clientSocket.GetStream(); bw = new BinaryWriter(clientStream); bw.Write(Command); Thread.Sleep(100); byte[] inStream = new byte[2000]; br = new BinaryReader(clientStream); int intLen = br.Read(inStream, 0, iResult); StrResult = ""; StrResult = ""; for (int i = 0; i < intLen; i++)//show hex { if (i == 0) { StrResult += Convert.ToString(inStream[i], 16).ToUpper().PadLeft(2, '0'); } else { StrResult += " " + Convert.ToString(inStream[i], 16).ToUpper().PadLeft(2, '0'); } } } private void ShowResult(String StrButtMsg) { String[] StrData = StrResult.Split(' '); String StrShowMsg = StrResult + "\n\n\n"; StrShowMsg += String.Format("{0} :STX\n", StrData[0]); StrShowMsg += String.Format("{0} {1} :length\n", StrData[1], StrData[2]); StrShowMsg += String.Format("{0} {1} {2} {3} :command: 4 bytes\n", StrData[3], StrData[4], StrData[5], StrData[6]); StrShowMsg += String.Format("{0} :Num: 1 bytes\n", StrData[7]); StrShowMsg += String.Format("{0} {1} {2} {3} :index: 4 bytes\n", StrData[8], StrData[9], StrData[10], StrData[11]); StrShowMsg += String.Format("{0} {1} {2} :Reverse: 3 bytes", StrData[12], StrData[13], StrData[14]); MessageBox.Show(StrShowMsg, StrButtMsg, MessageBoxButtons.OK, MessageBoxIcon.Information); } private void initUI(bool state=true) { buttConnect.Enabled = state; buttAddCard.Enabled = !state; buttDeleteCard.Enabled = !state; buttSetTime.Enabled = !state; buttGetRecord.Enabled = !state; txtrecord.Text = ""; } private void Form1_Load(object sender, EventArgs e) { initUI(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if( (clientSocket!=null) && (clientSocket.Connected) ) { clientSocket.Close(); } } private void buttConnect_Click(object sender, EventArgs e) { try { clientSocket.Connect(txtIP.Text, Convert.ToInt32(txtPort.Text)); if (clientSocket.Connected) { initUI(false); } } catch { } } private void buttAddCard_Click(object sender, EventArgs e) { int ishift = 0; byte[] card = new byte[8]; byte[] index = new byte[4]; byte[] name = new byte[32]; byte[] bSend = new byte[103]; byte[] pin = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; byte[] validdate = new byte[] { 0x07, 0xD0, 0x01, 0x01, 0x00, 0x00, 0x08, 0x14, 0x01, 0x01, 0x00, 0x00 }; byte[] parameter = new byte[] { 0x00, 0x0F, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00 }; byte[] status_week = new byte[] { 0x11, 0xFF }; byte[] timer = new byte[] { 0x00, 0x00, 0x17, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; byte[] reverse = new byte[] { 0x00, 0x00 }; String StrBuf = ""; byte[] ArraySRC = System.Text.Encoding.ASCII.GetBytes(txtCard.Text); Array.Reverse(ArraySRC); StrBuf = System.Text.Encoding.ASCII.GetString(ArraySRC); for (int i=0;i<8;i++) { card[i]= Convert.ToByte(Convert.ToInt32(txtCard.Text.Substring(i * 2, 2), 16)); } StrBuf = Convert.ToString(Convert.ToInt32(txtIndex.Text), 16); StrBuf = StrBuf.PadLeft(8, '0'); for (int i = 0; i < 4; i++) { index[i] = Convert.ToByte(Convert.ToInt32(StrBuf.Substring(i * 2, 2), 16)); } UnicodeEncoding Unicode = new UnicodeEncoding(); StrBuf = txtName.Text; if (StrBuf.Length<16) { StrBuf = StrBuf.PadRight(16, '\0'); } int byteCount = Unicode.GetByteCount(StrBuf.ToCharArray(), 0, 16); int bytesEncodedCount = Unicode.GetBytes(StrBuf, 0, 16, name, 0); for(int i=0;i

本文由jashliaoeuwordpress提供 原文連結

寫了 5860316篇文章,獲得 23313次喜歡
精彩推薦