Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6587301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:53:59+00:00 2026-05-25T16:53:59+00:00

I am writing a program to control a relay switch connected thru the serial

  • 0

I am writing a program to control a relay switch connected thru the serial (RS-232) COM1 port. The device I am using has contains two relay switches. These are either ‘open’ or ‘closed’.

By default, both relays are open. The number 1 relay is closed by setting bit 1 in the modem control register. Number 2 relay is closed by setting bit 0 in the modem control register.

In C, this can be achieved as follows:

x = inportb(0x3FC);
x=x & ~2; //Set bit 1 to zero
x=x | 2;        //Set bit 1 to one
x=x & ~1; //Set bit 0 to zero
x=x | 1;        //Set bit 0 to one

C++ however, does not make COM port access so easy (or so I am led to believe). I currently have the following code, which successfully opens and closes the COM port so that it can be communicated with in C++ and Windows:

#include <windows.h>

//Initialise Windows module
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance,
LPSTR lpszArgument, int nFunsterStil)

{
 //Define the serial port precedure
 HANDLE hSerial;

 //Initialise relay
 hSerial = CreateFile("COM1",GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

 //SOME CODE MUST GO HERE

 //Deactivate relay
 CloseHandle(hSerial);

 return 0;
}

However, I am at a loss as to how to ‘set bits’. I’m still very new to C++. Any help would be gratefully appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:54:00+00:00Added an answer on May 25, 2026 at 4:54 pm

    You need to understand how your device works. You cannot “set bits” directly in hSerial. Having COM port opened, you can send/receive bytes. Possibly you need to send something to device using WriteFile(hSerial, …). Maybe you need to read information from device using ReadFile(hSerial, …). COM port is not port as it was called in DOS, this is not address or register. This is input/output stream. You work with COM port like C program works with STDIN and STDOUT – read and wrire information from/to port.

    Once you understand what your device needs (this means, define communication protocol), implement it in the program. Read this article: Serial Communications http://msdn.microsoft.com/en-us/library/ff802693.aspx – everything Win32 programmer needs to know about serial communications.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a small Bluetooth-based serial control program to talk to a serial
I am writing a program which has two panes (via CSplitter ), however I
I'm writing a program (for Mac OS X, using Objective-C) and I need to
I'm writing a program that contains a generational garbage collector. There are just two
I am writing a program in VS 2005 and I wanna control the access
I'm writing a c# application which uses automation to control another program. Naturally that
I'm writing a plug-in for a program where I have a custom user control
I am writing a program to control an Iridium modem that communicates with a
I am writing a program to control a flashbulb. The flash fires in response
I'm writing a C# program that monitors a dedicated Gmail account using POP3 for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.