c#串口通信停止位设置遇到的问题

易2017 2017-09-01 08:51:44
串口通信中停止位有1 1.5 2三种,c#中System.IO.Ports中定义了一个枚举变量,代码如下
// 摘要: 
// 指定在 System.IO.Ports.SerialPort 对象上使用的停止位的数目。
public enum StopBits
{
// 摘要:
// 不使用停止位。 System.IO.Ports.SerialPort.StopBits 属性不支持此值。
None = 0,
//
// 摘要:
// 使用一个停止位。
One = 1,
//
// 摘要:
// 使用两个停止位。
Two = 2,
//
// 摘要:
// 使用 1.5 个停止位。
OnePointFive = 3,
}

当使用停止位0时会出现错误这是可以理解的,但是使用停止位1.5还出现错误就不可理解了,会出现一个配置错误的提示,有兴趣的大佬可以试试,这是open函数,代码太多也不知道怎么贴
 public void OpenPort()
{
if (comPort.IsOpen)
comPort.Close();

comPort.PortName = _portName;
comPort.BaudRate = (int)19200;
comPort.Parity = Parity.None;
comPort.DataBits = (int)8;
comPort.StopBits = StopBits.OnePointFive;

try
{
comPort.Open();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}
...全文
1351 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
易2017 2017-09-01
  • 打赏
  • 举报
回复
引用 5 楼 qq_38204686 的回复:
Windows不支持这个配置。根据微软的规定,数据位数必须为5到8,使用5个数据位和2个停止位是非法的组合。同样,使用6、7、8个数据位和1.5个停止位也是非法的。当使用5个数据位时,可以使用1或1.5个停止位。使用其它数据位选项(6,7,8)时,只能使用1或2个停止位. http://www.eleok.com/thread-86448-1-1.html
就服你,分拿好,不送
大米粥哥哥 2017-09-01
  • 打赏
  • 举报
回复
Windows不支持这个配置。根据微软的规定,数据位数必须为5到8,使用5个数据位和2个停止位是非法的组合。同样,使用6、7、8个数据位和1.5个停止位也是非法的。当使用5个数据位时,可以使用1或1.5个停止位。使用其它数据位选项(6,7,8)时,只能使用1或2个停止位. http://www.eleok.com/thread-86448-1-1.html
大米粥哥哥 2017-09-01
  • 打赏
  • 举报
回复
windows不支持吧
易2017 2017-09-01
  • 打赏
  • 举报
回复
引用 2 楼 qq_38588710 的回复:
虽然是个小问题,分也有点少,但是蚊子再小也是肉,望高手不吝赐教。当把停止位设置改成
            comPort.StopBits = (StopBits)1.5;
程序可以正常运行,我想是不是MSDN中存在某些bug,还是我忽略了什么,来来来,总得有个人接分
这个1.5强制转换后变成了1,所以又回到初始起点了,我想大概是串口通信中的1.5位存在某些问题
易2017 2017-09-01
  • 打赏
  • 举报
回复
虽然是个小问题,分也有点少,但是蚊子再小也是肉,望高手不吝赐教。当把停止位设置改成
            comPort.StopBits = (StopBits)1.5;
程序可以正常运行,我想是不是MSDN中存在某些bug,还是我忽略了什么,来来来,总得有个人接分
易2017 2017-09-01
  • 打赏
  • 举报
回复
给个小demo,大家运行试试
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }
        public SerialPort comPort = new SerialPort();//初始化端口类对象
        private void button1_Click(object sender, EventArgs e)
        {
            if (comPort.IsOpen)
                comPort.Close();

            comPort.PortName = "COM9";
            comPort.BaudRate = (int)19200;
            comPort.Parity = Parity.None;
            comPort.DataBits = (int)8;
            comPort.StopBits = StopBits.OnePointFive;

            try
            {
                comPort.Open();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


        }

        private void Form1_Load()
        {

        }
    }
}

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧