I'm creating a program to work with some equipment using serial port. I have documentation where there're details how to send command by port. It works very well but I should get some information about status of the equipment. I tried everything but I'm not confident in these type of developing. May be I missed something.
From the documentation:
There are totally 21 commands, 20 commands are of opening, 1 command is of reading status
Open command:
Address Command fixed value( 0x55) lock number
Address 0XF2 0X55 0X01
Address 0XF2 0X55 0X02
Address 0XF2 0X55 0X03
......
Above commands execution
Success returns : Address+0X59+0X59
Failure returns: Address+0X5E+0X5E
/// <summary>
/// Send open lokcer door command
/// </summary>
/// <param name="boardAddress">0 means open left slave cabinet,1 means open right slave cabinet</param>
/// <param name="command"></param>
/// <returns></returns>
public byte[] OpenBox(byte boardAddress, byte command)
{
var openCommand = new byte[5];
openCommand[0] = (byte)(255 - boardAddress);
openCommand[0] = 0xF2;
openCommand[1] = 0x55;
openCommand[2] = command;
openCommand[3] = 0X00;
openCommand[4] = 0X00;
try
{
if (!serialPort.IsOpen)
{
//serialPort.WriteBufferSize = 5;
serialPort.Open();
}
serialPort.DiscardInBuffer();
serialPort.Write(openCommand, 0, 3);
return openCommand;
}
catch (ArgumentException ex)
{
throw new InvalidOperationException("Locker cell can not be opened.", ex);
}
}
It works very well but last 2 bytes return nothing. They are always 0X00. Also I'm using SerialPortDataReceived but I always get 0 there.
public void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
{
DBException.WriteLog("");
DBException.WriteLog("Serial port data received");
var sp = (SerialPort)sender;
DBException.WriteLog("Read existing = " + sp.ReadExisting());
try
{
DBException.WriteLog("Length = " + sp.BytesToRead);
if (serialPort.BytesToRead == 4)
{
var readBuffer = new byte[4];
sp.Read(readBuffer, 0, serialPort.BytesToRead);
DBException.WriteLog("Read serial port :" + readBuffer.ByteArrayToString());
// RaiseLockerStatusEvent(readBuffer.ByteArrayToString());
}
}
catch (Exception ex)
{
DBException.WriteLog(ex);
}
}
QUESTION 1 How to get Success or Failure returns? I think they should be in the last 2 bytes.
QUESTION 2 How to get all states? I get nothing at the present.
Documentation:
Read lock status command: Address + Data1 + Data2 + Data3 Address: 0XF10X55 Data1 Data2 Data3 0000 lock20 … lock17 lock16 … lock9 lock8 … lock1 The first 4 digits is fixed number of 0000 Sample: 0000 0000 0000 0001 0000 0001 means lock1 and lock9 is open, others are closed Note: for some type of electronic lock, “1” means close, “0” means open.
My snippet
/// <summary>
/// Send check machine(cabinet) status command
/// </summary>
/// <param name="address">0 means check left cabinet, 1 means check right cabinet </param>
/// <returns></returns>
public byte[] SendCheckLockControlStatueCommand(byte address)
{
lock (this)
{
var checkCommand = new byte[6];
checkCommand[0] = Convert.ToByte(255 - address);
checkCommand[1] = 0xF1;
checkCommand[2] = 0x55;
checkCommand[3] = 0x00;
checkCommand[4] = 0x00;
checkCommand[5] = 0x00;
try
{
if (!serialPort.IsOpen)
{
// serialPort.WriteBufferSize = 5;
serialPort.Open();
}
//Send inquiry command
serialPort.DiscardInBuffer();
// serialPort.DiscardOutBuffer();
serialPort.Write(checkCommand, 0, 6);
return checkCommand;
}
catch (ArgumentException ex)
{
throw new InvalidOperationException("Locker cell can not be opened.", ex);
}
}
}