-
-
Save dj1711572002/10fee174c76cfaaa784932b430d3980e to your computer and use it in GitHub Desktop.
C# Serial Recive Send Test
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.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
using System.IO.Ports; | |
using System.Diagnostics; | |
using static System.Net.Mime.MediaTypeNames; | |
using static System.Windows.Forms.VisualStyles.VisualStyleElement; | |
namespace SerialTest_rev01 | |
{ | |
public partial class Form1 : Form | |
{ | |
int i; | |
public struct rdata | |
{ | |
public string[] lines; | |
public string[] sline; | |
public string itowB; | |
} | |
rdata r; | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
serialPort1.BaudRate = 115200; | |
serialPort1.Parity = Parity.None; | |
serialPort1.DataBits = 8; | |
serialPort1.StopBits = StopBits.One; | |
serialPort1.Handshake = Handshake.None; | |
serialPort1.PortName = portComboBox.Text; | |
serialPort1.Open(); | |
} | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
string[] ports = SerialPort.GetPortNames(); | |
foreach (string port in ports) | |
{ | |
portComboBox.Items.Add(port); | |
} | |
if (portComboBox.Items.Count > 0) | |
portComboBox.SelectedIndex = 0; | |
} | |
delegate void DataDelegate(string sdata); | |
//=================printdata 内でデータ処理================================ | |
private void printdata(string text) | |
{ | |
textBox1.AppendText(text); | |
richTextBox1.AppendText(text); | |
richTextBox1.ScrollToCaret(); | |
/* | |
r.lines = text.Split('['); | |
r.sline = r.lines[1].Split(','); | |
for(i=0;i<r.sline.Length-1;i++) | |
{ | |
Debug.Print("sline[" + i.ToString() + "]=" + r.sline[i].ToString()); | |
} | |
*/ | |
} | |
// -------------------Serial受信部-------------------------------------------------- | |
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) | |
{ | |
string str = serialPort1.ReadLine(); | |
DataDelegate a = new DataDelegate(printdata); | |
BeginInvoke(a, str); | |
} | |
//----------------------------------------------------------------------------------- | |
private void button2_Click(object sender, EventArgs e) | |
{ | |
} | |
private void button3_Click(object sender, EventArgs e) | |
{ | |
serialPort1.Close(); | |
} | |
private void button4_Click(object sender, EventArgs e) | |
{ | |
if (serialPort1.IsOpen) | |
{ | |
serialPort1.Write("p"); | |
} | |
} | |
private void button5_Click(object sender, EventArgs e) | |
{ | |
if (serialPort1.IsOpen) | |
{ | |
serialPort1.Write("d"); | |
} | |
} | |
private void button6_Click(object sender, EventArgs e) | |
{ | |
if (serialPort1.IsOpen) | |
{ | |
serialPort1.Write("a"); | |
} | |
} | |
private void button2_Click_1(object sender, EventArgs e) | |
{ | |
if (serialPort1.IsOpen) | |
{ | |
serialPort1.Write("s"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment