Windows Serial Port Programming Example

  1. Windows Serial Port Programming Example List
  2. Serial Port Communication
  1. Nov 14, 2017  This site uses cookies for analytics, personalized content and ads. By continuing to browse this site, you agree to this use.
  2. #Serial Port Programming using Win32 API and C language This repo contains code for transmitting and receiving characters (Strings) serially between an x86 Windows PC and a Microcontroller (MSP430G2553 on Launchpad).The code is written in C and uses Win32 API calls to control the SerialPort on a Windows machine.
  3. Port (if the serial port device gets disconnected or turned o, for example) then attempting to read from the port can cause your application to hang while waiting for data to show up. There are two ways of going about xing this. First, you can use multithreading in your application with one thread dealing with the serial port stu and the other doing the actual processing. This can get very messy and complex and really isn’t necessary.
20 Oct 2002CPOL
The purpose of this article is to describe how to interface to serial port on Win32.

Windows Serial Port Programming Robertson Bayer March 30, 2008 Intro This paper covers the basics of opening, reading from, writing to, and managing serial port communications using the Windows API. The overall goal here is to give you a basic overview of how serial communications programming works and to at least get you started along the.

Introduction

The purpose of this article is to describe how to interface to serial port on Win32. The serial port can be implemented by several techniques such as ActiveX, access I/O and file operation. This article explains the use of serial port on Win32 platform by file operation technique. The programmer can use kernel32.lib library that is provided with the Microsoft Visual C++ Version 6.0. In Microsoft Windows (2000, Me, XP and 95/98), serial port can be treated as a file. Therefore it's possible to open a serial port by using Windows file-creating function.

This article explains not only about serial port communication but also how to implement multi-tasking that can apply with our project 'serial port' application. The reason why the software (serial communication) will be implemented with multi-tasking method is that the serial communication application has to handle work with more than one task at the same time. For example: the portName is a variable that is declared by const char*. It is used to specify port name that wants to create a serial port handle.

Restoring a configuration

The restoration of serial port configuration is getting current configuration at control device. The configuration of serial port includes parameters that are used for setting a serial communications device.

The GetCommState function is used to get the current device-control and then fills to a device-control block (a DBC structure) with the current control settings for a specified communications device. The following code shows the function that is used to get the current control device:

Modifying a configuration

When you already have serial port configuration in the DBC format, you have to modify parameters a bit. Following code shows the parameters modified:

  • DWORD BaudRate:

    Current baud rate (default = 9600)

  • BYTE StopBits:

    0,1,2 = 1, 1.5, 2 (default = 0)

  • BYTE Parity:

    0-4= no, odd, even, mark, space (default = 0)

  • BYTE ByteSize:

    Number of bits/byte, 4-8 (default = 8)

Note: Recommend that programmers use default value for typical communication. As shown in figure 3, Watch Dialog Box shows the default values that are used for typical communication.

Figure 3: Serial port configuration
Windows Serial Port Programming Example

Storing a configuration

The next step is the storage of new configuration that is modified already into device control. Call SetCommState API function to store the configuration. The SetCommState function configures a communications device according to the specifications in a device-control block (a DBC structure). The function reinitializes all hardware and control settings, but it does not empty output or input queues. Following code shows storage of a new configuration:

Setting a Time-Out communication

The final step in serial port opening is setting communication Time-out by using the COMMTIMEOUTS data-structure and calling SetCommTimeouts function. The code below shows setting time-out of communication:

ReadIntervalTimeout

Specifies the maximum time, in milliseconds, allowed to elapse between the arrival of two characters on the communications line. During a ReadFile operation, the time period begins when the first character is received. If the interval between the arrival of any two characters exceeds this amount, the ReadFile operation is completed and any buffered data is returned. A value of zero indicates that interval time-outs are not used.

A value of MAXDWORD, combined with zero values for both the ReadTotalTimeoutConstant and ReadTotalTimeoutMultiplier members, specifies that the read operation is to return immediately with the characters that have already been received, even if no characters have been received.

ReadTotalTimeoutMultiplier

Specifies the multiplier, in milliseconds, used to calculate the total time-out period for read operations. For each read operation, this value is multiplied by the requested number of bytes to be read.

ReadTotalTimeoutConstant

Specifies the constant, in milliseconds, used to calculate the total time-out period for read operations. For each read operation, this value is added to the product of the ReadTotalTimeoutMultiplier member and the requested number of bytes.

A value of zero for both the ReadTotalTimeoutMultiplier and ReadTotalTimeoutConstant members indicates that total time-outs are not used for read operations.

WriteTotalTimeoutMultiplier

Specifies the multiplier, in milliseconds, used to calculate the total time-out period for write operations. For each write operation, this value is multiplied by the number of bytes to be written.

WriteTotalTimeoutConstant

Specifies the constant, in milliseconds, used to calculate the total time-out period for write operations. For each write operation, this value is added to the product of the WriteTotalTimeoutMultiplier member and the number of bytes to be written.

A value of zero for both the WriteTotalTimeoutMultiplier and WriteTotalTimeoutConstant members indicates that total time-outs are not used for write operations.

Note: After the user has set the time-out of communication without any error, the serial port has opened already.

Sending data

Most of data transmission of serial port is done as writing a file. Programmer can apply file operation functions for sending data to serial port. The WriteFile function is a function used to send data in serial port communication.

Note: If the function succeeds, the return value is nonzero.

Receiving data

Most of data reception of serial communication is done as reading a file. Programmer can apply file operation functions for receiving data from serial port. The ReadFile function is the function that handles reading data in serial port communication.

Windows Serial Port Programming Example

Note: If the function succeeds, the return value is nonzero.

Closing a serial port

The serial port closing calls the CloseHandle API function to close handle of device control.

Windows Serial Port Programming Example List

Note: If the function succeeds, the return value is nonzero.

#Serial Port Programming using Win32 API and C language

This repo contains code for transmitting and receiving characters (Strings) serially between an x86 Windows PC and a Microcontroller (MSP430G2553 on Launchpad).The code is written in C and uses Win32 API calls to control the SerialPort on a Windows machine.

--------------------------------------------------------------------------------------------------------------------------------------##Details

Full code explanation along with screenshots can be found here on the xanthium website

Serial Port Communication

  • The Microcontroller and PC are connected in null modem configuration using 3 signals (TX,RX and Ground).

  • The code uses standard Win32 API's to intialize the PC serial port and transmit a character to the microcontroller board.

  • The PC side code is written in C using Win32 API

  • and can be compiled using GCC or Microsoft Visual Studio Express.

  • The Code will Work With Standard RS232 Serial ports or any USB to Serial Converter.

More info about the USB to Serial/RS232/RS485 Converter used in the above tutorial can be found here

  • The Microcontroller side code is written in Embedded C and Compiled using IAR embedded Workbench for MSP430.

  • The Hardware used is MSP430G2553 on Launch pad development board.

    Then install the current version of Java. Fonality hud download client. Install the HUD and allow it to install the Java version it wants (I believe it is something like 6.11).

##Repo Contents

  • USB2SERIAL_Read

    • Contains Code for Reading data from the serial port of Windows Machine .
    • A string is transmitted by MSP430 Microcontroller which is received by the PC Serialport and then displayed on the Console.
  • USB2SERIAL_Write

    • Contains Code for Transmitting data from the serial port of Windows Machine .
    • A character is transmitted by the Windows PC towards the MSP430 Microcontroller.The character is received by the MSP430 and an LED on the development board is turned ON to signify data reception.