using System; using System.Diagnostics.Metrics; using System.IO.Ports; using System.Timers; class Program { static SerialPort? serialPort; static void Main() { string[] ports = SerialPort.GetPortNames(); serialPort = new SerialPort("COM3", 115200, Parity.None, 8, StopBits.One); serialPort.Handshake = Handshake.None; serialPort.Open(); byte[] dataToSend = new byte[44]; // 送信データ(ゼロ埋め) System.Timers.Timer sendTimer = new System.Timers.Timer(40); // 40ms周期 sendTimer.Elapsed += (sender, e) => SendData(dataToSend); sendTimer.AutoReset = true; sendTimer.Start(); Console.WriteLine("送信中。Enterキーで終了します..."); Console.ReadLine(); sendTimer.Stop(); serialPort.Close(); } static void SendData(byte[] data) { if (serialPort != null && serialPort.IsOpen) { serialPort.Write(data, 0, data.Length); Console.WriteLine($"送信: {DateTime.Now:HH:mm:ss.fff}"); } } }