程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 編寫了一個簡單的串口映射工具,編寫串口映射

編寫了一個簡單的串口映射工具,編寫串口映射

編輯:C#入門知識

編寫了一個簡單的串口映射工具,編寫串口映射


編寫了一個簡單的串口映射工具,方便調試串口

代碼:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO.Ports; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace ComMax { public partial class Form1 : Form { private Socket m_ClientSocket; private Socket m_ServerSocket; private Socket m_Client; private SerialPort m_ClientPort; private SerialPort m_ServerPort; private object m_lock = new object(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { Environment.Exit(0); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { } public void m_ClientPort_DataReceived(object obj, SerialDataReceivedEventArgs args) { if (m_ClientPort.BytesToRead > 0) { Thread.Sleep(50); byte[] bArr = new byte[m_ClientPort.BytesToRead]; m_ClientPort.Read(bArr, 0, bArr.Length); m_ClientSocket.Send(ComData.pack(bArr)); } } public void m_ServerPort_DataReceived(object obj, SerialDataReceivedEventArgs args) { if (m_ServerPort.BytesToRead > 0) { Thread.Sleep(50); byte[] bArr = new byte[m_ServerPort.BytesToRead]; m_ServerPort.Read(bArr, 0, bArr.Length); m_Client.Send(ComData.pack(bArr)); } } private void btnServer_Click(object sender, EventArgs e) { btnServer.Enabled = false; btnClient.Enabled = false; m_ServerPort = new SerialPort(txtServerCom.Text.Trim(), 9600, Parity.None, 8, StopBits.One); m_ServerPort.ReadBufferSize = 1024; m_ServerPort.WriteBufferSize = 1024; m_ServerPort.DataReceived += new SerialDataReceivedEventHandler(m_ServerPort_DataReceived); m_ServerPort.Open(); IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 3001); m_ServerSocket = new Socket(ipep.AddressFamily, SocketType.Stream, ProtocolType.Tcp); m_ServerSocket.Bind(ipep); m_ServerSocket.Listen(10); new Thread(new ThreadStart(delegate() { while (true) { m_Client = m_ServerSocket.Accept(); new Thread(new ThreadStart(delegate() { while (true) { byte[] inBuffer = new byte[m_Client.ReceiveBufferSize]; m_Client.Receive(inBuffer, m_Client.ReceiveBufferSize, SocketFlags.None);//如果接收的消息為空 阻塞 當前循環 byte[] data = ComData.unpack(inBuffer); m_ServerPort.Write(data, 0, data.Length); } })).Start(); } })).Start(); } private void btnClient_Click(object sender, EventArgs e) { btnServer.Enabled = false; btnClient.Enabled = false; m_ClientPort = new SerialPort(txtClientCom.Text.Trim(), 9600, Parity.None, 8, StopBits.One); m_ClientPort.ReadBufferSize = 1024; m_ClientPort.WriteBufferSize = 1024; m_ClientPort.DataReceived += new SerialDataReceivedEventHandler(m_ClientPort_DataReceived); m_ClientPort.Open(); IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(txtIP.Text.Trim()), 3001); m_ClientSocket = new Socket(ipep.AddressFamily, SocketType.Stream, ProtocolType.Tcp); m_ClientSocket.Connect(ipep); new Thread(new ThreadStart(delegate() { while (true) { byte[] inBuffer = new byte[m_ClientSocket.ReceiveBufferSize]; m_ClientSocket.Receive(inBuffer, m_ClientSocket.ReceiveBufferSize, SocketFlags.None);//如果接收的消息為空 阻塞 當前循環 byte[] data = ComData.unpack(inBuffer); m_ClientPort.Write(data, 0, data.Length); } })).Start(); } } } View Code

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved