Re: EMERGENCY: Problem with COM port (RS232) communication
From: Nicholas Paldino [.NET/C# MVP] (mvp_at_spam.guard.caspershouse.com)
Date: 03/17/04
- Next message: Nicholas Paldino [.NET/C# MVP]: "Re: Debug option vs release .."
- Previous message: webdudeIA: "re:Use existing C code in C# application"
- In reply to: AlliXSenoS: "EMERGENCY: Problem with COM port (RS232) communication"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 17 Mar 2004 17:16:23 -0500
AlliXSenoS,
When calling the CreateFile method, you probably should call pass
FILE_FLAG_NO_BUFFERING (0x20000000) and FILE_FLAG_WRITE_THROUGH (0x80000000)
in for the flags value, indicating that you don't want anything to be
buffered. It is possible that there is a buffer that the OS is trying to
fill, which it is waiting on (after all, not many people want to read one
byte at a time, and there is probably no length descriptor on the COM port,
so it thinks that it can just keep reading and reading).
Also, of course, make sure you are opening the right COM port =)
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
"AlliXSenoS" <pebg-kcws@disposable.spamcon.org> wrote in message
news:Xns94AFE76C280E4allixs@161.53.2.66...
> hi to everybody
>
> I have a small but urgent problem... seems me and my friend got ourselves
> in a project a bit out of our league, and now we're stuck. and the
delivery
> date was... um. last month :)
>
> the program is supposed to communicate with a COM port-based device that
is
> actually a reader for keys (a lo-tek version of USB thumbdrives) that
carry
> information.
>
> the communication is done in 'bytes' because the device is pretty stupid
> and uses 1-byte commands and responses
>
> we picked up this code from some MSDN page dealing with COM port
> communication:
>
> #region COM port HocusPocus
> [DllImport("Kernel32.dll")]
> static extern IntPtr CreateFile(
> string filename,
> [MarshalAs(UnmanagedType.U4)]FileAccess fileaccess,
> [MarshalAs(UnmanagedType.U4)]FileShare fileshare,
> int securityattributes,
> [MarshalAs(UnmanagedType.U4)]FileMode creationdisposition,
> int flags, IntPtr template);
> #endregion
>
> public void Connect()
> {
> if (!Connected)
> {
> try
> {
> IntPtr ptr = CreateFile(_port, FileAccess.ReadWrite,
> FileShare.ReadWrite, 0, FileMode.Create, 0, IntPtr.Zero);
> stream = new FileStream(ptr, FileAccess.ReadWrite);
> }
> catch (ArgumentException)
> {
> throw new InvalidOperationException("Cannot open
> communication port. Please close all other applicaions using port COM
1.");
> }
> }
> else
> throw new InvalidOperationException("Already connected");
> }
>
>
> public void Send(byte b)
> {
> stream.WriteByte(b);
>
> int response = 0;
>
> timer.Reset();
> while ((response != 32) || (!timer.Timeout))
> {
> response = stream.ReadByte();
> response = response & 0x20;
> }
> }
>
>
>
> the problem is that the stream.ReadByte call hangs and never returns
> anything. the programs hangs here, and we've been busting our heads over
> this. we used a terminal program to test the equipment we're communicating
> with and it returns 0xE0 if there's an error or 0xE1 if the command is
> understood. but our program just sits there waiting... forever
>
>
>
> can anyone please help us, we're in deeep trouble if we don't get this
> running.
>
>
> thanks,
> Luka
>
>
>
> --
> Someday I'll try again and not pretend, this time forever
> Someday I'll get it straight but not today, have you ever
> * AlliXSenoS * mailto:pebg-kcws@disposable.spamcon.org *
- Next message: Nicholas Paldino [.NET/C# MVP]: "Re: Debug option vs release .."
- Previous message: webdudeIA: "re:Use existing C code in C# application"
- In reply to: AlliXSenoS: "EMERGENCY: Problem with COM port (RS232) communication"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|