COM Port Programming in Win32

The control support for the COM port under Win32 can be used to operate the COM port, such as a monitoring and control program for a single-chip microcontroller.

Below is an example code related to COM port control:

// Open COM1
hCOM=CreateFile(
  "COM1",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if (hCOM==INVALID_HANDLE_VALUE) {
  MessageBox(
    GetForegroundWindow(),"Can not open the COM port!","Operation Failed",MB_ICONINFORMATION);
  return;
}

// Configure DCB
DCB dcb;
if (!GetCommState(hCOM,&dcb)) {
  MessageBox(
    GetForegroundWindow(),"Can't read the status of the COM port!","Operation Failed",MB_ICONINFORMATION);
    hCOM=INVALID_HANDLE_VALUE;
    return;
}

dcb.BaudRate=9600;
dcb.ByteSize=8;
dcb.Parity=NOPARITY;
dcb.StopBits=ONESTOPBIT;
if (!SetCommState(hCOM,&dcb)) {
  MessageBox(
    GetForegroundWindow(),"Can't config the status of the COM port!","Operation Failed",MB_ICONINFORMATION);
  hCOM=INVALID_HANDLE_VALUE;
  return;
}

// Set COMMTIMEOUTS
COMMTIMEOUTS communication_timeout;
communication_timeout.ReadIntervalTimeout=MAXDWORD;
communication_timeout.ReadTotalTimeoutMultiplier=0;
communication_timeout.ReadTotalTimeoutConstant=0;
communication_timeout.WriteTotalTimeoutMultiplier=0;
communication_timeout.WriteTotalTimeoutConstant=0;
if (!SetCommTimeouts(hCOM,&communication_timeout)) {
  MessageBox(
    GetForegroundWindow(),"Timed out to set COM port!","Operation Failed",MB_ICONINFORMATION);
  hCOM=INVALID_HANDLE_VALUE;
  return;
}

// Write ABC to the COM port
ULONG nBytesWritten;
WriteFile(hCOM,"ABC",3,&nBytesWritten,NULL);

// Read 3 byte and put it in the str buffer
char str[3];
ULONG bytes_read_num;
ReadFile(hCOM,str,3,&bytes_read_num,NULL);

Similar Posts

  • |

    GNU glibc Manual

    “The C language provides no built-in facilities for performing such common operations as input/output, memory management, string manipulation, and the like. Instead, these facilities are defined in a standard library, which you compile and link with your programs. The GNU C library, described in this document, defines all of the library functions that are specified…

  • Alternatives to goto in bash

    As we know: There is no goto statement support in bash. goto is useful for certain situations, especially for debugging. So, is there any alternative mechanisms to goto in bash? Robert Copeland gave an interesting hacking to this problem: http://bobcopeland.com/blog/2012/10/goto-in-bash/ The idea is like the self-modifying code. But the difference is to generate a piece…

  • |

    Linux Kernel: cifs: release auth_key.response for reconnect

    This change “cifs: release auth_key.response for reconnect.” (commit f5c4ba8) in Linux kernel is authored by Shu Wang <shuwang [at] redhat.com> on Fri Sep 8 18:48:33 2017 +0800. Description of “cifs: release auth_key.response for reconnect.” The change “cifs: release auth_key.response for reconnect.” introduces changes as follows. cifs: release auth_key.response for reconnect. There is a race that…

  • How to configure ifcfg-eth0 network scripts on Fedora/RHEL Linux?

    How to configure the ifcfg-* network script files under /etc/sysconfig/network-script/ on Fedora/RHEL Linux? Following is a very typical ifcfg-eth0 file setting static IP/gateway/network mask: DEVICE=eth0 BOOTPROTO=none ONBOOT=yes NETMASK=255.255.0.0 IPADDR=192.168.1.151 GATEWAY=192.168.1.10 USERCTL=no More options are supported, here you can find a reference: Interface Configuration Files (a easier to read PDF from F15 doc). Read more: How…

  • How to force a checkpointing of metadata in HDFS?

    HDFS SecondaraNameNode log shows 2017-08-06 10:54:14,488 ERROR org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode: Exception in doCheckpoint java.io.IOException: Inconsistent checkpoint fields. LV = -63 namespaceID = 1920275013 cTime = 0 ; clusterId = CID-f38880ba-3415-4277-8abf-b5c2848b7a63 ; blockpoolId = BP-578888813-10.6.1.2-1497278556180. Expecting respectively: -63; 263120692; 0; CID-d22222fd-e28a-4b2d-bd2a-f60e1f0ad1b1; BP-622207878-10.6.1.2-1497242227638. at org.apache.hadoop.hdfs.server.namenode.CheckpointSignature.validateStorageInfo(CheckpointSignature.java:134) at org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode.doCheckpoint(SecondaryNameNode.java:531) at org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode.doWork(SecondaryNameNode.java:395) at org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode$1.run(SecondaryNameNode.java:361) at org.apache.hadoop.security.SecurityUtil.doAsLoginUserOrFatal(SecurityUtil.java:415) at org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode.run(SecondaryNameNode.java:357) It seems the checkpoint…

Leave a Reply

Your email address will not be published. Required fields are marked *