无法读取COM3端口。始终输出为0 [英] Not able to read COM3 port. Always output is 0

查看:377
本文介绍了无法读取COM3端口。始终输出为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

This code is able to run, however it only return 0 even though the device has some value. The device is a roller machine where it should give the rolling meter value to the port number 3 however, my code always return the output as 0.

Please help me as soon as possible as this is a critical requirement.





我尝试过:





What I have tried:

using System;  
using System.Text;  
using System.IO.Ports;  
using System.Collections.Generic;  
namespace ConsoleApplication3  
{  
    class Program  
    {  
        static SerialPort sp;  
        string InputData = string.Empty;  
        static void Main(string[] args)  
        {  
            sp = new SerialPort("COM3", 4800, Parity.None, 8, StopBits.One);  
            sp.DiscardNull = false;  
            sp.RtsEnable = true;  
            sp.ReadTimeout = 500;  
            sp.Handshake = Handshake.None;  
            sp.Encoding = Encoding.UTF8;  
            sp.DtrEnable = true;    
            sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);              
            sp.Open();  
            //sp.DataReceived += sp_DataReceived;  
            string InputData = sp.ReadExisting();  
            while (sp.IsOpen)  
            {  
                try  
                {  
                    System.Threading.Thread.Sleep(300);  
                    int bytes = sp.BytesToRead;  
                    byte[] buffer = new byte[bytes];  
                    Console.WriteLine("Value - " + sp.Read(buffer, 0, bytes));  
                }  
                catch (Exception ex)  
                {  
                    Console.WriteLine(ex.Message);  
                }  
            }  
        }  
        private static void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)  
        {  
            SerialPort sport = (SerialPort)sender;  
            string indata = sport.ReadExisting();  
            Console.WriteLine(indata);  
        }  
    }  
}  

推荐答案

嗯。停止这样做!

您可以通过两种不同的方式在同一代码中读取数据:一个删除数据的循环和一个也删除数据的事件。选择一种方法,并坚持下去。



其次,检查机器是否正在输出数据,并且您的PC正在接收它:使用类似Hyperterminal的东西观察数据,并确保你们都能接收并理解它发送的数据 - 如果它发送一个字节流,它们可能必须被正确解释才能成为滚动权重 - 即使它发送的值是一个单字节,表示权重为0到255之间的值,这是一个字节可以传输的全部。几乎可以肯定的是,这并不意味着它发送的值占用多个字节,这意味着它们以某种方式打包,因此您可以分辨每个值的开始和结束位置。



如果您有确切的数据进入Hyperterminal,请阅读机器手册并找出该数据格式:然后在您尝试跳入代码之前手动解释收到的内容!
Um. Stop doing that!
You have two different ways to read data in the same code: a loop which removes data and an event which also removes data. Pick one method, and stick with it.

Second, check that the machine is outputting data at all, and that your PC is receiving it: use something like Hyperterminal to watch the data as it comes in and make sure you both receive and can understand the data it is sending - if it's sending a stream of bytes, they may have to be interpreted correctly in order to be a "rolling weight" - even if the value it sends is a single byte indicating a weight as a value between 0 and 255 which is all a byte can transfer. Almost certainly, it's not that which means that the values it sends take more than one byte, which means they are packaged in some way so you can tell where each value begins and ends.

When you have definite data coming in to Hyperterminal, read the manual for the machine and find out what that data format is: then manually interpret what is being received before you even try to leap into code!


这篇关于无法读取COM3端口。始终输出为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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