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!

smeeb
  • 4,820
  • 10
  • 30
  • 49

1 Answers1

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.

whatsisname
  • 27,463
  • 14
  • 73
  • 93
  • 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 04:38
  • **(3)** Can you just confirm that MAX232's (or similar components) are just hardware devices living on PCs and are only necesary for PC-to-offboard MCU interactions? I ask because in my example, my bradboard circuit *does not* have an MCU (**or is having an MCU a requirement for serial comm with a PC?**) – smeeb Mar 28 '17 at 04:38
  • And finally **(4)** When you say "*The driver for the serial port on your computer instructs your OS how to talk to the specialized hardware.*", is that because we're expecting both the PC and the breadboard to be "speaking" RS232? If not, then can you elaborate on what you mean by this? Thanks again so much for a really great and well thought out answer! – smeeb Mar 28 '17 at 04: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
  • Thanks @BartvanIngenSchenau (+1 to both!) One last followup question (if you don't mind!): I **assume** (but correct me if I'm wrong) given this setup that a device driver isn't needed here, since my app would send bytes to the serial/USB device (using the native serial/USB driver that ships with my OS), and the driver would then send those bytes out to, essentially, the MCU on the connected board. My app and any program running on that MCU could then read/write bytes to each other, and determine what those bytes mean. But if that's the case, then why do we have *any* device drivers at all?!? – smeeb Mar 28 '17 at 16:28
  • ...meaning, if I buy a new USB webcam, I need to install device drivers on my machine for it. But couldn't the GUI software (that also ships with the webcam; perhaps for configuring the webcam or seeing a thumbnail version of yourself, etc.) just be reading/writing data directly to the USB driver (on my OS/machine), and then have those commands forwarded onto the MCU running on the webcam?!? What value does a separate device driver add here? Thanks again! – smeeb Mar 28 '17 at 16:30
  • 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
  • Thanks @whatsisname (+1 for both) - say, whats a "CBC driver" (what does the acronym stand for)? – smeeb Mar 28 '17 at 16:49
  • When I said CBC i meant CDC. – whatsisname Mar 28 '17 at 16:51