的SerialPort的WriteLine文本错误 [英] Serialport writeline textbox error
本文介绍了的SerialPort的WriteLine文本错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用来测试我的程序虚拟COM端口。我想与COM8串行写和COM9串行读。当想写从TextBox1的价值观,我得到这个错误:
IOException异常是未处理(参数不正确)
我如何摆脱呢?
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;使用System.Threading.Tasks
;使用System.Windows.Forms的
;使用System.IO.Ports
;
命名空间Flowerpod_User_Interface
{
公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent();
//有效的COM端口
的foreach(字符串s在System.IO.Ports.SerialPort.GetPortNames())
{
comboBox1.Items.Add的节目列表(S) ;
}
}
私人无效textBox1_TextChanged(对象发件人,EventArgs五)
{
}
私人无效Form1_Load的(对象发件人,EventArgs五)
{
}
私人无效的button1_Click(对象发件人,EventArgs五)
{
如果(serialPort1.IsOpen)
{
serialPort1.WriteLine(textBox1.Text);
}
,否则
{
MessageBox.Show(SerialPort1不开);
}
}
私人无效comboBox1_SelectedIndexChanged(对象发件人,EventArgs五)
{
}
私人无效Connect_Click(对象发件人,EventArgs五)
{
如果
{
serialPort1.PortName = comboBox1.SelectedItem.ToString()(serialPort1.IsOpen!);
serialPort1.Open();
textBox3.Text =打开;
}
}
私人无效textBox3_TextChanged(对象发件人,EventArgs五)
{
}
私人无效textBox2_TextChanged(对象发件人,EventArgs五)
{
}
私人无效button3_Click(对象发件人,EventArgs五)
{
随机slumpGenerator =新的随机();
//或者你想...下一页()返回一个双
INT TAL = slumpGenerator.Next(1000,10000)的任何限制;
textBox1.Text = tal.ToString();
}
私人无效textBox4_TextChanged(对象发件人,EventArgs五)
{
}
私人无效serialPort1_DataReceived(对象发件人,SerialDataReceivedEventArgs E)
{
}
私人无效button4_Click(对象发件人,EventArgs五)
{
如果(serialPortRead.IsOpen!)
{
serialPort1.PortName =COM9;
serialPortRead.Open();
textBox4.Text = serialPortRead.ReadLine();
}
}
}
}
解决方案
有没有这两个端口造成的问题之间的桥梁。虚拟COM端口软件骗我!
I use virtual COM ports for testing my program. I want to Serial Write with COM8 and Serial read with COM9. When a want to write the values from textbox1, i get this error:
IOException was unhandled (The parameter is incorrect)
How do i get rid of this?
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 Flowerpod_User_Interface
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// show list of valid COM ports
foreach (string s in System.IO.Ports.SerialPort.GetPortNames())
{
comboBox1.Items.Add(s);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.WriteLine(textBox1.Text);
}
else
{
MessageBox.Show("SerialPort1 is not open");
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Connect_Click(object sender, EventArgs e)
{
if (!serialPort1.IsOpen)
{
serialPort1.PortName = comboBox1.SelectedItem.ToString();
serialPort1.Open();
textBox3.Text = "Open";
}
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
Random slumpGenerator = new Random();
// Or whatever limits you want... Next() returns a double
int tal = slumpGenerator.Next(1000, 10000);
textBox1.Text = tal.ToString();
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void serialPort1_DataReceived(object sender,SerialDataReceivedEventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
if (!serialPortRead.IsOpen)
{
serialPort1.PortName = "COM9";
serialPortRead.Open();
textBox4.Text = serialPortRead.ReadLine();
}
}
}
}
解决方案
There wasn't any bridge between the two ports which caused the problem. The virtual COM port software tricked me !.
这篇关于的SerialPort的WriteLine文本错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文