Can't open serialport on Windows CE application
I am working on a Windows CE application which involves using the serial ports of the machine. However when I try to open a port it gives an IOException. Below is the example code:
SerialPort NewSerialPort = new SerialPort("COM7", 115200, Parity.None, 8, StopBits.One);
NewSerialPort.Handshake = Handshake.None;
//opening the serial port
NewSerialPort.Open();
I have tried to close the connection before the open statement and also checked the IsOpen status before opening it (which is actually false before opening). The SerialPort.GetPortNames function gives back: COM1 / COM2 / COM5 / COM7. I tried all including in a diffrend way li开发者_如何学JAVAke this: COM1: / COM2: / COM5: / COM7:. I can't seem to get it to work.
My first thought would be that I forgot to add something in the Windows CE image. But I can't seem to find anything in the catalog which would seem important for serial communication.
Does anyone have a thought on this?
at System.IO.Ports.SerialStream.WinIOError() at System.IO.Ports.SerialStream.WinIOError() at System.IO.Ports.SerialStream.CheckResult() at System.IO.Ports.SerialStream.SetBufferSizes() at System.IO.Ports.SerialPort.Open() at commtest.Form1.button2_Click() at System.Windows.Forms.Control.OnClick() at System.Windows.Forms.Button.OnClick() at System.Windows.Forms.ButtonBase.WnProc() at System.Windows.Forms.Control._InternalWnProc() at Microsoft.AGL.Forms.EVL.EnterMainLoop() at System.Windows.Forms.Application.Run() at commtest.Program.Main()
Try the exact port name as you get it from GetPortNames() - including the colon
SerialPort NewSerialPort = new SerialPort("COM7:", 115200, Parity.None, 8, StopBits.One); // or string[] portNames = SerialPort.GetPortNames() SerialPort newSerialPort = new SerialPort(portNames[3], , 115200, Parity.None, 8, StopBits.One);
If that doesn't work, try "\\.\COM7"
SerialPort NewSerialPort = new SerialPort("\\.\COM7", 115200, Parity.None, 8, StopBits.One);
I don't know if you already have the answer to this yet but make sure you are using latest .NET CF version which is 3.5. There was a bug in the .NET CF 2.0 version that IOException created upon opening the serial port. if you look up the bug fixed under .NET CF 2.0 SP you will see these mentioned.
精彩评论