C# 串口通信程序 串口通信中转

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;
using System.IO;

namespace Com
{
    public class para
    {
        public SerialPort[] p;
    }
    class Program
    {
        //串口的读是以阻塞方式读取的.本机有两个com口,专门做数据中转调试.
        static void Main(string[] args)
        {
            try
            {
                SerialPort port1 = new SerialPort();
                port1.BaudRate = 115200;
                port1.StopBits = StopBits.One;
                port1.DataBits = 8;
                port1.Encoding = Encoding.Unicode;
                port1.PortName = "COM1";
                port1.Open();


                SerialPort port2 = new SerialPort();
                port2.BaudRate = 115200;
                port2.StopBits = StopBits.One;
                port2.DataBits = 8;
                port2.Encoding = Encoding.Unicode;
                port2.PortName = "COM8";
                port2.Open();

                para ps = new para();
                ps.p = new SerialPort[] { port1, port2 };

                Thread th1 = new Thread(new ParameterizedThreadStart(SendToClient));
                Thread th2 = new Thread(new ParameterizedThreadStart(SendToHost));

                th2.Start(ps);
                th1.Start(ps);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
          
            while (true) ;
        }

        /// <summary>
        /// 从com8 接收 向com1写
        /// </summary>
        /// <param name="p"></param>
        public static void SendToClient(object ps)
        {
            FileStream file = File.Open(@"C:\Com1收到的数据.txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(file);
            para p = ps as para;
            Byte[] bit = new Byte[4096];
            while (true)
            {
                int r = p.p[0].Read(bit, 0, bit.Length);
                p.p[1].Write(bit, 0, r);
                while (--r >= 0)
                {
                    string str = string.Format("0x{0:x} ", bit[r]);
                    sw.Write(str);
                    Console.WriteLine(str);
                }
                sw.WriteLine();
            }
        }

        /// <summary>
        /// 从com1 接收 向com8写
        /// </summary>
        /// <param name="p"></param>
        public static void SendToHost(object ps)
        {
            FileStream file = File.Open(@"C:\Com8收到的数据.txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(file);
            para p = ps as para;
            Byte[] bit = new Byte[4096];
            while (true)
            {
                int r = p.p[1].Read(bit, 0, bit.Length);
                p.p[0].Write(bit, 0, r);
                while (--r >= 0)
                {
                    string str = string.Format("0x{0:x} ", bit[r]);
                    sw.Write(str);
                    Console.WriteLine(str);
                }
                sw.WriteLine();
            }
        }
    }

}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值