如何同时发送和接收数据 [英] How do I Send and Recieve data Simultenously

查看:146
本文介绍了如何同时发送和接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个C#Form App。它将通过串口与Arduino通信并控制连接到Arduino的电机,并从Arduino接收一个字节并显示一条消息。我的问题是我不能同时写作和阅读。我必须阻止一个人去执行另一个。这是我的代码。

I want to write a C# Form App. that will communicate with Arduino via Serial port and control the motors connected to the Arduino., and also receive a byte from Arduino and display a Message. My problem is i can't do both the writing and reading simultenously. i have to stop one to perform the other. Here is my code.

 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;
using System.IO.Ports;

namespace Securino
{
    public partial class Form1 : Form
    {
        SerialPort port = new SerialPort();
        
        public Form1()
        {

            InitializeComponent();
            port.PortName = "COM2";
            port.BaudRate = 9600;
            port.Open();
           // listen();
                  
        }

        private void button1_Click(object sender, EventArgs e)
        {
            port.Write("l");
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            port.Write("r");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            port.Write("f");

        }

        private void button4_Click(object sender, EventArgs e)
        {
            port.Write("o");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            port.Write("c");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            port.Write("x");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button7_Click(object sender, EventArgs e)
        {
            
            Console.WriteLine("listening");
            int ch = port.ReadChar();
            if (ch == 65)
            {
                MessageBox.Show("INTRUDER DETECTED");
            }
        }
        }
        

        
    }

推荐答案

每个SerialPort都是这样工作的。

但你可以做到以下几点:



你可以(例如)构建一个每100ms调用一次的Timer-Method。

如果有要写的数据,这个方法会为你做port.write,并询问是否有数据要在端口读取,如果是的话'它读取数据。

使用此解决方案,您的击键必须写入一种缓冲区,而不是直接写入端口。



我建议使用Timer,因为这是最简单的方法。

您也可以使用BackgroundWorker或任务执行此操作。
Each SerialPort is working like this.
But you can do the following :

You could (for example) build a Timer-Method which is called each 100ms.
This method do the port.write for you if there is data to write and it asks if there is data to read in the port and if 'yes' it reads the data.
With this solution your keystrokes have to be written to a kind of buffer and not longer to the port directly.

I suggested a Timer because it is the easiest way to do this.
You could also do this with a BackgroundWorker or a Task.


这篇关于如何同时发送和接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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