I have always had this question, but I couldn't quite find anything that answered it. I was hoping someone could shed some light on the question...

Thank you in advance.

This is hardware dependent. Look at your PC. There are connectors on it. USB ports, maybe a parallel port and a serial port. Using C++, you can ask the operating system to set voltage values on those connectors. Some operating systems will only allow you to set it to low (usually zero volts) or high (some other value, 5V comes to mind, but it's hardware dependent). If the operating system is happy to, it will then change the voltage values on those connectors. If you have connected some other hardware to that connector, you can use those voltage changes to communicate. This is what happens when you plug something into a USB socket. You can also ask the operating system to read the voltage at those connectors.

Not every operating system will give you this functionality. You can also buy specialised hardware connectors to go inside a PC that come with libraries; you use the library in your C++ code to control voltage values at connetors on that hardware.

There is also embedded software; this is for hardware of a different sort. Not like a PC. Dedicated hardware for doing things, like a wristwátch or an intelligent smoke alarm or a dishwasher. We call this "embedded software", and usually it does not have an operating system that you ask to do things for you. The compiler that turns the C++ (or more commonly, C code) into a program you can put onto the hardware will contain specific means of controlling the voltages at connectors, and you read the manual to find what memory locations you must write to or read from to do this, or what special functions are provided to do this.

Edited 3 Years Ago by Moschops

Comments
I like that explanation

Short answer, you don't. Get yourself an arduino and program it using that (it happens to be a b.astardized bit of c++ and java anyway so it's fairly easy).

Edited 3 Years Ago by iamthwee

There is the circuitry of the computer, external circuitry of other devices, and just other (simple) external circuitry, which if you want to control you need some kind of connectivity, such as RS-232, RS-485, etc. How you "talk" to those devices and circuitry depends upon the operating system, and a lot of other stuff. Most of the time, people use C to access these functions, to set or read voltages on various output ports or other stuff, but thankfully C code can be used in C++ - consider C++ as C with classes... :-)

Thanks to everyone who replied. I understand what you guys are saying. i really want to look into this kind of thing. If you guys have any books, textbooks, articles, websites or anything else, please tell me. Thank you.

Generally, you connect C++ code to circuitry by storing it inside a memory. Then some circuitry will read that code line by line and do what it says. A microcontroller is like that, it's a computer on a chip and will be under the control of the code stored in it's memory. A microcontroller can cost just a few dollars. To load C++ code into a micro controller, you do the following:

Write the C++ code on a PC using a compiler for the microcontroller you are using.
The compiler will compile the C++ code into what is called a Hex file. This Hex file
will be sent to a programmer (about $18.00) which connects to the USB port on your computer.
A cable from the programmer connects to a few of the pins on the microcontroller.

You then use some programming software (like Avrdude) to take the hex file and run it through the programmer to the microcontroller. This "burns" the code into the microcontroller's memory. When the microcontroller is turned on, it will start obeying the code in it's memory.

Programming a microcontroller is not really hard, you just have to understand a little bit of electronics, and then control the pins on the micrcontroller the way you want via your code.

Edited 3 Years Ago by MandrewP

I agree with AW - arduino boards/devices are a great way to learn about interacting with hardware from your PC. My grandson does some awesome stuff with them, using his Linux laptop to control them. He uses them for control systems on his RPV aircraft and 'copters.

Using C++, you can ask the operating system to set voltage values on those connectors. Some operating systems will only allow you to set it to low (usually zero volts) or high (some other value, 5V comes to mind, but it's hardware dependent). If the operating system is happy to, it will then change the voltage values on those connectors. If you have connected some other hardware to that connector, you can use those voltage changes to communicate. This is what happens when you plug something into a USB socket. You can also ask the operating system to read the voltage at those connectors.

That is a rather unfortunate explanation. I understand what you meant to explain, but the chosen example makes the explanation completely wrong. Most things like USB ports, serial ports, or any other kind of port, have many layers of circuitry (some just plain old circuits, others being programmable logic units) before it gets anywhere near the main "PC" (i.e., the CPU instructions), never mind the operating system. Usually, for a thing like a USB port, you'll have a port controller that listens to incoming data and can output data with the USB protocol, this will be a simple programmable logic unit (a very tiny micro-controller), most often a PIC (Peripheral Interface Controller). Then, there can be a number of additional steps, such as multiplexing, driving a bus (e.g., PCI / PCIe), and so on, most of which are achieved with a combination of PICs and simple digital circuits. Basically, if you open a computer and look at what's in there, you'll see lots of "cards" (the mother-board, the graphics card, some PCI boards, etc...) called printed circuit boards (PCBs), and most of the stuff on those cards are PICs or simpler integrated circuits (ICs) which do most of the "setting voltages" work.

Even in the case of simple micro-controllers and much simpler types of ports (like RS-232 / UART / TTL, or I2C), the "setting the voltages" part is done with "dumb" circuitry that is integrated inside the micro-controller chip. The reason for this is generally that these communication protocols require precise timing according to a hardware clock (oscillator) and/or hardware interrupts (a thing that detects a voltage change on a wire and trigger some action to be taken immediately). Usually, what a C/C++ program might do is set a byte (8 bits) value in a register (small reserved slot of memory) reserved for "outgoing data" and then set a bit in another register to tell the circuitry to "send whatever is on the outgoing data register" (so it will be sent at the next opportunity to do so, depending on the port's protocol and clock), and then you repeat for the next byte you want to send. Receiving data is similar, with usually a register reserved for the "last received byte", and an interrupt to signal the new data has arrived. Of course, you can write a port controller using C/C++ on a micro-controller via its GPIOs (General Purpose I/O pins) and its programmable interrupts, but people rarely do this because you generally try to get a micro-controller that already has the ports (and their controllers) that you need (so that you don't have to use GPIOs for that purpose).

If you have an operating system on top of this, then the OS generally hides away all this trouble of "tweeking registers" to communicating things on ports (or system buses), and generally abstract all of that in the form of files (i.e., write to the "file" to send data on port, and read from the "file" to get data from port) or other kernel calls. Most operating systems don't allow or give much access for user-space code to actually tweek with reserved registers, GPIOs or hardware interrupts, because that would seriously mess up its operation.

how do you connect c++ code to for example a piece of circuitry?

I think the question is just too vague. C/C++ code is compiled into a sequence of CPU instructions which can be loaded into some memory and executed. It works the same for any target platform: you write the code, you compile it, load it in memory, and execute. For different platforms, the process will be different in practice, but not in principle. So, if you ask how does executable code "communicate" with some external circuitry, then that is basically what I just explained about the port communications. It is mostly a matter of setting register values and responding to external triggers (interrupts). Some stuff can be done with GPIOs for special purposes (more common in embedded software, like flashing some LED light or similar things on small devices). And then, there are tons of "complicated" peripheral types (ethernet, graphics cards, analog converters, etc...), but they are mostly just controlled the same way (PCI / PCIe buses or similar things).

I guess the thing to understand is that the more complicated the device (from an alarm clock to a super-computer), the more layers there are. Each layer is more abstract but more "capable" then the last. A serial port controller is not able to do anything else but spit out one byte on the port, and repeat, but it is very "concrete", as in, it directly sets the voltages on the wires to the correct values in the correct timing such that the thing at the other end will understand it. But to do this, you only need a simple PIC unit, nothing more. C/C++ code is generally limited to the real of things that are at least as big as a micro-controller, and even then, you are already quite a bit removed from the absolute low-level (e.g. setting voltages) except if you are playing around with the GPIOs.

This question has already been answered. Start a new discussion instead.