用c#编写pc机与plc的串口通信,需要用到什么技术,需要看哪些方面的书.望高手请教!!!

解决方案 »

  1.   

    /******************************************************************
     * Copyright(c)  : Tangxu
     * Description   : 
     * CreateDate    : 2006-9-01 04:53:08
     * Creater       : Tang.xu
     * LastChangeDate: 
     * LastChanger   : 
     * Version Info  : 1.0.0
     * ******************************************************************/
    using System;
    using System.IO.Ports;
    using System.Threading;
    using System.Text;namespace Tangxu.Common
    {
        public class ReadCom
        {
            public ReadCom() { }        public ReadCom(string sCom,int nBaud) 
            {
                _sComPort = sCom;
                _nBaud = nBaud;
            }        private string _sComPort;
            private int _nBaud;
            private SerialPort _Sp;
            #region Open com port        public bool InitCom()//初始化建串口类实例
            {
                try
                {
                    _Sp = new SerialPort(_sComPort, _nBaud, Parity.None, 8);
                    _Sp.ReceivedBytesThreshold = 10;
                    _Sp.Handshake = Handshake.RequestToSend;
                    _Sp.Parity = Parity.None;
                    _Sp.ReadTimeout = 600;
                    _Sp.WriteTimeout = 600;
                    if (!_Sp.IsOpen)
                    {
                        _Sp.Open();                  
                    }
                    return true;
                }
                catch
                {
                    throw new Exception("Serial Port Can't Open!");
                }
            }
            #endregion        #region FreeDrv
            /// <summary>
            /// free opw
            /// </summary>
            public void FreeDrv()
            {
                if (_Sp != null)
                {
                    _Sp.Dispose();
                    _Sp.Close();
                }
            }
            #endregion        #region Write command to OPW
            /// <summary>
            /// 发操作命令给OPW设备
            /// 并返回状态
            /// </summary>
            /// <param name="sCommand"></param>
            /// <returns></returns>
            public string WriteCommand(string sCommand)
            {
                StringBuilder sb = new StringBuilder();
                _Sp.Write(sCommand+"\r");
                Thread.Sleep(500);
                bool bRead = true;
                while (bRead)
                {
                    byte[] buffer = new byte[_Sp.BytesToRead];
                    _Sp.Read(buffer, 0, buffer.Length);
                    sb.Append(System.Text.Encoding.ASCII.GetString(buffer));
                    if (buffer.Length <= 0)
                    {
                        bRead = false;
                    }
                    Thread.Sleep(300);
                }
                return sb.ToString();
            }
            #endregion        #region Get All COM Port
            public string[] GetAllComPort()
            {
                string[] sAllPort = null;
                try
                {
                    sAllPort = SerialPort.GetPortNames();
                }
                catch (Exception ex)
                {
                    throw new Exception("获取计算机COM口列表失败!\r\n错误信息:" + ex.Message);
                }
                return sAllPort;
            }
            #endregion    }}
      

  2.   

    不同的PLC是有协议的,不只是简单的串口通信。此外还要看结构设计了。
    ------------------------
    兰亭MP3管理大师
    www.longs-soft.com
    ------------------------