0
votes

I am wondering that since the serial port can't be used by two programs at the same time, how my barcode reader(hardware device) is able to send data to my computer via serial port, while the port is being opened by my program, and monitored for the newly coming data.

1
The data from the bar code reader isn't going any place until you open the port with an application. The bar code reader isn't sending data any place unless the driver is doing something special. A lot of devices are using Ethernet connections which can handle multiple connections to the same device - jdweng
Are you saying that the bar code reader connected to my computer will be able to transmit data to my computer regardless whether the serial port which it is connected to is being opened by windows program? The data transmission between bar code reader and computer is low level staff, which is not restricted by windows OS that serial port is only been able to be connected by one windows program at the same time? Thanks. - CJ_
It depends on the low level driver. I agree with Hans comments, but a service can be written for a serial port that can handle multiple connections. I was just responding to your comment that the serial port was communicating without you application running before you connected. For this to happen, a low level custom driver must exist. - jdweng

1 Answers

1
votes

Sure, only one process can open the port. On the other end of it sits the device driver, it actually talks to the hardware.

That hardware is a chip called an UART, Universal Asynchronous Receiver Transmitter. It connects to the wire you use to hook up the device. Any data that comes in through the RxD pin on the connector gets converted to bytes by the UART. The device driver responds to the chip's interrupt and copies the byte(s) from the UART's receive buffer into the driver's receive buffer. Ready to be consumed by a program, it calls ReadFile() to empty the buffer. Today it is often a USB device driver that emulates a serial port.

Serial port communications are very primitive, there is no notion of a logical connection and no agreed-upon protocol to label received data to belong to a specific connection or consumer. Nothing similar to UDP or TCP, the protocols that allows a network connection to be shared. Serial ports site at the very bottom of the OSI model, the physical layer. The driver therefore does not allow more than one program to open the port. It is first-come, first serve.