T
TinaAnas
Guest
Hi,
I am tasked with reading emitted data from equipment data via a serial port. The equipment is going to send multiple data (Maybe more than one: in each send button click on the equipment) to the server.
I have written the following class for reading "single data" sent by the equipment.
I am asking how to keep reading the equipment emitted data till the finish command is send to the server (Stop reading).
public class MachineIntegrationManager : IMachineIntegrationService
{
public SerialPort Sport { set; get; }
public MachineIntegrationManager()
{
Sport = new SerialPort("COM6", 9600, Parity.None, 8, StopBits.One);
}
public ResultClass<string> ReadEquipmentOutput()
{
ResultClass<string> res = new ResultClass<string>();
try
{
Sport.Open();
Sport.DataReceived += SportOnDataReceived;
res.Result = "";
res.Code = Errors.Success;
res.Message = Errors.GetErrorMessage(res.Code);
}
catch (Exception ex)
{
res.Result = string.Empty;
res.Code = Errors.UnknownError;
res.Message = Errors.GetErrorMessage(res.Code);
}
return res;
}
private void SportOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
{
File.WriteAllText("test.txt", Sport.ReadExisting());
}
}
}
Continue reading...
I am tasked with reading emitted data from equipment data via a serial port. The equipment is going to send multiple data (Maybe more than one: in each send button click on the equipment) to the server.
I have written the following class for reading "single data" sent by the equipment.
I am asking how to keep reading the equipment emitted data till the finish command is send to the server (Stop reading).
public class MachineIntegrationManager : IMachineIntegrationService
{
public SerialPort Sport { set; get; }
public MachineIntegrationManager()
{
Sport = new SerialPort("COM6", 9600, Parity.None, 8, StopBits.One);
}
public ResultClass<string> ReadEquipmentOutput()
{
ResultClass<string> res = new ResultClass<string>();
try
{
Sport.Open();
Sport.DataReceived += SportOnDataReceived;
res.Result = "";
res.Code = Errors.Success;
res.Message = Errors.GetErrorMessage(res.Code);
}
catch (Exception ex)
{
res.Result = string.Empty;
res.Code = Errors.UnknownError;
res.Message = Errors.GetErrorMessage(res.Code);
}
return res;
}
private void SportOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
{
File.WriteAllText("test.txt", Sport.ReadExisting());
}
}
}
Continue reading...