Visual Studio 2010 Code C#:Visual Studio 2010 C# DataReceived ne semble pas déclencher
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace YamanPonics
{
public partial class Form1 : Form
{
string RxString;
//Default SerialPortStatus equals TRUE when first starting up
Boolean serialPortDisconnected = true;
public Form1()
{
InitializeComponent();
//Add available Serial COM ports to combobox
foreach (string ports in System.IO.Ports.SerialPort.GetPortNames())
{
//MessageBox.Show("Serial port avialible" + " " + ports);
comPortCmbBox.Items.Add(ports);
}
}
private void DisplayText(object sender, EventArgs e)
{
serialMsgViewerRchTxt.AppendText(RxString);
MessageBox.Show("Displayed Serial Text!");
}
private void serialPort1_DataReceived (object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadExisting();
this.Invoke(new EventHandler(DisplayText));
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//If SerialPort1 IsOpen
if (serialPort1.IsOpen)
{
//Close SerialPort1 communication
serialPort1.Close();
}
}
private void connectDisconnectBtn_Click(object sender, EventArgs e)
{
//Set arduinoComPort value to COM Port value
string arduinoComPort = comPortCmbBox.Text;
//if SerialPortStatus boolean equals FALSE then
if (serialPortDisconnected && (arduinoComPort != ""))
{
//Set serialPort1 BaudRate value to default value of 38400(required for atlas-scientific sensors)
serialPort1.BaudRate = 38400;
//Set serialPort1 Read and Write timeout values
serialPort1.ReadTimeout = 250;
serialPort1.WriteTimeout = 250;
//Set serialPort1 DataBits value
serialPort1.DataBits = 8;
//Open serialPort1 communication
serialPort1.Open();
//Change connectDisconnectBtn text to Disconnect
connectDisconnectBtn.Text = "Disconnect";
//Set serialPortDisconnected to FALSE
serialPortDisconnected = false;
}
else //if SerialPortStatus bollean equals TRUE
{
//Close SerialPort1 communication
serialPort1.Close();
//Set connectDisconnectBtn text to Connect
connectDisconnectBtn.Text = "Connect";
//Set serialPortDisconnected to TRUE
serialPortDisconnected = true;
}
}
private void SendBtn_Click(object sender, EventArgs e)
{
//if serialPort1 IsOpen then
if (serialPort1.IsOpen)
{
serialPort1.Write("{ph}");
}
}
}
}
ce code est une série simple de connexion/déconnexion et envoyer/recevoir application. Connecter, déconnecter et envoyer fonctionne correctement. Quand mon arduino reçoit une commande, il renvoie une réponse. Mon application C# 2010 ne reçoit pas la réponse dans la zone richtext et ne comprend pas pourquoi. Je peux recevoir une réponse lorsque j'utilise un autre programme de terminal série, donc je suis sûr que les données sont envoyées. Qu'est-ce que je ne fais pas pour recevoir une réponse avec succès?
Merci! Cela a fait l'affaire. Je suis tout nouveau à C#. – dottedquad