The call time on the phone, for example, is 20 seconds, but the data retrieved from the switchboard is 30 seconds, which does not match the call time between the switchboard and the phone. (How to only calculate time when the called person picks up the phone and stop calculating time when the call ends so that the time on the phone and switchboard can match).
When the called person does not pick up the phone, the waiting time for the called person to pick up the phone until they do not pick up the phone, for example, is 40 seconds. After hanging up, the switchboard data still returns the call time as 40 seconds. (How to not display call time when the called person does not pick up).
Language and device
WinForms, Net Framework 4.7.2
Panasonic kx-tes824 switchboard
Thanks
using System;
using System.IO.Ports;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace TGTH
{
public partial class frmMain : Form
{
SerialPort serialPort;
public frmMain()
{
InitializeComponent();
serialPort = new SerialPort(
portName: "COM1",
baudRate: 9600,
parity: Parity.None,
dataBits: 8,
stopBits: StopBits.One
)
{
Handshake = Handshake.None,
DtrEnable = true,
RtsEnable = true,
};
serialPort.Open();
serialPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string data = serialPort.ReadLine();
string pattern = @"b(d{1,2}/s?d{1,2}/d{2})b";
Match match = Regex.Match(data, pattern);
DateTime parsedDate;
string date = "", time = "", jack = "", phone = "", voice = "", line = "", code_call = "", destination = "";
double totalCost = 0;
string dateString = match.Groups[1].Value.Trim();
if (DateTime.TryParseExact(dateString, new string[] { "M/ d/yy", "MM/ d/yy", "M/dd/yy", "MM/dd/yy" }, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out parsedDate))
{
string[] parts = data.Substring(match.Index + match.Length).Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
date = parsedDate.ToString("dd/MM/yyyy");
time = parts[0];
DateTime parsedTime;
DateTime.TryParseExact(time, "h:mmtt", null, System.Globalization.DateTimeStyles.None, out parsedTime);
time = parsedTime.ToString("HH:mm");
jack = parts[1];
line = parts[2];
line = "C" + line;
phone = parts[3];
voice = parts[4];
voice = voice.Replace("'", ":").Replace(""", "");
}
else
{
return;
}
AddDataToDataGridView(date, time, jack, phone, voice, line, code_call, destination, totalCost.ToString());
}
private void AddDataToDataGridView(string date, string time, string jack, string phone, string voice, string line, string code_call, string destination, string totalCost)
{
if (dgvDataCom.InvokeRequired)
{
dgvDataCom.Invoke(new Action(() => AddDataToDataGridView(date, time, jack, phone, voice, line, code_call, destination, totalCost)));
}
else
{
dgvDataCom.Rows.Add(date, time, jack, phone, voice, line, code_call, destination, totalCost);
}
}
}
}
3