TOP Win32API Serial Programming Win32 SerialPort



Funcs

FunctionDescription
BuildCommDCBFills a specified DCB structure with values specified in a device-control string.
BuildCommDCBAndTimeoutsTranslates a device-definition string into appropriate device-control block codes and places them into a device control block.
ClearCommBreakRestores character transmission for a specified communications device and places the transmission line in a nonbreak state.
ClearCommErrorRetrieves information about a communications error and reports the current status of a communications device.
CommConfigDialogDisplays a driver-supplied configuration dialog box.
EscapeCommFunctionDirects a specified communications device to perform an extended function.
GetCommConfigRetrieves the current configuration of a communications device.
GetCommMaskRetrieves the value of the event mask for a specified communications device.
GetCommModemStatusRetrieves modem control-register values.
GetCommPropertiesRetrieves information about the communications properties for a specified communications device.
GetCommStateRetrieves the current control settings for a specified communications device.
GetCommTimeoutsRetrieves the time-out parameters for all read and write operations on a specified communications device.
GetDefaultCommConfigRetrieves the default configuration for the specified communications device.
PurgeCommDiscards all characters from the output or input buffer of a specified communications resource.
SetCommBreakSuspends character transmission for a specified communications device and places the transmission line in a break state.
SetCommConfigSets the current configuration of a communications device.
SetCommMaskSpecifies a set of events to be monitored for a communications device.
SetCommStateConfigures a communications device according to the specifications in a device-control block.
SetCommTimeoutsSets the time-out parameters for all read and write operations on a specified communications device.
SetDefaultCommConfigSets the default configuration for a communications device.
SetupCommInitializes the communications parameters for a specified communications device.
TransmitCommCharTransmits a specified character ahead of any pending data in the output buffer of the specified communications device.
WaitCommEventWaits for an event to occur for a specified communications device.

Structures

COMMCONFIG
COMMPROP
COMMTIMEOUTS
COMSTAT
DCB
MODEMDEVCAPS
MODEMSETTINGS

COMMTIMEOUTS

typedef struct _COMMTIMEOUTS {
  DWORD ReadIntervalTimeout;
  DWORD ReadTotalTimeoutMultiplier;
  DWORD ReadTotalTimeoutConstant;
  DWORD WriteTotalTimeoutMultiplier;
  DWORD WriteTotalTimeoutConstant;
} COMMTIMEOUTS, *LPCOMMTIMEOUTS;

DCB

typedef struct _DCB {
  DWORD DCBlength; //The length of the structure, in bytes. The caller must set this member to sizeof(DCB).
  DWORD BaudRate; //CBR_9600 , CBR_9600 , CBR_115200
  DWORD fBinary  :1; //TRUE: binary mode is enabled. Windows does not support nonbinary mode transfers, so this member must be TRUE.
  DWORD fParity  :1; //TRUE: parity checking is performed and errors are reported.
  DWORD fOutxCtsFlow  :1;//TRUE: CTS signal is monitored for output flow control. If TRUE and CTS off, output is suspended.
  DWORD fOutxDsrFlow  :1;//TRUE: DSR signal is monitored for output flow control. If TRUE and DSR off, output is suspended.
  DWORD fDtrControl  :2; //DTR_CONTROL_DISABLE | DTR_CONTROL_ENABLE | DTR_CONTROL_HANDSHAKE
  DWORD fDsrSensitivity  :1;//TRUE: sensitive to the state of the DSR signal. The driver ignores any bytes received, unless the DSR modem input line is high.
  DWORD fTXContinueOnXoff  :1;//TRUE: transmission continues after the input buffer has come within XoffLim bytes of being full and the driver has transmitted the XoffChar character to stop receiving bytes. FALSE: transmission does not continue until the input buffer is within XonLim bytes of being empty and the driver has transmitted the XonChar character to resume reception.
  DWORD fOutX  :1; //TRUE: transmission stops when the XoffChar character is received and starts again when the XonChar character is received.
  DWORD fInX  :1;//TRUE: the XoffChar character is sent when the input buffer comes within XoffLim bytes of being full, and the XonChar character is sent when the input buffer comes within XonLim bytes of being empty.
  DWORD fErrorChar  :1;//TRUE and the fParity member is TRUE, replacement occurs.
  DWORD fNull  :1; //TRUE: null bytes are discarded when received.
  DWORD fRtsControl  :2;//RTS_CONTROL_DISABLE|RTS_CONTROL_ENABLE|RTS_CONTROL_HANDSHAKE|RTS_CONTROL_TOGGLE
  DWORD fAbortOnError  :1;//TRUE: the driver terminates all read and write operations with an error status if an error occurs. -> ClearCommError ()
  DWORD fDummy2  :17; // Reserved
  WORD  wReserved; //Reserved; must be zero.
  WORD  XonLim; // The minimum number of bytes in use allowed in the input buffer before flow control is activated to allow transmission by the sender. This assumes that either XON/XOFF, RTS, or DTR input flow control is specified in the fInX, fRtsControl, or fDtrControl members.
  WORD  XoffLim;//The minimum number of free bytes allowed in the input buffer before flow control is activated to inhibit the sender. Note that the sender may transmit characters after the flow control signal has been activated, so this value should never be zero. This assumes that either XON/XOFF, RTS, or DTR input flow control is specified in the fInX, fRtsControl, or fDtrControl members. The maximum number of bytes in use allowed is calculated by subtracting this value from the size, in bytes, of the input buffer.
  BYTE  ByteSize; //The number of bits in the bytes transmitted and received.
  BYTE  Parity; //EVENPARITY|MARKPARITY|NOPARITY|ODDPARITY|SPACEPARITY
  BYTE  StopBits;//ONESTOPBIT|ONE5STOPBITS|TWOSTOPBITS
  char  XonChar;//The value of the XON character for both transmission and reception.
  char  XoffChar;//The value of the XOFF character for both transmission and reception.
  char  ErrorChar;//The value of the character used to replace bytes received with a parity error.
  char  EofChar;//The value of the character used to signal the end of data.
  char  EvtChar;//The value of the character used to signal an event.
  WORD  wReserved1;//Reserved; do not use.
} DCB, *LPDCB;

sample


#include <Windows.h>

DCB Dcb = { sizeof (DCB),
            CBR_9600, TRUE, FALSE, FALSE, FALSE, // 9600baud,no parity, no cts no dsr monitoring
            DTR_CONTROL_DISABLE, FALSE, // no dtr
            TRUE, FALSE, FALSE, FALSE, FALSE, // no xonoff ...
            RTS_CONTROL_DISABLE, FALSE, 0, 0,  // no rts
            10, 10, 
            8, NOPARITY, ONESTOPBIT, '\x11', '\x13', '\xFF', '\xFF', 0 }; // 8bit, no parity,1 stopbit,

char str[100];
sprintf(str, "\\.\\COM%u", port_number); // 1,2,3, ...
HANDLE hcomm = CreateFile(str, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hcomm == INVALID_HANDLE_VALUE) {
    fprintf(stderr, "failed to open %s.\n", str);
    return EXIT_FAILURE;
}
SetCommState(hComm, &Dcb);

COMMTIMEOUTS timeouts1 = { 0, 1, 0, 1, 200}; // 200ms for write, 0 for read
SetCommTimeouts(hComm, &Commtimeouts1);

unsigned char n, s[2];
DWORD len;
do
    ReadFile(hcomm, s, 2, &len, NULL);
while (len == 2);

s[0]='a'; s[1]='\0';
WriteFile(hcomm, s, 2, &len, NULL);

管理人/副管理人のみ編集できます