0

If I run my following Code, which I created with SharpDevelop:

        SerialPort serial = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);

        void sendMsg_Click(object sender, EventArgs e)
        {
            serial.Open();
            serial.WriteLine(textBox1.Text);
            serial.Write(new byte[] {0x0A, 0xE2, 0xFF}, 0, 3);
            serial.Close();
        }

Below is the Exception appearing while executing the code :

The Exception is the following:

System.IO.IOException: Falscher Parameter.
bei System.IO.Ports.InternalResources.WinIOError
bei System.IO.Ports.SerialStream.EndWrite
bei System.IO.Ports.SerialStream.Write
bei System.IO.Ports.SerialPort.Write
bei System.IO.Ports.SerialPort.WriteLine
bei Chat_via_RS232.MainForm.sendMsg_Click in c:\Users\admin\Documents\SharpDevelop Projects\Latias.eu IT\Chat via RS232\MainForm.cs:Zeile 35
bei System.Windows.Forms.Control.OnClick
bei System.Windows.Forms.Button.OnClick
bei System.Windows.Forms.Button.OnMouseUp
bei System.Windows.Forms.Control.WmMouseUp
bei System.Windows.Forms.Control.WndProc
bei System.Windows.Forms.ButtonBase.WndProc
bei System.Windows.Forms.Button.WndProc
bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage
bei System.Windows.Forms.Control.ControlNativeWindow.WndProc
bei System.Windows.Forms.NativeWindow.DebuggableCallback
bei System.Windows.Forms.Application.ComponentManager.FPushMessageLoop
bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner
bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop
bei System.Windows.Forms.Application.Run
bei Chat_via_RS232.Program.Main in c:\Users\admin\Documents\SharpDevelop Projects\Latias.eu IT\Chat via RS232\Program.cs:Zeile 24

Can someone help me please.

Regards

Lala

Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
  • What is the exact actual value of `textBox1.Text` when the error happens? – Dai Jan 30 '17 at 08:54
  • Which one is "MainForm.cs:Zeile 35" ? – Fildor Jan 30 '17 at 08:55
  • It already tells you: "Wrong Parameter". If you had added the line numbers or would tell us where it crashed (it says line 35 - but we don't know which one that is) you could get more help. But you should be able to resolve it yourself with that info. – Matthias247 Jan 30 '17 at 08:55
  • the line 35 is serial.Close(); – Lala Sabathil Jan 30 '17 at 08:56
  • in my textbox i type "abc" as test string – Lala Sabathil Jan 30 '17 at 08:57
  • OK, if the error is in `SerialPort.Close` then is a problem with the parameters you are passing to `Open` method. [MSDN](https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.close(v=vs.110).aspx) says that a IOException error would be thrown if _An attempt to set the state of the underlying port failed. For example, the parameters passed from this SerialPort object were invalid_ – Pikoh Jan 30 '17 at 09:01
  • com2 is an virtual serialport, i used com1 and it work – Lala Sabathil Jan 30 '17 at 09:07
  • Google for "SerialPort Incorrect parameter" and you'll find other people have had the same problem including http://stackoverflow.com/questions/14885288/io-exception-error-when-using-serialport-open – Joe Jan 30 '17 at 09:08
  • I think i've read others having problems with virtual serial ports. Have you tried to pass only port name to the `SerialPort` constructor and see if that way you don't get the error? `SerialPort serial = new SerialPort("COM2");` – Pikoh Jan 30 '17 at 09:10
  • yeah, that i tryed first, but it doesn't work ether – Lala Sabathil Jan 30 '17 at 09:15
  • You really have to **set all settings** of the serial port, even if you simply stick to standard values! The main reason is, that a COM port doesn't reset the current states when you open it. So if another program sets the COM port to some awkward values and you don't actively overwrite them they will be taken by your current connection. – Oliver Jan 30 '17 at 09:43
  • both ways doesn't work – Lala Sabathil Jan 30 '17 at 10:12

1 Answers1

0

The error is on this line:

serial.WriteLine(textBox1.Text)

Your exception report shows an IOException being raised, however the documentation for WriteLine does not list IOException as a documented exception: https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.writeline(v=vs.110).aspx - it's possible something else is going wrong.

The documentation for SerialPort.Open indicates if the port is unavailable then an exception is thrown there ( https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.open(v=vs.110).aspx ) but this isn't happening in your program. I suspect there might be invalid values being passed into the WriteLine function. What happens if you move the Write( Byte[] ) call to before the WriteLine call?

Dai
  • 141,631
  • 28
  • 261
  • 374