0
DataReceivedとハンドライベントを使用してmkからデータを受信しようとしています - mkのLEDがオンになり、データはプログラムに返されるべきです(バイト値で1を期待していますが、文字列値を試しても動作しません)。送信側が動作していますが、受信.... .... 私は何かが不足しているようです。どんな助けでもそれを感謝します。 Thx in moreC#は、マイクロコントローラからデータを受け取る必要があります
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 WindowsFormsApplication11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) // As i understood, here we configure where i data will be shown,
// trying to get it on TextBox1
{
SerialPort sp = (SerialPort)sender;
richTextBox1.Text += sp.ReadExisting() + "\n";
}
private void button1_Click(object sender, EventArgs e) // There are a main actions, first i receive data then send data by a click.
{
serialPort1.Write("\u0001");
serialPort1.Close();
System.ComponentModel.IContainer components = new System.ComponentModel.Container(); //
serialPort1 = new System.IO.Ports.SerialPort(components);
serialPort1.PortName = "COM4";
serialPort1.BaudRate = 9600;
serialPort1.DtrEnable = true;
serialPort1.Open();
serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
}
}
}
書き込みの前にリスナーを配線する必要があります。ピンがオンになっているように見えますが、情報を受け取るために何も聞いていませんでした。あなたがポートの聞き取りを開始する頃には、何が起こっているのでしょうか。 –
シリアルポートエミュレータを使用して問題をデバッグすることで、何が起こっているのかを正確に把握し、通信が入っていることを確認することをお勧めします。 – FeliceM
Thxあなたのご意見は、私のデバイスが動作していると言いましたので、私はデータを送信することができ、またコンソール(C#も使用)でデータを受信していることを確認し、mkから送信したデータを受け取りました。したがって、上の私のコードの問題。正しいコードで助けを借りて、それを親切に評価してください。 –