南京某山寨运动控制卡厂家封装的DLL
using System;
using System.IO.Ports;
using System.Text;
using System.Threading;
namespace IoRS485
{
public class IOContrl
{
private SerialPort sP = new SerialPort();
public string[] SearchPort()
{
return SerialPort.GetPortNames();
}
public int Open(string sPortName, string sBandRate)
{
int result;
try
{
if (!this.sP.IsOpen)
{
this.sP.PortName = sPortName;
this.sP.BaudRate = Convert.ToInt32(sBandRate);
this.sP.WriteTimeout = 500;
this.sP.ReadTimeout = 500;
this.sP.Open();
}
else
{
this.sP.Close();
this.sP.PortName = sPortName;
this.sP.BaudRate = Convert.ToInt32(sBandRate);
this.sP.Open();
}
this.sP.ReadTimeout = 500;
result = 0;
}
catch
{
result = -1;
}
return result;
}
public int Close()
{
int result;
try
{
if (this.sP.IsOpen)
{
this.sP.Close();
}
result = 0;
}
catch
{
result = -1;
}
return result;
}
public bool BufClear()
{
this.sP.DiscardInBuffer();
return true;
}
public string ReadStr(string cmd)
{
this.sP.DiscardInBuffer();
string text = "@" + cmd + "\r\n";
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
if (text.Length > 0)
{
return text;
}
return "1";
}
public string Reset(int addr)
{
this.sP.DiscardInBuffer();
string text = "@" + addr.ToString() + ":Reset\r\n";
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
if (text.Length > 0)
{
return text;
}
return "1";
}
public string ReadSN(int addr)
{
string text = "";
try
{
this.sP.DiscardInBuffer();
text = "@" + addr.ToString() + ":*IDN?\r\n";
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
text = text.Replace("\n", "");
text = text.Replace("\r", "");
}
catch
{
}
if (text.Length > 0)
{
return text;
}
return "1";
}
public int ReadAddr(int addr)
{
this.sP.DiscardInBuffer();
string text = "@" + addr.ToString() + ":Addr?\r\n";
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
text = text.Replace("\n", "");
text = text.Replace("\r", "");
if (text.Length > 0)
{
return Convert.ToInt32(text);
}
return 1;
}
public string WriteAddr(int addr, int value)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Addr ",
value.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string SetBTL(int addr, int value)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":BRG ",
value.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string MotorRun(int addr, string motorNum, long steps)
{
if (steps == 0L)
{
return "0";
}
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Motor:Run:M",
motorNum,
" ",
steps.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string MotorMoveTo(int addr, string motorNum, long steps)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Motor:MoveTo:M",
motorNum,
" ",
steps.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string SetMotorPostion(int addr, string motorNum, long steps)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Motor:Pos:M",
motorNum,
" ",
steps.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string ReadMotorPostion(int addr, string motorNum)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Motor:Pos:M",
motorNum,
"?\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
string oldValue = "P" + motorNum + ":";
text = text.Replace(oldValue, "");
text = text.Replace("\n", "");
return text.Replace("\r", "");
}
public string MotorHome(int addr, string motorNum)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Motor:Home:M",
motorNum,
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string SetMotorSpeed(int addr, string motorNum, int speed)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Motor:Speed:M",
motorNum,
" ",
speed.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string SetMotorInvert(int addr, string motorNum, int Invert)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Motor:Invert:M",
motorNum,
" ",
Invert.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string SetMotorSpeedStart(int addr, string motorNum, int value)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Motor:SStart:M",
motorNum,
" ",
value.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string SetMotorSpeedAdd(int addr, string motorNum, int value)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Motor:SAdd:M",
motorNum,
" ",
value.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string SetMotorSpeedHome(int addr, string motorNum, int value)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Motor:SHome:M",
motorNum,
" ",
value.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string MotorSave(int addr)
{
this.sP.DiscardInBuffer();
string text = "@" + addr.ToString() + ":Motor:Save\r\n";
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string ReadMotorSpeed(int addr, string motorNum)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Motor:Speed:M",
motorNum,
"?\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
text = text.Replace("\n", "");
return text.Replace("\r", "");
}
public string ReadMotoRunStatus(int addr)
{
this.sP.DiscardInBuffer();
string text = "@" + addr.ToString() + ":Motor:Run?\r\n";
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
text = text.Replace("R:", "");
if (text.Length >= 4)
{
text = text.Substring(0, 4);
}
return text;
}
public string ReadMotoRunStatus(int addr, string motorNum)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Motor:Run:M",
motorNum,
"?\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
string oldValue = "R" + motorNum.Trim() + ":";
text = text.Replace(oldValue, "");
return text.Replace("\r\n", "");
}
public string ReadMotoHomeStatus(int addr, string motorNum)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Motor:Home:M",
motorNum,
"?\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
text = text.Replace("\n", "");
return text.Replace("\r", "");
}
public string MotoStop(int addr, string motorNum)
{
string text = "@" + addr.ToString() + ":Motor:Stop:All\r\n";
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string SetMotorMotorIntMode(int addr, int value)
{
if (value > 1 || value < 0)
{
value = 0;
}
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Program:MINT ",
value.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string WriteOut(int addr, int channel, int onoff)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Output:Y",
channel.ToString(),
" ",
onoff.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string WriteOutDelayOff(int addr, int channel, int delaytime)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":OutDly:Y",
channel.ToString(),
" ",
delaytime.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string RealyOnOff(int addr, int value)
{
this.sP.DiscardInBuffer();
string text = "@" + addr.ToString() + ":Output:Y9 ";
text = text + value.ToString() + "\r\n";
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string ReadOutBit(int addr, int channel)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":OutPut:Y",
channel.ToString(),
"?\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
text = text.Replace("\n", "");
return text.Replace("\r", "");
}
public string ReadIn(int addr)
{
this.sP.DiscardInBuffer();
string text = "@" + addr.ToString() + ":Input:All?\r\n";
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
text = text.Replace("\n", "");
text = text.Replace("\r", "");
return text.Replace("X:", "");
}
public string ReadInBit(int addr, int ch)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Input:X",
ch.ToString(),
"?\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
string oldValue = "X" + ch.ToString() + ":";
text = text.Replace(oldValue, "");
return text.Replace("\r\n", "");
}
public int ReadInBit2(int addr, int port)
{
string text = this.ReadIn(addr);
if (text.Length < 8)
{
return -1;
}
if (text[port - 1] == '1')
{
return 1;
}
return 0;
}
public string ReadCounter(int addr, int channel, out int value)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Count:C",
channel.ToString(),
"?\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
text = text.Replace("\n", "");
text = text.Replace("\r", "");
if (text.Length >= 1)
{
value = Convert.ToInt32(text);
}
else
{
value = 0;
}
return "1";
}
public string XIntModeSet(int addr, int value)
{
if (value > 1 || value < 0)
{
value = 0;
}
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Program:Int ",
value.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string XIntModeRead(int addr, int value)
{
this.sP.DiscardInBuffer();
string text = "@" + addr.ToString() + ":Program:Int?\r\n";
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string MotorIntModeSet(int addr, int value)
{
if (value > 1 || value < 0)
{
value = 0;
}
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Program:MInt ",
value.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string ReadMotorIntMode(int addr)
{
this.sP.DiscardInBuffer();
string text = "@" + addr.ToString() + ":Program:MInt?\r\n";
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string SetX8EStopMode(int addr, int value)
{
if (value > 1 || value < 0)
{
value = 0;
}
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Motor:EStop ",
value.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string ReadX8EStopMode(int addr)
{
this.sP.DiscardInBuffer();
string text = "@" + addr.ToString() + ":Motor:EStop?\r\n";
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string SetFilter(int addr, int value)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Filter ",
value.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string ReadAdc(int addr)
{
this.sP.DiscardInBuffer();
string text = "@" + addr.ToString() + ":ADC?\r\n";
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
text = text.Replace("\n", "");
return text.Replace("\r", "");
}
public string EditWrite(int addr, int E_addr, int E_value)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Program:Edit:",
E_addr.ToString(),
" ",
E_value.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string EditRead(int addr, int E_addr)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Program:Edit?:",
E_addr.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string ReadStep(int addr)
{
this.sP.DiscardInBuffer();
string text = "@" + addr.ToString() + ":Program:Pstep?\r\n";
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
if (text.Length > 0)
{
return Convert.ToInt32(text).ToString();
}
return "1";
}
public int ReadPPStep(int addr)
{
this.sP.DiscardInBuffer();
string text = "@" + addr.ToString() + ":Program:PPstep?\r\n";
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
if (text.Length > 0)
{
return Convert.ToInt32(text);
}
return -2;
}
public string SetOrigStep(int addr, int step)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Program:ORIG ",
step.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
if (text.Length > 0)
{
return Convert.ToInt32(text).ToString();
}
return "1";
}
public string SetProgramMode(int addr, int mode)
{
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Program:mode ",
mode.ToString(),
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
if (text.Length > 0)
{
return text;
}
return "1";
}
public string PtogramStop(int addr)
{
this.sP.DiscardInBuffer();
string text = "@" + addr.ToString() + ":Program:Stop\r\n";
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
if (text.Length > 0)
{
return text;
}
return "1";
}
public string Pause(int addr, int value)
{
this.sP.DiscardInBuffer();
string text;
if (value == 0)
{
text = "@" + addr.ToString() + ":Program:Pause 0\r\n";
}
else
{
text = "@" + addr.ToString() + ":Program:Pause 1\r\n";
}
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
if (text.Length > 0)
{
return text;
}
return "1";
}
public string InputPassWord(int addr, string password)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":Compare:",
password.Length.ToString(),
":",
password,
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string SetPassWord(int addr, string password)
{
this.sP.DiscardInBuffer();
string text = string.Concat(new string[]
{
"@",
addr.ToString(),
":LOCPASS:",
password.Length.ToString(),
":",
password,
"\r\n"
});
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string ClearPassWord(int addr)
{
this.sP.DiscardInBuffer();
string text = "@" + addr.ToString() + ":Clear\r\n";
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string ReadCounterSpeed(int addr)
{
this.sP.DiscardInBuffer();
string text = "@" + addr.ToString() + ":Count1?\r\n";
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string ReadRotateDirection(int addr)
{
this.sP.DiscardInBuffer();
string text = "@" + addr.ToString() + ":FX?\r\n";
this.sP.Write(text.ToCharArray(), 0, text.Length);
text = this.readPort();
return text.Replace("\r\n", "");
}
public string readPort()
{
string text = "";
int num = 0;
string result;
try
{
do
{
text += this.readPortOnce();
if (text.Substring(text.Length - 1, 1) == "\n")
{
break;
}
Thread.Sleep(5);
num++;
}
while (num <= 20);
result = text;
}
catch (Exception)
{
result = text;
}
return result;
}
public string readPortOnce()
{
string str = "";
byte[] array = new byte[this.sP.ReadBufferSize + 1];
int count = this.sP.Read(array, 0, this.sP.ReadBufferSize);
string @string = Encoding.ASCII.GetString(array, 0, count);
return str + @string;
}
public static int Asc(string character)
{
if (character.Length == 1)
{
ASCIIEncoding aSCIIEncoding = new ASCIIEncoding();
return (int)aSCIIEncoding.GetBytes(character)[0];
}
throw new Exception("Character is not valid.");
}
}
}
来自 软件综合

想参与大家的讨论?现在就 登录 或者 注册

{{submitted?"":"投诉"}}
请选择违规类型:
{{reason.description}}
支持的图片格式:jpg, jpeg, png