串行通讯错误 [英] Serial Communication errors

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

问题描述

你好朋友,

     我正在编写一个将写入串行端口的应用程序,并且我想在超级终端上读取相同的数据.但是我无法在超级终端上读取数据.有人能建议我一些可以帮助我运行此简单应用程序的信息吗? .im使用C#编码

       i am writing an application which will write to the serial port,and i want to read the same data on hyperterminal.but i m not able to read the data on hyperterminal.can any body suggest me something which could help me to run this simple application.i m coding using C#

这是我的代码,我期望在超级终端上进行读写的一些解决方案

here is my code, i am expecting some solutions for writing and as well as reading on hyperterminal

 

使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.IO.Ports;
命名空间GetPort
{
   类Program
    {
      静态void Main(string [] args)
       {
        
           SerialPort serialPort =新的SerialPort("COM3",9600,Parity.None,8,StopBits.One);
String []端口= SerialPort.GetPortNames();
            for(int i = 0; i< ports.Length; i ++)
            {
              
               System.Console.WriteLine(端口[i]);
            
           }
          尝试
            {
               serialPort.Open();
             如果(serialPort.IsOpen)
{
                  System.Console.WriteLine("USB端口已打开");
;    byte [] msg =新的byte [] {1,0,1,0};
;      serialPort.Write(msg,0,msg.Length);
              ;    System.Console.WriteLine(写入完成");

         &bb ;     byte [] data = new byte [serialPort.BytesToRead];
             b ;    System.Console.WriteLine(serialPort.Read(data,0,data.Length));   
               
               &bb //String msg = serialPort.ReadLine();
             &bb; ; //System.Console.WriteLine();
             &bb;     serialPort.WriteTimeout = 500;
              }
               else
               {
                  //serialPort.Close();
             &bsp;   System.Console.WriteLine("USB端口已关闭");
; }
           }
           catch(异常前)
            {
               System.Console.WriteLine("+ ex.Message);
            }
          终于
            {
               serialPort.Close();
              System.Console.WriteLine(端口关闭");
System.Console.ReadLine();
            }
         
       }

        }
    }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
namespace GetPort
{
    class Program
    {
        static void Main(string[] args)
        {
         
            SerialPort serialPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
            String[] ports = SerialPort.GetPortNames();
            for (int i = 0; i < ports.Length; i++)
            {
               
                System.Console.WriteLine(ports[i]);
              
            }
             try
             {
                 serialPort.Open();
                 if (serialPort.IsOpen)
                 {
                     System.Console.WriteLine("USB Port is OPEN");
                     byte[] msg = new byte[] {1,0,1,0};
                     serialPort.Write(msg,0,msg.Length);
                     System.Console.WriteLine("writing finished");

                     byte[] data = new byte[serialPort.BytesToRead];
                     System.Console.WriteLine(serialPort.Read(data, 0, data.Length));   
                  
                     //String msg = serialPort.ReadLine();
                   // System.Console.WriteLine();
                     serialPort.WriteTimeout = 500;
                 }
                 else
                 {
                     //serialPort.Close();
                     System.Console.WriteLine("USB Port is CLOSED");
                 }
             }
             catch (Exception ex)
             {
                 System.Console.WriteLine(" " + ex.Message);
             }
             finally
             {
                 serialPort.Close();
                System.Console.WriteLine("Port Closed");
                 System.Console.ReadLine();
             }
          
        }

        }
    }

请尽快回复我.

谢谢

Ecosafe

推荐答案

您的代码显然没有错...您可以对设置进行更多描述吗(提到了USB,我想这是USB到串行端口吗?),您希望程序执行什么操作?观察到的行为是什么?

There is nothing obviously wrong with your code... could you be a little more descriptive about your setup (there is a mention to USB, I guess this is a USB to serial?), what you would expect your program to do and what is the observed behavior instead?

与往常一样,每当涉及到串行端口时,请检查您的设置:波特率,奇偶校验,停止位,握手...

And as usual, whenever a serial port is involved, check your settings: baudrate, parity, stop bits, handshake...

感谢
--mc

Thanks
--mc


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

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