大佬们轻点喷 全都是自学的 没有啥规则可言 代码也一团乱
这是一个测试程序 但是一直收不到从单片机发来的数据 用的arduino nano 168p 波特率9600 端口COM5 用了很多从网上早来的方法 都不行嘞
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;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;
namespace test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
try
{
button1.Text = "打开串口";
SerialPort.RtsEnable = true;
SerialPort.DtrEnable = true;
SerialPort com = new SerialPort();
SerialPort.Close();
com.BaudRate = int.Parse("9600");
com.PortName = "COM5";
com.DataBits = int.Parse("8");
com.Parity = System.IO.Ports.Parity.None;
com.StopBits = System.IO.Ports.StopBits.One;
com.WriteTimeout = 3000;
com.ReadTimeout = 3000;
com.ReceivedBytesThreshold = 1;
com.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived);
com.Open();
for(int j = 0; j < 10; j++) { com.WriteLine("aaa");Thread.Sleep(100); }
commonData.i = 1;
button1.Text = "关闭串口";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
//SerialPort.DataReceived += new SerialDataReceivedEventHandler(SerialDataReceivedEvent);
}
private void Sanu()
{
while(true)
{
int len = SerialPort.BytesToRead;
if (len >= 8)
{
//Byte[] readBuffer = new Byte[len];
//SerialPort.Read(readBuffer, 0, len);
string jjy = SerialPort.ReadExisting();
}
MessageBox.Show(SerialPort.ReadExisting());
}
}
private void Com_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
throw new NotImplementedException();
SerialPort serial = (SerialPort)sender;
string s = serial.ReadExisting();
this.Invoke(new Action(() => { textBox1.AppendText("\r\n"+s); }));
MessageBox.Show(s);
// textBox1.Text = SerialPort.ReadExisting() + "\r\n";
int ii = 0;
ii = SerialPort.BytesToRead;
textBox1.AppendText(ii.ToString() + "\r\n");
}
public class commonData//全局变量
{
public static int i = 0;
}
private void button2_Click(object sender, EventArgs e)
{
string baud = "9600";
string portname = "COM5";
string databits = "8";
try
{
if(SerialPort.IsOpen)
{
MessageBox.Show("初始化OK");
}
else
{
SerialPort.BaudRate = int.Parse(baud);
SerialPort.PortName = portname;
SerialPort.DataBits = int.Parse(databits);
SerialPort.Parity = System.IO.Ports.Parity.None;
SerialPort.StopBits = System.IO.Ports.StopBits.One;
SerialPort.Open ();
commonData.i = 1;
button1.Text = "关闭串口";
MessageBox.Show("初始化OK");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if(commonData.i == 1)
{
if (button1.Text == "打开串口")
{
SerialPort.Open();
button1.Text = "关闭串口";
}
if (button1.Text == "关闭串口")
{
SerialPort.Close();
button1.Text = "打开串口";
}
}
else
{
MessageBox.Show("未初始化");
}
}
catch(Exception es)
{
MessageBox.Show (es.Message);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
}
}
}