Win32 Error after opening connection port

Oct 20, 2010 at 11:59 AM
Edited Oct 20, 2010 at 11:59 AM

GetLastWin32Error method returns error code 12 after calling CreateFile and GetCommState methods in SerialStream constructor.

I'm using windows mobile 6.1. Any suggestion?

Oct 26, 2010 at 7:59 AM

My solution :

Change SerialStream constructor.

1- Set baudRate value to 9600 in first  line of constructor. Greater values causes problems in detection progress.

2- Change CreateFile to CreateFileW.

        public SerialStream(string portName, int baudRate, FileAccess access, FileShare sharing)
        {
            try
            {
                baudRate = 9600;

                // Remember the port name and baud rate
                _PortName = portName;
                _Access = access;
                BaudRate = baudRate;

                //// Open a connection to the port
                //_Handle = NativeMethods.CreateFile(
                //    portName,
                //    access,
                //    sharing,
                //    0,
                //    FileMode.Open,
                //    FileAttributes.Normal,
                //    IntPtr.Zero);

                UInt32 OPEN_EXISTING = 3;
                UInt32 GENERIC_READ = 0x80000000;
                UInt32 GENERIC_WRITE = 0x40000000;
                UInt32 CreateAccess = GENERIC_READ;

                _Handle = NativeMethods.CECreateFileW(
                    portName,
                    CreateAccess,
                    (uint)sharing,
                    IntPtr.Zero,
                    OPEN_EXISTING,
                    (uint)FileAttributes.Normal,
                    IntPtr.Zero);

in nativemethods.cs add :

        [DllImport("coredll.dll", EntryPoint = "CreateFileW", SetLastError = true)]
        public static extern IntPtr CECreateFileW(
            String lpFileName, UInt32 dwDesiredAccess, UInt32 dwShareMode,
            IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition, UInt32 dwFlagsAndAttributes,
            IntPtr hTemplateFile);