Re: EMERGENCY: Problem with COM port (RS232) communication

From: Nicholas Paldino [.NET/C# MVP] (mvp_at_spam.guard.caspershouse.com)
Date: 03/17/04


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 *


Relevant Pages

  • RE: Well, were getting closer. Still having issues installing B itTorrent.
    ... well it isn't really a bittorrent problem but rather a python one. ... I would check with the port maintainer if my ... This email communication is intended as a private communication for the sole ... Cette communication par courrier électronique est une communication privée à ...
    (freebsd-questions)
  • Re: Explain this about threads
    ... Basically I'm trying to do synchronous communication with the parallel ... I guess I'm curious at this point as to why you want to use this kernel ... the unmanaged parallel port access via CreateFile, ... If I do spinwaitthen I'm guaranteed a atleast whatever maximum time ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Highly responsive serial port
    ... My problem consists of reading from a serial COM port as fluent as possible. ... If my reader only reads the input buffer after 50ms I'm too late to reply but I don't know it. ... Would a driver(in kernel mode) have enough priority to process such requests in time? ... a general rule of thumb in thread synchronization is to stay away from depending on "sleep" and loop designs in a vain attempt to synchronize I/O. ...
    (microsoft.public.win32.programmer.kernel)
  • Serial Communication in C# Express
    ... ago and I really appreciated Nicholas Paldino's help, ... > I can only achieve to transmit '34', ... After the usual port initialization, ... // Obtain the number of bytes waiting in the port's buffer ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Telnet port 25
    ... I used IPSec to accomplish this task. ... telnet to port 25 will be closed. ... This email communication and any attachments may ... >contain confidential and privileged information for the use of the ...
    (AIX-L)