串口问题 [英] Serial Port Question

查看:61
本文介绍了串口问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

这可能是一个简单的问题.

我在下面有此代码

Hi to all.

This may be a simple question.

I have this code below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

class PortDataReceived
{
    public static string indata;
    public static void Main()
    {
        SerialPort mySerialPort = new SerialPort("COM4");

        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;

        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceviedHandler);

        mySerialPort.Open();

        //Console.WriteLine("Press any key to continue...");
        //Console.WriteLine();
        Console.ReadKey();
        
        mySerialPort.Close();
    }


    private static ulong getWeight(string serialData)
    {
        ulong weight = 0;
        char[] delimiters = new char[] { '\r', '\n' };
        string[] weights = serialData.Split(delimiters,StringSplitOptions.RemoveEmptyEntries);
        foreach (string singleWeight in weights)
        {
            weight = weight + Convert.ToUInt64(singleWeight);
        }
        weight = weight / Convert.ToUInt64(weights.Length);
        return weight;
    }

    private static void DataReceviedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        indata = sp.ReadExisting();
        Console.WriteLine("Data Received:");
        Console.WriteLine(getWeight(indata));
   
        //Console.ReadKey();
    }
}




我只想要将权重值传递到主菜单并从那里使用它.
忽略datarecievedhandler函数中的写行.
该程序似乎没有使用主程序.因此,我要做的就是将权重返回到主菜单,并继续执行和运行主菜单中的某些内容.程序按原样继续循环加载数据.

你能帮我吗?
谢谢&关于




All i want is that i want to pass the weight value to the main menu and use it from there.
Ignore the writelines in the datarecievedhandler function.
The program seems to be not using the main. So all i want is to return the weight to the main menu and continue executing and running some stuff in the main menu. The program as is continues to load data as it is in a loop.

Can you please help me out.
Thanks & Regards

推荐答案

它不是那么简单:它不使用main"是因为函数很忙.它正在等待来自键盘的输入,并且在得到输入之前将不执行任何其他操作.然后,只需关闭串行端口并退出即可.

如果要等待串行端口执行某项操作,则需要在主函数中处于循环状态,直到到达一些数据为止:
It''s not that simple: it "not using the main" because the function is busy. It is waiting for input from teh keyboard and will do nothing else until it gets some. Then all it will do is close the serial port and exit.

If you want to wait for the serial port to do something, then you need to sit in a loop in your main function until some data has arrived:
private bool hasData = false;
private ulong value;
...
while (!hasData) 
   {
   }
Console.WriteLine(value);
...
private static void DataReceviedHandler(object sender, SerialDataReceivedEventArgs e)
   {
   SerialPort sp = (SerialPort)sender;
   indata = sp.ReadExisting();
   value = getWeight(indata);
   hasData = true;
   }


这篇关于串口问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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