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.IO;
using System.Threading;
namespace wangxl{
public partial class Smart_meter : Form
{
public int begin_year;
public int begin_month;
public int begin_day;
public int finish_year;
public int finish_month;
public int finish_day;
string from_bs_2;
string from_EM;
double[,] power = new double[4, 23];
int chushihua = 0;
int chuqi_changshu = 0;
int n_1 = 0;
int n_2 = 0;
double[,] power_display_f = new double[4, 23];
string path = "C:\\Users\\Public\\data\\data_logging.txt";
string path_m = "C:\\Users\\Public\\data\\energy_monitoring.txt";
// string path = "C:\\energy_moniotring\\data_logging.txt";
public Smart_meter()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
String[] input = SerialPort.GetPortNames();
comboBox1.Items.AddRange(input);
comboBox16.Items.AddRange(input);
}
void button1(object sender, EventArgs e)
{
try
{
serialPort1.PortName = comboBox1.Text;
serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
serialPort1.DataBits = Convert.ToInt32(comboBox3.Text);
serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBox4.Text);
serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), comboBox5.Text);
serialPort1.Open();
progressBar1.Value = 100;
}
catch (Exception err)
{
MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
progressBar1.Value = 0;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
string output;
output = textBox2.Text;
serialPort1.Write(output);
textBox2.Text = "";
from_bs_2 = "";
serialPort1.DiscardOutBuffer();
// serialPort1.DiscardOutBuffer();
}
}
void button7_Click(object sender, EventArgs e)
{
try
{
serialPort2.PortName = comboBox16.Text;
serialPort2.BaudRate = Convert.ToInt32(comboBox15.Text);
serialPort2.DataBits = Convert.ToInt32(comboBox13.Text);
serialPort2.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBox14.Text);
serialPort2.Parity = (Parity)Enum.Parse(typeof(Parity), comboBox12.Text);
serialPort2.Open();
progressBar2.Value = 100;
}
catch (Exception err)
{
MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button8_Click(object sender, EventArgs e)
{
if (serialPort2.IsOpen)
{
serialPort2.Close();
progressBar2.Value = 0;
}
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Thread.Sleep(1000); // important
from_bs_2 += serialPort1.ReadExisting();
Thread.Sleep(1000);
this.Invoke(new EventHandler(showdata));
Thread.Sleep(1000);
}
private void serialPort2_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Thread.Sleep(1000); // important
from_EM += serialPort2.ReadExisting();
Thread.Sleep(1000);
this.Invoke(new EventHandler(showdata_2));
Thread.Sleep(1000);
// textBox1.Text += "laishujule";
}
我想设计一个可以同时连接2或3个Arduino的程序。因此,我创建了一个serialPort 1,它工作正常,可以从串行1中接收数据,但是当我使用相同的方式创建serialPort 2时,我发现serialPort不起作用,可以连接,但是可以无法从Arduino获取任何数据。我发现serialPort2_DataReceived
函数不起作用,我已经从工具中将serialPort 2添加到了程序中。你能帮我吗?
答案 0 :(得分:0)
在您的代码中,您没有为每个 SerialPort 指定 SerialDataReceivedEventHandler。在打开序列号之前尝试在代码中添加这些行:
yourenv/conda-meta/history