https://www.youtube.com/watch?v=fZd2PfuhQ7Y
https://www.youtube.com/watch?v=_wovNBQpu2o
//form1.cs
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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
mySerialPort.BaudRate = Class1.baudrate;
switch (Class1.parity)
{
case "Even":
mySerialPort.Parity = Parity.Even;
break;
case "Odd":
mySerialPort.Parity = Parity.Odd;
break;
case "None":
mySerialPort.Parity = Parity.None;
break;
default:
break;
}
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = Class1.databits;
mySerialPort.Handshake = Handshake.None;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.Open();
richTextBox1.AppendText("open\n");
}
private SerialPort mySerialPort = new SerialPort(Class1.comport);
public delegate void callrichtext(String ss);
public callrichtext myDelegate;
public void richtextappend(String s)
{
richTextBox2.AppendText(s);
}
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
richTextBox1.Invoke(this.myDelegate, new Object[] { indata });
}
private void Form1_Load(object sender, EventArgs e)
{
this.myDelegate = new callrichtext(richtextappend);
}
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
mySerialPort.Write(Convert.ToString(e.KeyChar));
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("new connection?", "inquiry", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Application.Restart();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
mySerialPort.Close();
}
}
}
----------------------------------------------------------------------------------------------------------
//form2.cs
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;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
button1.Enabled = false;
foreach (string s in SerialPort.GetPortNames())
{
comboBox1.Items.Add(s);
}
}
private bool[] ok = { false, false, false,false};
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Class1.comport = comboBox1.SelectedItem.ToString();
ok[0] = true;
if (okenable(ok)) { button1.Enabled = true; }
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
Class1.baudrate = Convert.ToInt32(comboBox2.SelectedItem);
ok[1] = true;
if (okenable(ok)) { button1.Enabled = true; }
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
Class1.databits = Convert.ToInt32(comboBox3.SelectedItem);
ok[2] = true;
if (okenable(ok)) { button1.Enabled = true; }
}
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
Class1.parity = comboBox4.SelectedItem.ToString();
ok[3] = true;
if (okenable(ok)) { button1.Enabled = true; }
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form1 hyperterm = new Form1();
hyperterm.Show();
}
private bool okenable(bool[] ok)
{
bool x = true;
foreach (bool b in ok)
{
if (b == false) { x = false; }
}
return x;
}
}
}
----------------------------------------------------------------------------------------------------
//class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication1
{
class Class1
{
private static Form2 x = new Form2();
public static Form2 mainForm
{
set { x = value; }
get { return x; }
}
private static string y;
public static string comport
{
set { y = value; }
get { return y; }
}
private static int z;
public static int baudrate
{
set { z = value; }
get { return z; }
}
private static int u;
public static int databits
{
set { u = value; }
get { return u; }
}
private static string v;
public static string parity
{
set { v = value; }
get { return v; }
}
}
}
-------------------------------------------------------------------------------------------------------
//program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(Class1.mainForm);
}
}
}
http://social.msdn.microsoft.com/Forums/vstudio/en-US/0388ec40-47f1-44f8-a25d-d0d5e7ddf2d2/usb-communication-from-c?forum=csharpgeneral
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived(v=vs.110).aspx
No comments:
Post a Comment