Thursday, November 20, 2008

UART-Access-in-Windowns (General)

To use the serial port in Win32 you can use the CreateFile (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/createfile.asp) function to setup a handle to the hardware. Specify the port name in place of the file name (e.g. "COM1"). Then you need to use the DCB (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/dcb_str.asp) struct. The DCB struct has members for setting up the baud rate, flow control, parity....etc. Set all of the parameters you want in the DCB struct, or have dialogs that let the user select the ones they want, and then use the SetCommState (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/setcommstate.asp) function to configure the port with the parameters in the DCb struct. You will also need to set the timeouts for the port with the SetCommTimeouts (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/setcommtimeouts.asp). Also you'll need to set buffer sizes with the SetupComm (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/setupcomm.asp) function.

Once the port is opened (briefly outlined above) use the ReadFile and WriteFile functions to read and write from and to the port. To switch individual lines on the port (RTS, DTR..etc) use EscapeCommFunction (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/escapecommfunction.asp).

That is just a quick overview but the links provided should get you started.

TDM

No comments: