我已经制作了一个Windows窗体应用程序,我想以这种形式向Pro Micro读取和写入串行数据。
将数据发送到µC可以正常工作,但我无法读取它。
甚至,µC不会发送数据。
现在,我删除了所有不必要的代码,µC应该只是回显我发送的内容。
当我在Arduino串行监视器中使用此命令时,它都可以正常工作,然后写入数据并返回它。
但是当在Windows窗体中尝试时,它却没有。
然后我决定测量µC的tx引脚,尽管接收到的数据是正确的,但它根本不发送任何数据。
脉冲长度有所不同,但我认为这还不足以引起这种情况,该形式的持续时间为0.1039ms(使频率为9.624 Hz),而Arduino监视器的持续时间为0.1041ms(使频率为9.606) Hz)。
这是我的代码:
Form1:
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;
namespace UART
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.TopMost = true; // Window always on top
this.AutoSize = true; // The size of the window will always be as small as possible while still showing all content
serial.DataReceived += new SerialDataReceivedEventHandler(mySerialPort_DataReceived);
lblInvalidPort.Hide();
tbWriteInput.Enabled = false;
btnSend.Enabled = false;
// Show all ports in the ports combobox and sellect the curently used option
combPort.Items.Clear();
combPort.Items.AddRange(SerialPort.GetPortNames());
combPort.SelectedItem = serial.PortName;
}
private void serial_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
rtbRead.Text = e.ToString() + "\n" + rtbRead.Text;
MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
private void combPort_Click(object sender, EventArgs e)
{
// Show all ports in the ports combobox and sellect the curently used option
combPort.Items.Clear();
combPort.Items.AddRange(SerialPort.GetPortNames());
}
private void btnConnect_Click(object sender, EventArgs e)
{
if (btnConnect.Text.ToLower().Equals("connect"))
{
try
{
serial.PortName = combPort.Text;
serial.BaudRate = 9600;
serial.Open();
lblInvalidPort.Hide();
tbWriteInput.Enabled = true;
btnSend.Enabled = true;
btnConnect.Text = "Disconnect";
}
catch (Exception err)
{
lblInvalidPort.Show();
MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
tbWriteInput.Enabled = false;
btnSend.Enabled = false;
}
}
else if (btnConnect.Text.ToLower().Equals("disconnect"))
{
serial.Close();
tbWriteInput.Enabled = false;
btnSend.Enabled = false;
btnConnect.Text = "Connect";
}
}
private void serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
MessageBox.Show("Got some data", "Serial", MessageBoxButtons.OK, MessageBoxIcon.Warning);
rtbRead.Text = serial.ReadExisting() + "\n" + rtbRead.Text;
}
private static void mySerialPort_DataReceived(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
MessageBox.Show("Got some data", "Serial", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
private void tbWriteInput_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
{
rtbWrite.Text = tbWriteInput.Text + "\n" + rtbWrite.Text;
serial.Write(tbWriteInput.Text);
}
}
private void btnSend_Click(object sender, EventArgs e)
{
rtbWrite.Text = tbWriteInput.Text + "\n" + rtbWrite.Text;
serial.Write(tbWriteInput.Text);
}
}
}
Arduino:
void setup() {
Serial.begin(9600);
}
void loop() {
while(Serial.available() == 0);
while(Serial.available() > 0) Serial.write(Serial.read());
}
答案 0 :(得分:1)
对不起,我在使用第三版时发现了问题,您需要添加serial.DtrEnable = true;
才能将其插入单词。