Setting SerialPort.DataBits

Sep 15, 2010 at 4:06 PM

I integrated your wrapper into my project and was able to create a reference to the serial port object without late binding, which is great!  But, I seem to be running into a problem changing the DataBits when configuring the SerialPort object.

Here is a code snippet in VB.

 Dim serialPort As New System.IO.Ports.SerialPort
        With serialPort            

.BaudRate = 115200            

.DataBits = 7            

.Parity = IO.Ports.Parity.Even            

.StopBits = IO.Ports.StopBits.One    

.Open   

End With

I can leave .DataBits out of it and it opens fine, but I need to set the data bits to 7 as that is what is required by my device.

Thanks for your help!

 

Ryan

Coordinator
Sep 20, 2010 at 6:52 AM
Edited Sep 20, 2010 at 10:30 AM

Hi Ryan,

internally this uses late-binding - so it is not without latebinding but just saves you the handling of it.

I just returned from a trip and will look into the problem today or tomorrow and than get back to you.

What is the problem you run into? Do you get an InvalidOperationException with message "No active port."? If so try to create the instance first, then set your params and finally open the port instead of using the With-syntax.

I am not familar with VB so I don't know if the code you posted can work with my implementation. The DataBits Property needs the object to be initialized before. Probably I should change that. And I certainly will, if you can confirm, that first instanciating SerialPort before setting that parameters solves your problem.

 

If you run into other poblems than the beforementioned InvalidOperationExeption, please drop me a note, so I can try to locate and solve that error.

Regards,

Paul

Jul 19, 2011 at 8:36 AM

Same thing.  follow is good (worked).

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            SerialPort port = new SerialPort("COM10", 19200, Parity.Odd);
            int Bits = port.DataBits;
            StopBits Stop = port.StopBits;
            port.Open();
            port.Write("ABCDEFGHIJKLMN\r\n");
            port.Close();
        }

--------------------------------

follow is NOT WORK.


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            SerialPort port = new SerialPort("COM10", 19200, Parity.Odd);
            int Bits = port.DataBits;
            StopBits Stop = port.StopBits;
            port.DataBits = 7;
            port.Open();
            port.Write("ABCDEFGHIJKLMN\r\n");
            port.Close();
         }

I can not change "port.DataBits" and "port.StopBits".  and I read source code (yours), but I can't solve this problem.

I'm using Windows 7 ultimate 64 bits.  with VS2010 Professional.

Coordinator
Jul 19, 2011 at 9:09 AM

Hi shio.
This is a Problem I also ran into, lately. But I had the pro lem only when trying to set these values via c'tor. The way you changed them (after initializing) worked for me. I will have to investigate this problem. As a workaround in the wrapper you could try to initialize the dynamic COM object with the values you need.
Hope this helps for now.


Regards, Paul

Jul 20, 2011 at 3:30 AM

OK, I added following line to SerialPort project (interop).

it works.  but not convenience.

namespace InteropComObjects.IO.Ports {
   /// <summary>Makes the serial ports COM accessible
    /// </summary>
    [Guid("30E24601-6E65-4FAF-9999-2B135F0B512F"),
    ClassInterface(ClassInterfaceType.None),
    ComSourceInterfaces(typeof(ISerialPortEvents))]
    public class SerialPort : ISerialPort {

        private readonly System.IO.Ports.SerialPort selectedPort;
        private String[] portNames;
        private static Dictionary<int, string> errors;

        /// <summary>Initializes a new COM accesible object of a serial port
        /// </summary>
        public SerialPort() {
            selectedPort = new System.IO.Ports.SerialPort();
            errors = new Dictionary<int, string>();
            selectedPort.DataBits = 7;                                <==================  insert to InteropComObjects.IO.Ports  SerialPort.cs
        }

 

Coordinator
Jul 20, 2011 at 4:40 AM

Ya, I know this is not  convenient at all. Unfortunately my paid job  keeps me all busy at the moment. I will investigate further soon.

Thank you for trying out this rather unintuitive workaround.

Regards,

Paul

Jul 20, 2011 at 4:42 AM

this ???

        public int DataBits {
            get {
                if (selectedPort != null) return selectedPort.DataBits;
                throw new InvalidOperationException("No active port.");
            }
            set {
                if (selectedPort != null) selectedPort.DataBits = value;
                throw new InvalidOperationException("No active port.");  <<<<< else ?
            }
        }
 

       always throw exception ?

Coordinator
Jul 20, 2011 at 6:16 AM
Edited Jul 21, 2011 at 8:42 AM

Hi shio.

Thank you for yor hint.

I replaced the downloads with a newer version, reflecting your suggested change.

 

Regards,

Paul