使用串口从计算机连接到机器的问题。(Windows应用程序) [英] Problem on connection from computer to machine with serial port.(windows application)

查看:93
本文介绍了使用串口从计算机连接到机器的问题。(Windows应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从setup(exe)连接机器时,它会给出错误和挂起设置,它不是所有时间,但有时候应该连接,有时会出错。



当我尝试使用asp.net平台时,就像那时由asp.net代码运行没有任何错误发生。(它工作正常)。



我尝试了什么:



when i try to connect with machine from setup(exe) then it gives error and hang setup, it is not all time but some time it should be connect and some times gives error.

when i try same with asp.net platform, like that is run by asp.net code at that time no any error is occurs.(it works fine).

What I have tried:

private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {             
                Thread.Sleep(10);
                _rxString = "";
                _rxString = serialPort.ReadExisting();
                                         
                this.Invoke(new EventHandler(StartUTM));
              
               }
            catch (Exception ex)
            { 
                log.Error("Error", ex);
            }

推荐答案

我认为你需要一个试试......抓住初始化 serialPort 的部分,并使用 sender 对象,如下所示:

I think you need a try ... catch around the part that initializes serialPort, and use the sender object, something like this:
var serialPort = new SerialPort(Rs232Port);

        private static void SerialDataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                var sp = (SerialPort)sender;
                var message = sp.ReadExisting();
                Debug.Print("Serial data received: " + message);
            }
            catch (Exception ex)
            {
                Debug.Print(@"SerialDataReceivedHandler() " + ex.Message);
            }
        }


这篇关于使用串口从计算机连接到机器的问题。(Windows应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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