1

Please note: Although this question mentions Java and even a specific Java library, it really has nothing to do with Java and is really more about serial communication and device drivers. As such, I think this can be answered by anybody with experience in those fields.


I've been in "Enterprise Java Land" for a while now, and am trying to make my way to the lower levels and understand the stuff closer to bare metal. I'm trying to understand serial ports and device drivers, and how they relate to each other, and I'm just not seeing the forest through the trees on a few things.

I thought long and hard about how to ask a succinct question on here that would address my main mental blockers, and I think the best way to do so is to ask using a specific example, and then try to generalize the solution(s).

Take the following example:

There is a Java library for reading/writing data to/from serial ports called JSerialComm. If I wanted my Java app to communicate directly with a serial port, I can use JSerialComm to get the job done.

Say I'm an electronics guy and I built a really impressive circuit on a breadboard. Doesn't matter what this circuit does, other than it has lots of IO components (LEDs, maybe a few motors, etc.). I want to connect this circuit to my laptop via the serial port, and then control it from my app via JSerialComm (or some comparable/alternative lib):

  • Would this circuit need to conform to RS232? Why/why not? And if so, how?
  • Would this circuit need its own native device driver? Why/why not? If yes, then why would I need a driver when I can communicate to the port through JSerialComm? And if no, then what dictates whether you can talk to an external device directly via the port (JSerialComm) vs. when you need to use a native device driver?

Again, I don't think it should matter what language we're talking about (Java, C++, Python, etc.). Thanks in advance!

2

RS-232 defines the electrical characteristics and timing of signals, the meaning of signals, and the physical size and pinout of connectors. Your circuit would clearly need to conform to that. RS-232 signaling has voltages from +3 to +15, and -3 to -15, if your components couldn't handle the +/-15 for example, trying to talk to it could release the magic smoke.

To implement RS-232, since the signaling levels are used by nearly nothing else, you would need a signal converter to convert the signals to voltage levels used by your MCU. A MAX232 is one such component that is used in roughly a gazillion devices for that purpose. For further details about how that would work, look at the MAX232 datasheet.

As for the driver, you would not need to create one, however you would have to write application software to communicate to it. The driver for the serial port on your computer instructs your OS how to talk to the specialized hardware. Your computer probably wouldn't have a MAX232, but it would have other components that fulfill similar rolls. The OS driver would provide the means to access a small send and recieve buffer, and a means to check the status of pins such as RTS and CTS. I've never used JSerialComm, but it is likely a simple wrapper around your OS's native serial port interface.

When you're at the application level, the driver exposes for the most part a stream of input and output bytes. In many ways, it can be thought of similar to network streams. You'd need to define a protocol that your device would speak, so you can send messages to turn on your motors and shut off your LEDs. You don't need an OS driver, because at that point all communication can be done through the existing serial port drivers.

| improve this answer | |
  • Thanks so much @whatsisname (+1) - a few followup questions if you don't mind. (1) Why do you say "Your circuit would clearly need to conform to that (RS232)"? Is RS232 necessary for communicating with PC componentry (e.g. making sure its voltage levels are within range, etc.)? (2) When you say "since the signaling levels are used by nearly nothing else", what do you mean by "signaling levels" and why aren't they used by anything else? – smeeb Mar 28 '17 at 4:38
  • 1
    @smeeb: (1) RS232 is not required, but it is the standard specifying the electrical characteristics of a serial port. You could also choose to connect your board over USB, in which case you need to follow that standard. The bottom line is that you need to follow one of the standards that exist for connecting external devices, or you run the risk of damaging your computer, the board or both. (2) The signalling levels are the voltage levels that represent either a 1 or a 0 in the communication. – Bart van Ingen Schenau Mar 28 '17 at 10:51
  • 1
    @smeeb: (3) The MAX232 is a hardware component that needs to live on your board and is used to normalize the voltage levels used by RS232 into levels that a MCU is designed to work with. You need an MCU on your board to interpret the bytes that the PC will be sending into meaningful actions. (4) Yes, we expect the PC and the board to speak the same protocol. On the level of RS232, you are communicating just a sequence of bytes. On top of that, your application and board also need to agree on what those bytes mean. – Bart van Ingen Schenau Mar 28 '17 at 10:54
  • 1
    @smeeb: a driver is always needed in the chain, however someone else will write one, not you. Drivers to the PCs Serial port hardware come with your OS or your motherboard driver packages. In the case of a webcam, you could implement it solely as a CBC or similar driver, however then it would be nearly useless to everyone but engineers. Video softwares expect video frames, video formats, etc, and the webcam driver is used to convert the stream of bytes coming from the device into those video abstractions used by the OS to utilize video devices. – whatsisname Mar 28 '17 at 16:44
  • 1
    So you could have a barebones driver for a webcam and only your software can view the video, and you'd have to write your own video subsystem in your client to do that, however that would be a webcam no one would want to use. – whatsisname Mar 28 '17 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.