我有DSC IT_100。我想发送数据命令来调用警报。但是我无法处理它。
SerialPort myPort = new SerialPort("COM6");
myPort.BaudRate = 9600;
myPort.Parity = Parity.None;
myPort.StopBits = StopBits.One;
myPort.DataBits = 8;
myPort.Handshake = Handshake.None;
myPort.Open();
我发送数据为HEX。这是我的程序
var sendData = GetBytes("6541D2CRLF");
myPort.WriteLine(sendData);
private static string GetBytes(string input)
{
string result = "";
char[] values = input.ToCharArray();
foreach (char letter in values)
{
int value = Convert.ToInt32(letter);
result += String.Format("{0:X}", value);
}
return result;
}
在编程表中,它说:
你能帮我把这些数据发送给COM ..
答案 0 :(得分:0)
我相信你需要遵循方法:
public static byte[] BuildPacket(string Command, string DataBytes)
{
List<byte> output = new List<byte>();
// Add command text
output.AddRange(Encoding.ASCII.GetBytes(Command));
// Add data bytes
output.AddRange(Encoding.ASCII.GetBytes(DataBytes));
// Add checksum
byte chkSum = 0;
foreach(byte b in output )
{
chkSum += b;
}
output.AddRange(Encoding.ASCII.GetBytes(chkSum.ToString("X")));
output.AddRange(Encoding.ASCII.GetBytes(Environment.NewLine));
return output.ToArray();
}
要获取需要发送到端口的字节,请使用以下方法:
var sendData = BuildPacket("654","3");
myPort.WriteLine(sendData);
查看编程表后似乎出现了错误:
请注意我没有COM端口或您的设备进行测试,因此您的结果可能会有所不同。
我阅读了设备手册here。
答案 1 :(得分:0)
我有这个工作。经过一夜的挑战。
关键是校验和计算。下面的代码连接到我的IT-100(在IP /串行适配器上运行),并将其发送到&#34; Poll&#34;命令和&#34;状态&#34;命令。
困难的部分(花了我几个小时的试验和错误)是bizzare校验和计算。手册中的算法非常奇怪:
例如,在上面的步骤4中:0x01 =&#39; 1&#39; = 0x31 =超越愚蠢
我的完整代码如下。
using System;
using System.Net.Sockets;
using System.Text;
namespace IT100TestDriver
{
class Program
{
static void Main(string[] args)
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
var address = System.Net.IPAddress.Parse("192.168.1.250");
System.Net.EndPoint ep = new System.Net.IPEndPoint(address, 1024);
s.Connect(ep);
byte[] buffer = new byte[1024];
NetworkStream ns = new NetworkStream(s);
AsyncCallback callback = null;
callback = ar =>
{
int bytesRead = ns.EndRead(ar);
string fromIT100 = Encoding.ASCII.GetString(buffer, 0, bytesRead);
Console.Write(fromIT100);
ns.BeginRead(buffer, 0, 1024, callback, null);
};
Console.WriteLine("Connected to: {0}", ep.ToString());
ns.BeginRead(buffer, 0, 1024, callback, null);
while (true)
{
var ki = Console.ReadKey(true);
byte[] command = null;
switch (ki.KeyChar)
{
case 'p':
{
Console.WriteLine("Sending Ping");
command = Encoding.ASCII.GetBytes("000"); // ping
}
break;
case 's':
{
Console.WriteLine("Sending Status Request");
command = Encoding.ASCII.GetBytes("001"); // status request
}
break;
}
if (command != null)
{
byte[] crlf = { 0x0D, 0x0A };
ns.Write(command, 0, command.Length);
byte[] checksum = calculateChecksum(command);
ns.Write(checksum, 0, checksum.Length);
ns.Write(crlf, 0, crlf.Length);
}
}
}
private static byte[] calculateChecksum(byte[] dataToSend)
{
int sum = 0;
foreach(byte b in dataToSend)
{
sum += b;
}
int truncatedto8Bits = sum & 0x000000FF;
byte upperNibble = (byte)(((byte)(truncatedto8Bits & 0x000000F0)) >> 4);
byte lowerNibble = (byte) (truncatedto8Bits & 0x0000000F);
// value is 0x09, need to treat it as '9' and convert to ASCII (0x39)
byte upperNibbleAsAscii = (byte)nibbleToAscii(upperNibble);
byte lowerNibbleAsAscii = (byte)nibbleToAscii(lowerNibble);
return new byte[] { upperNibbleAsAscii, lowerNibbleAsAscii };
}
private static char nibbleToAscii(byte b)
{
switch (b)
{
case 0x00: return '0';
case 0x01: return '1';
case 0x02: return '2';
case 0x03: return '3';
case 0x04: return '4';
case 0x05: return '5';
case 0x06: return '6';
case 0x07: return '7';
case 0x08: return '8';
case 0x09: return '9';
case 0x0A: return 'A';
case 0x0B: return 'B';
case 0x0C: return 'C';
case 0x0D: return 'D';
case 0x0E: return 'E';
case 0x0F: return 'F';
default:
throw new ArgumentOutOfRangeException("Unknown Nibble");
}
}
}
}