Serial Port write Timeout error [duplicate] - c#

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 !.

Related

WebCam_Capture notfound

I'm new and I have a problem with a class.
I have ever that problem " An unhandled exception of type 'System.IO.FileNotFoundException' occurred in WinFormCharpWebCam.exe"
But I added the refrence to my code.
Someone Can help me ?
Thank's
using System;
using System.Linq;
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using WebCam_Capture;
namespace WinFormCharpWebCam
{
//Design by Pongsakorn Poosankam
public partial class mainWinForm : Form
{
public mainWinForm()
{
InitializeComponent();
}
WebCam webcam;
private void mainWinForm_Load(object sender, EventArgs e)
{
webcam = new WebCam();
// webcam.InitializeWebCam(ref imgVideo);
}
private void bntStart_Click(object sender, EventArgs e)
{
webcam.Start();
}
private void bntStop_Click(object sender, EventArgs e)
{
webcam.Stop();
}
private void bntContinue_Click(object sender, EventArgs e)
{
webcam.Continue();
}
private void bntCapture_Click(object sender, EventArgs e)
{
imgCapture.Image = imgVideo.Image;
}
private void bntSave_Click(object sender, EventArgs e)
{
Helper.SaveImageCapture(imgCapture.Image);
}
private void bntVideoFormat_Click(object sender, EventArgs e)
{
webcam.ResolutionSetting();
}
private void bntVideoSource_Click(object sender, EventArgs e)
{
webcam.AdvanceSetting();
}
}
}

Windows Form is Really Laggy

I have a windows Form Project that runs smoothly and fine on framework 4.5.1
But yet I have a project on framework 2.0 that is so laggy even after i changed the target framework to 4.5.1
so why is one project slow and the other is normal?
Update :
here is the Main From
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DataBaseLab_Library_Project
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button5_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
MessageBox.Show(" ", "About US");
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
Add AddForm = new Add();
AddForm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
Search SearchForm = new Search();
SearchForm.Show();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
}
}
and here is its design
Update 2 :
Form "Add.cs" that dose not lag .
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DataBaseLab_Library_Project
{
public partial class Add : Form
{
public Add()
{
InitializeComponent();
}
private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
AddBook AddBookForm = new AddBook();
AddBookForm.Show();
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
MainForm mainForm = new MainForm();
mainForm.Show();
}
private void label3_Click(object sender, EventArgs e)
{
}
private void publisherButt_Click(object sender, EventArgs e)
{
this.Close();
AddPublishercs AddPublisherForm = new AddPublishercs();
AddPublisherForm.Show();
}
private void authorButt_Click(object sender, EventArgs e)
{
//this.Close();
//AddAuthor AddAuthorForm = new AddAuthor();
//AddAuthorForm.Show();
}
private void Add_Load(object sender, EventArgs e)
{
}
}
}
Try removing the panel1 paint eventhandler. This will get called (even tho its empty) ever time the panel has to draw itself, which is all the time in a designer environment.
Remove this code:
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
There will also be code inside of InitializeComponent() similar to this
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1);
Remove it as well. Clean and rebuild solution. Close all designer windows. Try again.

Media Player - Preload Multiple Clips

I'm trying to create a media player that can load MULTIPLE CLIPS at a time.
Just load. Not play.
When you load the program, I was hoping that the user can click "load" and select different files for each of the buttons.
Once the clips have all been loaded, the user can go through and click a "play" button that corresponds with the Load Button.
I also hoped that beside each "play" button, there be a Loop Checkbox (if ticked, it loops the video)
The Program so far:
Current Source:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MediaPlayer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
MediaPly _mp = null;
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
_mp = new MediaPly();
}
}
private void button2_Click(object sender, EventArgs e)
{
_mp.LoadFile(openFileDialog1.FileName, this.panel1);
}
private void button3_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
_mp = new MediaPly();
}
}
private void button4_Click(object sender, EventArgs e)
{
_mp.LoadFile(openFileDialog1.FileName, this.panel1);
}
private void button5_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
_mp = new MediaPly();
}
}
private void button6_Click(object sender, EventArgs e)
{
_mp.LoadFile(openFileDialog1.FileName, this.panel1);
}
}
}
You are saving all clips to the same variable (_mp).
Try to save each in a different variable:
MediaPly _mp = null;
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
_mp = new MediaPly();
}
}
private void button2_Click(object sender, EventArgs e)
{
_mp.LoadFile(openFileDialog1.FileName, this.panel1);
}
MediaPly _mp2 = null;
private void button3_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
_mp2 = new MediaPly();
}
}
private void button4_Click(object sender, EventArgs e)
{
_mp2.LoadFile(openFileDialog1.FileName, this.panel1);
}
MediaPly _mp3 = null;
private void button5_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
_mp3 = new MediaPly();
}
}
private void button6_Click(object sender, EventArgs e)
{
_mp3.LoadFile(openFileDialog1.FileName, this.panel1);
}

Serialport writeline textbox error

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 !.

C# program to communicate with a xbee module using COM Ports / Using USB Serial COM Port

I am working a c# program which use the usb serial COM Port to transmit and receive data. The component is XBEE module which I want ot access but the code I wrote dosen't detect any serial port on my Computer. Any one who can help me rectifying my mistake.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApplication3
{
public partial class Test : Form
{
private StringBuilder receivedData = new StringBuilder();
public Test()
{
InitializeComponent();
}
private void Test_Load(object sender, EventArgs e)
{
Array ports = System.IO.Ports.SerialPort.GetPortNames();
for (int x = 0; x <= ports.Length; comboBox1.Items.Add(ports.GetValue(x))) ;
timer1.Start();
}
private void button1_Click(object sender, EventArgs e)
{
serialPort1.PortName = comboBox1.Text;
if (!serialPort1.IsOpen)
{
serialPort1.Open();
}
}
private void button2_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
}
}
private void Test_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
}
}
private void button3_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Write(textBox2.Text + "\n\r");
}
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
receivedData.Append(serialPort1.ReadExisting());
}
private void timer1_Tick(object sender, EventArgs e)
{
textBox1.Text = receivedData.ToString();
}
}
}
You need to set the correct parameters on the serial port to match the settings of the device. Have you installed XCTU? Take note of the settings in the "search for devices" dialog - assuming it does find your device!
For me, I found I actually just needed to set the baud rate:
var serialPort = new SerialPort(portName);
serialPort.BaudRate = 115200;
serialPort.Open();
// win!

Categories