串口通信问题 [英] Serial port communication problem

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

问题描述

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



namespace PortDataReceived
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort mySerialPort = new SerialPort("COM1");
            if (mySerialPort.IsOpen == true) mySerialPort.Close();
            mySerialPort.BaudRate = 2400;
            mySerialPort.Parity = Parity.None;
            mySerialPort.StopBits = StopBits.One;
            mySerialPort.DataBits = 8;
            mySerialPort.Handshake = Handshake.None;
            mySerialPort.Open();
            Console.WriteLine("Press any key to continue...");
            Console.WriteLine();
            mySerialPort.ReadTimeout = 500;
            string indata = mySerialPort.ReadExisting();
            if (string.IsNullOrEmpty(indata))
            {
                FileStream fs = new FileStream(@"c:\arjun.txt",FileMode.Append,FileAccess.Write);
                try
                {
                   
                    StreamWriter sw = new StreamWriter(fs);
                    sw.WriteLine("0");
                    sw.Close();
                }
                finally
                {
                    fs.Close();
                }
                
            }
            else
            {
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < indata.Length; i += 8)
                {

                    string section = indata.Substring(i, 8);

                    int ascii = 0;
                    try
                    {
                        ascii = Convert.ToInt32(section, 2);
                    }
                    catch
                    {
                        Console.WriteLine("Current Weight : " + section.ToString());
                        FileStream fs = new FileStream(@"c:\arjun.txt", FileMode.Append, FileAccess.Write);
                        try
                        {
                            StreamWriter sw = new StreamWriter(fs);

                            foreach (char c in section)
                            {
                                if (Char.IsLetterOrDigit(c))
                                {
                                    builder.Append(c);
                                }
                            }

                            sw.WriteLine(builder.ToString());
                            sw.Close();
                            break;
                        }
                        finally
                        {
                            fs.Close();
                        
                        }

                    }
                   
                }
            }
           // Console.WriteLine(builder.ToString());
            Console.WriteLine(indata);
            Console.WriteLine("Data Received:");
            Console.Write(indata);
            Console.ReadKey();
            mySerialPort.Close();
        }
    }
}











你好以上所有以上是我的代码在这个代码中我从com端口读取数据但问题是我收到6到7零比从comport收到的实际数据我不是在什么是我的代码中的问题请让我知道我非常感谢所有人。我想要优先解决方案pls






hello all the above is my code in this code i am read data from com port but problem is i was received 6 to 7 zero than received actual data from comport i was not under what was problem in my code pls let me know i was very thank full to all.i want solution on priority pls

推荐答案



可能没有什么理由。但最关键的一点是您创建的串口是否可以读取数据。我专注于此串口的波特率。你怎么想出2400这个。指定此值时,需要确保写入数据的应用程序应具有相同的波特率(发送方和接收方应具有相同的波特率)。因此,作为起点,您可以将波特率设置为与发送方相同。之后你可以继续花一些调试。



如果有一些不需要的数据字节,它应该与
Hi,
There could be few reasons. But the most critical one is whether the serial port that you created can read the data. I focus on the Baud Rate of this serial port. How you came up with 2400 for this. When specifying this value you need to make sure that the application that writes the data should have the same baud rate (both sender and receiver should have same baud rate). So as a start point you can set the Baud Rate as same as the sender. After that you can move forward with spending some debugging.

If there is some unwanted data bytes it should be related to
mySerialPort.ReadExisting();

因为此方法读取com端口缓冲区中的所有数据。所以想一想在再次发送数据之前清理它。

问候。

As this method reads all data in the com port buffer. So think a way to clean it before send data again.
Regards.


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

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