-
-
Save dj1711572002/40a56288bb06ca5254574db83ad25478 to your computer and use it in GitHub Desktop.
chatGPT made C# Serrial Receiving Program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.IO.Ports; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
using static System.Windows.Forms.VisualStyles.VisualStyleElement; | |
/* | |
namespace SerialPortExample | |
{ | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
} | |
} | |
using System; | |
using System.Windows.Forms; | |
using System.IO.Ports; | |
*/ | |
namespace SerialPortExample | |
{ | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
InitializeSerialPort(); | |
} | |
private void InitializeSerialPort() | |
{ | |
serialPort1.PortName = "COM6"; // COM6ポートを使用する | |
serialPort1.BaudRate = 115200; // ボーレートを115200に設定する | |
serialPort1.Parity = Parity.None; | |
serialPort1.DataBits = 8; | |
serialPort1.StopBits = StopBits.One; | |
serialPort1.Handshake = Handshake.None; | |
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived); | |
serialPort1.Open(); | |
} | |
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) | |
{ | |
// データが受信されたときに呼び出されるメソッドです | |
// 受信したデータを処理するコードをここに追加します | |
// 受信したデータをテキストボックスに表示する例: | |
string receivedData = serialPort1.ReadExisting(); | |
Invoke(new Action(() => richTextBox1.AppendText(receivedData))); | |
} | |
private void Form1_FormClosing(object sender, FormClosingEventArgs e) | |
{ | |
if (serialPort1 != null && serialPort1.IsOpen) | |
{ | |
serialPort1.Close(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment