基本信息
源码名称:COM口访问实例
源码大小:3.72KB
文件格式:.rar
开发语言:C#
更新时间:2019-07-14
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

对COM发送指令并返回数据

namespace SerialPortReceiveData
{
    class Program
    {
        static void Main(string[] args)
        {

            float f1 = -0.970000f;
            float f2 = -0.130000f;

            Console.Write(f1);
            Console.Write('=');
            byte[] b1 = BitConverter.GetBytes(f1);
            ConsoleBytes(b1);
            Console.WriteLine();

            Console.Write(f2);
            Console.Write('=');
            byte[] b2 = BitConverter.GetBytes(f2);
            ConsoleBytes(b2);
            Console.WriteLine();

            byte[] byteFloat = { 0x02 , 0x03 , 0x04 , 0xff , 0xc3 , 0x00 , 0x00 , 0x09 , 0x1b };
            float fd = BitConverter.ToSingle(byteFloat, 0);
            ConsoleBytes(byteFloat);
            Console.Write('=');
            Console.WriteLine(fd);

            string strCom = "COM1";
            if (args.Length > 0)
            {
                strCom = args[0].ToUpper();
            }
            SerialPort mySerialPort = new SerialPort(strCom);

            mySerialPort.BaudRate = 9600;   //比特率
            mySerialPort.Parity = Parity.None;  //奇偶校验
            mySerialPort.StopBits = StopBits.One;//停止位
            mySerialPort.DataBits = 8;//数据位
            mySerialPort.Handshake = Handshake.None;

            //绑定数据接收事件,因为发送是被动的,所以你无法主动去获取别人发送的代码,只能通过这个事件来处理
            //mySerialPort.ReadTimeout = 1000; //读超时,即在1000内未读到数据就引起超时异常

            //mySerialPort.ReadBufferSize = 9;

            mySerialPort.DataReceived = new SerialDataReceivedEventHandler(DataReceivedHandler);

            mySerialPort.Open();

            Console.WriteLine(string.Format("Start to Listen from {0}", strCom));
            int nLoopCount = 10;
            while (nLoopCount-- > 0)
            {
                byte[] byteData = { 0x02, 0x03, 0x00, 0x5D, 0x00, 0x02, 0x55, 0xEA };
                mySerialPort.Write(byteData, 0, byteData.Length);
                Thread.Sleep(1000);
            }
            Console.WriteLine("Press any key to continue...");
            Console.WriteLine();
            Console.ReadKey();
            mySerialPort.Close();
        }

        private static void ConsoleBytes(byte[] byteData)
        {
            foreach (byte b in byteData)
            {
                Console.Write(b.ToString("x"));
                Console.Write(" ");
            }
        }

        static int nReceiveCount = 0;
        static byte[] byteResult = new byte[9];

        private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            //int nData = sp.ReadByte();
            //byteResult[nReceiveCount % 9] = (byte)nData;
            //if (nReceiveCount % 9 == 0 && nReceiveCount > 0)
            //{
            //    Int16 nY = (Int16)(byteResult[3] * 0x100 byteResult[4]);
            //    Int16 nX = (Int16)(byteResult[5] * 0x100 byteResult[6]);
            //    int nnY = (int)nY;
            //    Console.Write("=");
            //    Console.Write(nY / 100.00);
            //    Console.Write(" ");
            //    Console.Write(nnY / 100.00);
            //    Console.Write(" ");
            //    Console.Write(nX / 100.00);
            //    Console.WriteLine();

            //}

            //Console.Write(nData.ToString("x2"));
            //Console.Write(' ');
            //nReceiveCount ;



            //byte[] readBuffer = new byte[sp.ReadBufferSize];
            //sp.Read(readBuffer, 0, readBuffer.Length);



            byte[] byteData = new byte[9];
            sp.Read(byteData, 0, 9);

            for (int i = 0; i < 9; i )
            {
                Console.Write(byteData[i]);
                Console.Write(' ');
            }

            //string indata = sp.ReadExisting();
            //Console.WriteLine(indata);
            //Console.Write("Data Received:");
        }
    }
}