Files
laatzen/Common/OmniFeaturesTest/Program.cs
T
2023-11-29 09:20:28 +01:00

88 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace OmniFeaturesTest
{
class Program
{
static readonly Connection connection = new Connection("COM26");
static void Main(string[] args)
{
connection.Open();
var factoryIDBytes = SendAndWaitResponse(0x01);
Console.WriteLine(Encoding.UTF8.GetString(factoryIDBytes));
var x = new byte[14];
x[0] = 0x02;
Encoding
.ASCII
.GetBytes("816232411982")
.CopyTo(x, 1);
var response = SendAndWaitResponse(x);
factoryIDBytes = SendAndWaitResponse(0x01);
Console.WriteLine(Encoding.UTF8.GetString(factoryIDBytes));
if (Console.ReadKey().Key == ConsoleKey.Enter)
{
connection.Close();
}
}
private static byte[] SendAndWaitResponse(params byte[] request)
{
var response = new byte[1];
var received = false;
if (request?.Length <= 0)
{
return response;
}
void receiveResponse(byte[] data)
{
response = data
.FromIrdaMessage()
.FromUI1236Message();
if (response?.Length > 0 && response[0] > 0)
{
Console.WriteLine("RX: " + BitConverter.ToString(response));
connection.ResponseReceived -= receiveResponse;
received = true;
}
}
connection.ResponseReceived += receiveResponse;
Console.WriteLine("TX: " + BitConverter.ToString(request));
request = request
.ToUI1236Message()
.ToIrdaMessage();
connection.Send(1, 1, 1, 1);
Thread.Sleep(1);
connection.Send(request);
while (!received)
{
Thread.Sleep(1000);
}
connection.ResponseReceived -= receiveResponse;
return response;
}
}
}