163 lines
4.5 KiB
C#
163 lines
4.5 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Text;
|
|||
|
using System.Threading;
|
|||
|
using Microsoft.VisualBasic;
|
|||
|
using System.IO.Ports;
|
|||
|
using CommonClassLib;
|
|||
|
namespace SimensSerialPort
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Create by Richard.liu 2008
|
|||
|
/// Simens PLCͨѶ<CDA8><D1B6>,RS232<33><32><EFBFBD><EFBFBD><EFBFBD>ɿ<EFBFBD>Э<EFBFBD><D0AD>
|
|||
|
/// </summary>
|
|||
|
public class CSimensSerialPort
|
|||
|
{
|
|||
|
SerialPort com;
|
|||
|
bool _ifInit;
|
|||
|
short _portNo;
|
|||
|
string _settings;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// <20><>ʼ<EFBFBD><CABC>PLC<4C><43><EFBFBD>ɿ<EFBFBD>Э<EFBFBD><D0AD>ͨѶ<CDA8><D1B6>
|
|||
|
/// </summary>
|
|||
|
/// <param name="Portno"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD>COM<4F>ڣ<EFBFBD><DAA3>ţ<EFBFBD>1<EFBFBD><31>2<EFBFBD><32>3<EFBFBD><33>4<EFBFBD><34>5<EFBFBD><35>6<EFBFBD><36>7<EFBFBD><37>8</param>
|
|||
|
/// <param name="Settings">ͨѶ<CDA8><D1B6><EFBFBD>ã<EFBFBD>9600<30><30>None<6E><65>8<EFBFBD><38>One<6E><65><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>ʹ<EFBFBD><CAB9>ȫ<EFBFBD>ǡ<EFBFBD><C7A1><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
public CSimensSerialPort(short Portno, string Settings)
|
|||
|
{
|
|||
|
|
|||
|
_portNo = Portno;
|
|||
|
_settings = Settings;
|
|||
|
com = new SerialPort();
|
|||
|
|
|||
|
}
|
|||
|
string _ComError;
|
|||
|
|
|||
|
|
|||
|
public string ComError
|
|||
|
{
|
|||
|
get { return _ComError; }
|
|||
|
set { _ComError = value; }
|
|||
|
}
|
|||
|
public bool InitCom()
|
|||
|
{
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
|
|||
|
if (com.IsOpen == true)
|
|||
|
{
|
|||
|
com.Close();
|
|||
|
}
|
|||
|
com.PortName ="COM"+ _portNo.ToString();
|
|||
|
char[] cc = new char[1] { '<EFBFBD><EFBFBD>'};
|
|||
|
string[] split = _settings.Split(cc);
|
|||
|
com.BaudRate =Convert.ToInt32( split[0]);
|
|||
|
com.Parity = Parity.None;//(Parity)Convert.ChangeType(split[1], typeof(Parity));
|
|||
|
com.DataBits = Convert.ToInt32(split[2]);
|
|||
|
com.StopBits = StopBits.One;// (StopBits)Convert.ChangeType(split[3], typeof(StopBits));
|
|||
|
|
|||
|
com.Open();
|
|||
|
_ifInit = true;
|
|||
|
return true;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_ComError = "ͨѶ<CDA8>˿<EFBFBD>:" + _portNo + "<22><>ʼ<EFBFBD><CABC>ʧ<EFBFBD><CAA7>!" + ex.Message;
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
public byte[] ReadCom(short InBufCount)
|
|||
|
{
|
|||
|
if (_ifInit == false)
|
|||
|
InitCom();
|
|||
|
int i = 0;
|
|||
|
try
|
|||
|
{
|
|||
|
do
|
|||
|
{
|
|||
|
i++;
|
|||
|
if (i > 5)
|
|||
|
break;
|
|||
|
Thread.Sleep(300);
|
|||
|
|
|||
|
} while (com.BytesToRead < InBufCount);
|
|||
|
if (com.BytesToRead <= 0)
|
|||
|
{
|
|||
|
_ComError = "<22>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>뻺<EFBFBD><EBBBBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB>Ҫ<EFBFBD><D2AA><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>";
|
|||
|
return null;
|
|||
|
}
|
|||
|
byte[] inbyte = new byte[InBufCount];
|
|||
|
com.Read(inbyte, 0, InBufCount);
|
|||
|
//CCarryConvert.WriteDarkCasket(this.ToString(), "Read", com.PortName, inbyte);
|
|||
|
return inbyte;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_ComError = ex.Message;
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
public string ReadCom()
|
|||
|
{
|
|||
|
if (_ifInit == false)
|
|||
|
InitCom();
|
|||
|
try
|
|||
|
{
|
|||
|
string gets = com.ReadExisting();
|
|||
|
//CCarryConvert.WriteDarkCasket(this.ToString(), "ReadExisting", com.PortName, gets);
|
|||
|
return gets;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_ComError = ex.Message;
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
public bool WriteCom(string wData)
|
|||
|
{
|
|||
|
if (_ifInit == false)
|
|||
|
InitCom();
|
|||
|
try
|
|||
|
{
|
|||
|
|
|||
|
com.Write( wData);
|
|||
|
//CCarryConvert.WriteDarkCasket(this.ToString(), "Write", com.PortName, wData);
|
|||
|
return true;
|
|||
|
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_ComError = "д<><D0B4><EFBFBD>ڻ<EFBFBD><DABB><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>" + ex.Message;
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
public bool WriteCom(byte[] wData)
|
|||
|
{
|
|||
|
if (_ifInit == false)
|
|||
|
InitCom();
|
|||
|
try
|
|||
|
{
|
|||
|
//com.WriteBufferSize = 0;
|
|||
|
com.Write(wData, 0, wData.GetUpperBound(0)+1);
|
|||
|
//CCarryConvert.WriteDarkCasket(this.ToString(), "Write", com.PortName, wData);
|
|||
|
return true;
|
|||
|
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_ComError = "д<><D0B4><EFBFBD>ڻ<EFBFBD><DABB><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>" + ex.Message;
|
|||
|
return false;
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
public void CloseCom()
|
|||
|
{
|
|||
|
if (com.IsOpen==true) com.Close();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|