Has anyone successfully received serial communications from a COM port/RS232C device? It's easy to connect and write to a serial device with Framework 2.0, but when I try to read from the input buffer I get nothing except a timeout exception. No matter how high you set the SerialPort.ReadTimeout property it doesn't seem to matter. My code (using C# for Framework 2.0) looks something like this: use System.IO.Ports; public static void Main (string[] args) { /* Fully specified port params...portname and baud is the minimum to open port. */ SerialPort sp = new SerialPort("COM1",19200,Parity.None,8,StopBits.One); sp.Open(); sp.NewLine = "\n"; /* set the new line char for input and output so that Write/ReadLine functions correctly interpret end of data. */ sp.ReadTimeout = 2000; /* set time out to 2 seconds. */ sp.WriteLine("SOMECMD"); /* synchronous call to read incoming data from serial port. */ string incomingData = sp.ReadLine(); sp.Close(); } Only thing that seems to happen here is that ReadLine times out after waiting however long you've told it to wait in the ReadTimeout property even though I KNOW for a fact data is coming back from the serial device (verified w/HyperTerminal and another app). Two things about the short little code snippet is that 1) ReadLine should be wrapped in a try/catch as you will surely get TimeoutExceptions; and 2) the reading operation should be done via a delegate assigned to the SerialPort.ReceivedEvent event so that read/write operations are asynchronous. Unfortunately the VS05 documentation appears to be missing for this event so I don't have that code working just yet. --
Don't see what you're looking for? Try a search.
|