简体   繁体   中英

Reading serial data from Arduino via C#

I am trying to make a program that allows me to control the Arduino to send out a HIGH signal to turn my motor and there will be a feedback through serial that tells me that the motor has been moved.

My problem is that I am unable to get any feedback from the Arduino. These are my code snippets.

SerialPort serialComms;
Select_Arduino.IsEnabled = false;
serialComms = new SerialPort(Port_Name.SelectedItem.ToString(), Convert.ToInt32(Baud_Rate.SelectedItem.ToString()));
serialComms.DtrEnable = true;
serialComms.DataReceived += serialComms_DataReceived;

void serialComms_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    SamsungTV.Text = SamsungTV.Text + "\n" + e.ToString();
}

The program totally does not enter serialComms_DataReceived . Is there any way for me to receive the feedback?

The feedback is as so "The motor has been moved 15 degrees counter-clockwise."

您需要调用serialComms.Open()

Usually when I use the event handler for recieving messages through the Serial port I do something like this...

void serialComms_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
     String testing = serialComs.ReadLine(); // Also, I use the Serial.println(arg) command from the arduino in order to simplify the syntax a little bit..
     SamsungTV.Text = testing + "\n";
}

I hope that this helps in some way!

Here is my solution:

using System.IO.Ports;

private void Read()
{
   SerialPort myport = new SerialPort();

   myport = new SerialPort();
   myport.BaudRate = 9600;
   myport.PortName = "COM3";

   myport.Open();
   string data = myport.ReadLine();

   myport.Close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM