You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

松下KX-TES824交换机与话机通话时长匹配及未接通计时优化

松下KX-TES824交换机串口数据读取问题

当前基于WinForms(.NET Framework 4.7.2)开发的程序,通过RS232端口读取松下KX-TES824交换机数据时存在以下两个问题:

  • 话机显示通话时长为20秒,但交换机返回数据为30秒,需实现仅计算被叫接通至挂断的时长,使两者数据匹配;
  • 被叫未接通时,交换机返回等待时长(如40秒),需实现此类场景不显示通话时长。

当前串口数据读取代码

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);
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者286_Trần Hoài Trọng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 03:17:48