C#,需要从微控制器接收数据 [英] C#, need to receive data from microcontroller

查看:55
本文介绍了C#,需要从微控制器接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Good Fellows。

尝试使用DataReceived和处理程序事件从mk接收数据,我所做的是 -

按下程序上的按钮(代码在下面)然后mk上的LED将被撕开,然后数据应该被发送回程序(期望1,在字节值,但也尝试字符串值,不起作用)。发送方正在工作,但收到....不是

似乎我错过了什么。任何帮助都可以帮助它。 Thx in Further



我用mdsn控制台示例检查,我收到的数据很好。

所以我认为在上面的代码中存在问题。

有一个示例:

https://msdn.microsoft.com/en- us / library / system.io.ports.serialport.datareceived(v = vs.110).aspx



我尝试过:



Hello Good Fellows.
Trying to receive data, from mk, using DataReceived and handler event, what i do is -
push a button on a program(code is below) then LED on mk will torn on, then the data should be sent back to program(expecting 1, on byte value, but also tried string value, doesnt work). Sennding side is working, but recieving....not
seems like im missing something. Any help apreciate it. Thx in Further

I checked with mdsn console sample, i receiving data well.
So the problem i think in code above.
There are a sample:
https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived(v=vs.110).aspx

What I have tried:

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);

          
        }
    }
}

推荐答案

第一件事就是do是调试串行链接。

您需要查找微控制器是否无法发送应答或Windows应用程序无法接收它,或者两者都失败。

使用终端模拟器而不是你的应用程序并手动发送命令,如果微控制器工作,你将在终端上得到答案。然后在另一边做同样的事情,看它是否失败。

这将告诉你电缆是否有效并有助于缩小研究范围。



我不知道你在哪里找到了串口com的例子,但这很奇怪。

事情的顺序是:

- 设置串口参数

- 打开通讯

- 发送命令

- 等待回答

- close comm
First thing to do is debug serial link.
You need to find if microcontroler fail to send answer or the windows app fail to receive it, or if both fail.
Use a terminal emulator instead of your app and send the command manually, if microcontroler works, you will the answer on terminal. Then do the same on the other side and see if it fail or not.
This will tell you if cable works and help to narrow the research.

I don't know where you found examples of serial com, but it is weird.
The order of things is:
- set serial parameters
- open comm
- send command
- wait for answer
- close comm

这篇关于C#,需要从微控制器接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆