你的位置:首页 > ASP.net教程

[ASP.net教程]编写了一个简单的串口映射工具


编写了一个简单的串口映射工具,方便调试串口

代码:

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