无法知道serialport程序中的错误 [英] Could not know error in serialport program

查看:81
本文介绍了无法知道serialport程序中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hellow

在以下2个程序中我从msdn.i获得它的主要部分试图让它有两个路径(读写)我的意思是计算机A中的第一个程序(发送)和计算机B中的下一个程序(接收)经过多次尝试它工作正常然后我开始改进它(我的意思是将一些变量从全局更改为本地)但我在接收程序中出错,我想我的错误是如何声明变量

name,message,_continue和stringComparer。它们应该是全球性的还是静态的......

这样的东西

编程串口难以想象并难以遵循其流程图

i我已尽全力

需要你的帮助

非常感谢



我有什么试过:



hellow
in the following 2 programs which i have got the main part of it from msdn.i tried to have it two path (read and write ) i mean 1st program in computer A (sending)and the next program (receiving ) in computer B. after many tries it was working fine then i begin to improve it (i mean to change some variable from global to local) but i got errors in the receiving program , i guess my error is how to declare the variables
name,message,_continue,and stringComparer. should they be global and static ....
something like this
programming serial port is difficult to imagine and hard to follow its flowchart
i have given it all i can
needs your fine help
many thanks

What I have tried:

        static SerialPort _serialPort = new SerialPort();
        static bool _continue = true;
        //static StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        public static void Main(string[] args)
        {
            string name;
            string message;
            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            Thread readThread = new Thread(Read);
//serial port setting =com1,9600,Parity.None,8,StopBits.One,Handshake.Nonereadtimeout=500,wrtetimeout=500
            _serialPort.Open();
            _continue = true;
            readThread.Start();
            Console.Write("Name: ");
            name = Console.ReadLine();
            Console.WriteLine("Type QUIT to exit");
            while (_continue)
            {
                message = Console.ReadLine();
                if (stringComparer.Equals("quit", message))
                {
                    _serialPort.WriteLine(String.Format("<{0}>: {1}", name, message));
                    _continue = false;
                }
                else
            _serialPort.WriteLine( String.Format("<{0}>: {1}", name, message) );
                  }//while
            
   readThread.Join();   _serialPort.Close();
        }//Main
        //----------------------------------------------------------
        public static void Read()
        {
            string name="MyName";
            string message;
            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            while (_continue)
            {
                try
                {
                    //string message = _serialPort.ReadLine();
                    message = _serialPort.ReadLine();
                    Console.WriteLine(String.Format("<{0}>: {1}", name, message));
                    if (stringComparer.Equals("quit", message))
                    {
                        _continue = false;
                    }

                }//try

                catch (TimeoutException) { }
            }//while
        }//Read

    }
}
//=============================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO.Ports;

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

namespace SerialConsol
{
    class Program
    {
        static SerialPort _serialPort = new SerialPort();
        //static StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;

        static bool _continue = true;
        //static string name;
        //static string message;

        public static void Main(string[] args)
        {
            string name;
            string message;
            //bool _continue = true;
            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            Thread readThread = new Thread(Read);

            // Create a new SerialPort object with default settings.
            //SerialPort _serialPort = new SerialPort();

            // Allow the user to set the appropriate properties.
            _serialPort.PortName  = "com1" ;//SetPortName(_serialPort.PortName);
            _serialPort.BaudRate  = 9600;//SetPortBaudRate(_serialPort.BaudRate);
            _serialPort.Parity    = Parity.None;//SetPortParity(_serialPort.Parity);
            _serialPort.DataBits  = 8;//SetPortDataBits(_serialPort.DataBits);
            _serialPort.StopBits  = StopBits.One;//SetPortStopBits(_serialPort.StopBits);
            _serialPort.Handshake = Handshake.None;//SetPortHandshake(_serialPort.Handshake);

            // Set the read/write timeouts
            _serialPort.ReadTimeout = 500;
            _serialPort.WriteTimeout = 500;

            _serialPort.Open();
            //_continue = true;
            readThread.Start();

            Console.Write("Name: ");
            name = Console.ReadLine();

            //Console.WriteLine("Type QUIT to exit");
            /*while (_continue)
            {
                message = Console.ReadLine();

                if (stringComparer.Equals("quit", message))
                {
                    _continue = false;
                }
                else
                {
                    _serialPort.WriteLine(
                        String.Format("<{0}>: {1}", name, message));
                }
            }//while
            */
            Read();
            readThread.Join();
            _serialPort.Close();

        }//Main
        //----------------------------------------------------------
        public static void Read()
        {
            //Console.WriteLine("hellow read");
            string name = "MyName";
            string message;
           //bool _continue = true;
            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            //Console.Write("Name: ");
            //name = Console.ReadLine();

            while (_continue)
            {
                try
                {
                    //string message = _serialPort.ReadLine();
                    message = _serialPort.ReadLine();
                    Console.WriteLine(String.Format("<{0}>: {1}", name, message));

                    if (stringComparer.Equals("quit", message))
                    {
                        _continue = false;
                    }
                    //else
                    //{
                    //    _continue = true;
                    //}
                }//try
                catch (TimeoutException) { }
            }//while
        }//Read

    }
}

推荐答案

//我猜电缆不好(有些电线似乎连接但实际上没有)这迫使我责怪一个nd修改代码。这里的代码最终运行没有任何错误,抱歉错误

// i guess the cable was not good ( some wires were seems it is connected but actually not )which force me to blame and modify the code. here the code which finally run without any error ,sorry for the mistake
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO.Ports;

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

namespace SerialConsol
{
    class Program
    {
        static SerialPort _serialPort;
        static bool _continue;

        public static void Main(string[] args)
        {
            string name;
            string message;
            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            Thread readThread = new Thread(Read);

            // Create a new SerialPort object with default settings.
            _serialPort = new SerialPort();

            // Allow the user to set the appropriate properties.
            _serialPort.PortName  = "com1" ;//SetPortName(_serialPort.PortName);
            _serialPort.BaudRate  = 9600;//SetPortBaudRate(_serialPort.BaudRate);
            _serialPort.Parity    = Parity.None;//SetPortParity(_serialPort.Parity);
            _serialPort.DataBits  = 8;//SetPortDataBits(_serialPort.DataBits);
            _serialPort.StopBits  = StopBits.One;//SetPortStopBits(_serialPort.StopBits);
            _serialPort.Handshake = Handshake.None;//SetPortHandshake(_serialPort.Handshake);

            // Set the read/write timeouts
            _serialPort.ReadTimeout = 500;
            _serialPort.WriteTimeout = 500;
          
            _serialPort.Open();
            _continue = true;
            readThread.Start();

            Console.Write("Name: ");
            name = Console.ReadLine();

            Console.WriteLine("Type QUIT to exit");

            while (_continue)
            {
                message = Console.ReadLine();

                if (stringComparer.Equals("quit", message))
                {
                    _continue = false;
                }
                else
                {
                    _serialPort.WriteLine( String.Format("<{0}>: {1}", name, message) );
                }
            }//while
            
            readThread.Join();
            _serialPort.Close();

        }//Main
        //----------------------------------------------------------
        public static void Read()
        {
            while (_continue)
            {
                try
                {
                    string message = _serialPort.ReadLine();
                    Console.WriteLine(message);//String.Format("<{0}>: {1}", name, message));
                }//try
                catch (TimeoutException) { }
            }//while
        }//Read

    }
}


这篇关于无法知道serialport程序中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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