关于#c##的问题:为什么用Modbus slave添加一对虚拟串口,发送数据无法上位机无法读取到数据

为什么用Modbus slave添加一对虚拟串口,发送数据无法上位机无法读取到数据?

img

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Modbus.Device;
using System.Windows.Forms;

namespace 串口调试助手
{
    public partial class SerialDebug : Form
    {
        SerialPort serialPort1 = new SerialPort();
        public SerialDebug()
        {
            InitializeComponent();
        }
        /// 
        /// 退出程序功能
        /// 
        /// 
        /// 
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //打开该程序
            //取该程序文件的路径
            //string path = Application.StartupPath;

            string path = System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName;

            //造一个进程
            Process p = new System.Diagnostics.Process();

            //造一个进程的启动信息
            ProcessStartInfo ps = new ProcessStartInfo(path);

            //设置进程启动信息
            p.StartInfo = ps;

            //启动进程
            p.Start();

            //关闭程序
            this.Close();
        }
        /// 
        /// 获取可用串口号
        /// 
        public void GetcomName() {
            string[] GetcomList = SerialPort.GetPortNames();
            comboBox_Serial.Items.Clear();//在存入可用串口号前,清空之前的端口号
            if (GetcomList.Length > 0)
            {
                for (int i = 0; i < GetcomList.Length; i++)
                {
                    comboBox_Serial.Items.Add(GetcomList[i]);
                    comboBox_Serial.SelectedIndex = 0;
                }
                toolStripStatus_Port.Text = "可用串口号:" + comboBox_Serial.Text;//在状态栏显示可用串口号
                toolStripStatus_Port.ForeColor = Color.Green;//将字体颜色设置为绿色

            }
            else
            {
                toolStripStatus_Port.Text = "可用串口号:无";
                toolStripStatus_Port.ForeColor = Color.Red;//将字体颜色设置为红色
                comboBox_Serial.Text = "无可用串口";
                comboBox_Serial.Enabled = false;

            }
        }

        /// 
        /// 串口参数写入初始化
        /// 
        /// 
        /// 
        public void SerialPortArgs() {
            try
            {
                
            // 设置端口基本信息
            serialPort1.PortName = comboBox_Serial.Text;
            serialPort1.BaudRate = Convert.ToInt32( comboBox_BandRate.SelectedItem);
            serialPort1.DataBits = Convert.ToInt32(comboBox_Data.SelectedItem);
            serialPort1.Handshake = Handshake.None;
            if (comboBox_Stop.SelectedItem.Equals(1))
            {
                serialPort1.StopBits = StopBits.One;
            }
            else
            {
                    serialPort1.StopBits = StopBits.Two;
            }
            //serialPort1.ReadTimeout = -1;
            //serialPort1.DtrEnable = true;
            if (comboBox_Check.SelectedItem.Equals("None"))
            {
                serialPort1.Parity = Parity.None;
            }
            if (comboBox_Check.SelectedItem.Equals("Odd"))
            {
                serialPort1.Parity = Parity.Odd;
            }
            if (comboBox_Check.SelectedItem.Equals("Even"))
            {
                serialPort1.Parity = Parity.Even;
            }
            // 打开串口端口
            serialPort1.Open();
                    ReceiveData();
                }
            catch (Exception)
            {

                MessageBox.Show("打开串口失败","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
}
        
        private void SerialDebug_Load(object sender, EventArgs e)
        {
            GetcomName();
        }
        /// 
        /// 打开串口
        /// 
        /// 
        /// 
        private void button_OK_Click(object sender, EventArgs e)
        {
            SerialPortArgs();
        }
        /// 
        /// 串口接收数据
        /// 
        /// 
        /// 
        public void ReceiveData() {
            int len = serialPort1.BytesToRead;
            byte[] data = new Byte[len];
            string SDateTemp = "";
            try
            {
                
                int a = serialPort1.Read(data, 0, len);   // 向串口中读取数据
                //richTextBox_Receive.Text = Convert.ToString(a);
                SDateTemp = Encoding.Default.GetString(data);
                //for (int i = 0; i < data.Length; i++)
                //{
                //    richTextBox_Receive.Text +=Convert.ToString(data[i]);
                //}
                MessageBox.Show(SDateTemp);
            }
            catch (Exception)
            {
                MessageBox.Show("数据接收失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            //for (int i = 0; i < data.Length; i++)
            //{
            //    richTextBox_Receive.Text += data[i];
            //}
            richTextBox_Receive.Text=SDateTemp;
            //问题:用虚拟软件发送数据,接收不到,数据为空

        }
    }
}


可能是Modbus slave的配置不正确,比如Modbus slave的地址、波特率、校验位等参数设置不正确,或者Modbus slave的硬件连接不正确,或者上位机的Modbus协议设置不正确。