1

I recently stumbled across the Java Communication API, which is a javax package meant for "serial communication" in pure Java. I have heard of serial communication many times before, but I guess I don't understand what "serial communication" exactly means, or what it implies.

According to Wikipedia:

In telecommunication and computer science, serial communication is the process of sending data one bit at a time, sequentially, over a communication channel or computer bus.

OK...so the Java Communication API allows me to read/write data one bit at a time. But what does that even mean?!? Can't I just do that on my own (pseudo-code here)?:

String str = "I want to send this bit by bit.";
byte[] strAsBytes = str.getBytes();
byte[] bits = new byte[strAsBytes.length * 8]; // For each byte we need 8 bits.
for(int i = 0; i < strAsBytes.length; i++) {
    for(int j = 0; j < 8; j++) {
        // Something like this (again this is just pseudo-code).
        // Regardless, populate all elements in the bits array with the
        // bit in the position of the current byte.
        bits[i*j + j] = getBit(strAsBytes[i], j);
    }
}

// Sends the array of bits over a network, maybe using Netty and TCP,
// or something. Sends each bit one by one.
networkManager.sendBitByBit(bits);

// Retrieves the value of a bit at any position in a byte (theByte).
public byte getBit(byte theByte, int position) {
    return (theByte) & (0x01 << pos) ;
}

I feel like the term "serial communication" implies something more than just "sending one bit at a time". Does it mean that I can use it to read/write from serial ports on a machine? Something else?!?

I guess I'm looking for a plain-English, layman explanation of what the broader term "serial communication" is and what types of things you can do with it, and then put that into context of the Java Communications API. It would also be great to have an example or two of what you might use the API to accomplish.

dsolimano
  • 8,870
  • 3
  • 48
  • 63
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756

2 Answers2

2

In this case, serial refers to the serial port, which is a (largely outdated) connector that allows for serial communication (which, in fact, implies not much more than a protocol that sends one bit at a time, through one dedicated wire). That stands in contrast to the parallel port which has multiple wires for sending and receiving. Don't let the amount of pins confuse you. Only two of the serial wires are used for data transfer, each in one direction (one for send and one for receive), as laid out in this pin diagram.

The Java Communications API provides ways for communicating with the underlying system's serial but also parallel ports, as you can quickly see from this documentation page. Since most modern computers don't have these ports anymore, this API might only be useful when running on certain embedded devices that still use these ports for external connections.

This HowStuffWorks article explains most asepcts of the Serial Port in layman's terms.

Let me further disambiguate "serial port" and "serial protocol". The serial port uses a serial protocol, but so do USB and Firewire. They also only have one transfer wire per direction (send and receive), but their protocol and technology allows them to transfer data much faster. Parallel protocols, in contrast, need more power and, as of 2013, there are few (I can't think of any, contributions welcome) that can match the speed of serial protocols, due to synchronization overhead.

Domi
  • 22,151
  • 15
  • 92
  • 122
  • Please note that an USB Port is a serial port as well. – TwoThe Dec 01 '13 at 12:21
  • @TwoThe Any citations on that? – Domi Dec 01 '13 at 12:24
  • Thanks @Domi (+1) - that makes a lot of sense. However I'm still not quite seeing the "*forest through the trees*" yet. A few followup questions: (1) can I assume that Ethernet (CAT5, etc.) is **not** serial, and that the main difference between Serial and Ethernet is that Ethernet requires the use of protocols (and hence, knowledge of all bytes to send/receive upfront)? And (2) can you give me a real-life use case of using the "JavaComm" API? Perhaps I would use it to communicate with a hardware device physically connected to my laptop's USB port or something? Thanks again! – IAmYourFaja Dec 01 '13 at 12:30
  • @Domi: Wikipedia. ;) And maybe the fact that Windows puts them under "Serial Connections". And experience, but that's not quotable. – TwoThe Dec 01 '13 at 12:32
  • @TwoThe it has a serial protocol, it is not a serial port :) I added some info on that in my answer. – Domi Dec 01 '13 at 12:41
  • It is a serial port using a serial protocol, the difference is RS232 vs USB. – TwoThe Dec 01 '13 at 12:46
  • 1
    @TicketMonster As I said, "The Java Communications API provides ways for communicating with the underlying system's serial but also parallel ports". These ports have very specific protocols and you can only use that API to communicate with them, not with other ports that use a serial or parallel protocol. So, you can't use it for ethernet or USB. You will need something else for that. I explained the use-case of the API a bit further in my answer. – Domi Dec 01 '13 at 12:47
  • I'm going to add something that I think has been sort-of stated but might be confusing -- "serial communication", as Domi has said, usually refers to communication over a serial port, and that is what this API enables you to do. There are a number of protocols that are used over serial ports; the communication either allows you to choose one for which it provides methods or forces you to implement a protocol on your own. But serial communication is a more general method of data transfer, used by a lot of things besides serial port. The API you're looking at doesn't handle those. – arcy Dec 01 '13 at 12:53
  • @rcook note that the last paragraph already explains "serial protocols", but I see your point. It should explicitly state that such protocols only use one wire for data transfer (possibly per direction). I updated it. – Domi Dec 01 '13 at 12:54
  • Thanks again @Domi (+1) - but I'm still looking for an actual use case of the JavaComm API. I've read, and re-read your answer several times now, and still not seeing one. Can you (or someone else) provide a real-life use case for using this API, **beyond** the description that it is used for communicating with the system's serial/parallel ports? Thanks again! – IAmYourFaja Dec 01 '13 at 13:03
  • 1
    @TicketMonster Did you read the web page you linked? "developing platform-independent communications applications for technologies such as Smart Cards, embedded systems, and point-of-sale devices, financial services devices, fax, modems, display terminals, and robotic equipment." Old versions of those devices all use the serial or parallel port. Does that explain it? You do understand that the port is something on your computer that lets you plug other things into it, and effectively, lets your computer communicate with those things? – Domi Dec 01 '13 at 13:06
  • Ahhh I think I get it now @Domi (+1), so serial/parallel ports are for communicating older/legacy devices. That would explain why the JavaComm API doesn't have a big following/usage in 2013, because everything is Ethernet-based now, right? Is that a fair assessment? Thanks again! – IAmYourFaja Dec 01 '13 at 13:09
  • 1
    @TicketMonster most devices use USB. Ethernet is a standard that allows computers and other devices to be connected in a network. It's heavily used in data centers, offices and some other places (and I personally use it at home), but I would guess that most homes use wireless these days. The modem in your home is connected to the backbone using yet other kinds of technology. – Domi Dec 01 '13 at 13:12
  • Thanks again @Domi (+1) - last question, just to confirm: the JavaComm API **can** or **can't** be used for USB communication? – IAmYourFaja Dec 01 '13 at 13:15
  • @TicketMonster As I said before, you can't. [This thread](http://stackoverflow.com/questions/862880/java-usb-library) discusses Java USB libraries. – Domi Dec 01 '13 at 13:18
  • re: Parallel ports: the reason that they're not used anymore is not so much because of a lack of speed or synchronization, the problem is that at the electrical level you get a lot of crosstalk between the wires. This makes it harder transfer data, as it's a lot easier to get data corruption. – rm5248 Dec 03 '13 at 01:01
  • *"the serial port...is a (largely outdated) connector"* -- That's a PC-centric comment that is false if you were involved with industrial and/or embedded computing. SoCs that can run Linux have one or as many as seven serial ports (USART/UART devices). – sawdust Jun 01 '14 at 02:47
  • @sawdust What kind of serial connector do those have? You sure it's one of those that Java's comm API supports? – Domi Jun 01 '14 at 03:01
  • "*What kind of serial connector do those have"* -- Any kind of connector you want to wire up to the line-driver/transceiver and U[S]ART. Sometimes the USART is wired up directly to another device on the board (e.g. XBee radio module or Digi Ethernet module) at TTL (logic not RS-232) voltages. *"You sure it's one of those that Java's comm API supports?"* -- I'm thinking of ARM SoCs, so if there's an ARM port, then yes. That serial API is for USART/UART devices. "Serial" port and USART/UART are synonymous (unless you're using "bit-banging"). – sawdust Jun 01 '14 at 03:38
1

A description of serial communication that shows the difference from other communication is that serial communication can start sending data without knowing all the data to be sent, or even how much data there will be.

In your example you already have all the data that is going to be sent, so you don't need to use serial communication to send it.

Serial communication allows you to start sending data where part of the data doesn't exist yet, for example if you send data as you type it in, or where the data is too large to handle all at once.

The Java Communications API handles serial commincation over the serial port and the parallel port. (Yes, the parallel port does serial communcation. Eventhough it sends several bits at a time, the data stream is serial.)

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Thanks @Guffa (+1) - please see my followup questions underneath Domi's answer - I have the same questions for you! Thanks again! – IAmYourFaja Dec 01 '13 at 12:30
  • @TicketMonster: Ethernet is serial at the physical level, but then transfer protocols are used on top of that to send packages of information. Interrestingly enough, a series of packages form a stream, which can actually be serial in the sence that you don't need to specify the total size of the stream beforehand, that's for example how a HTTP response without a `Content-Length` header works. The Java Communications API only handles communication over the serial and parallel ports though, for example direct output to an old (pre-USB) printer. – Guffa Dec 01 '13 at 13:26
  • "*parallel port does serial communcation ... the data stream is serial"* -- That's a misuse of "serial". You are equating "serial" with "sequential", but in the context of communications they are not synonymous. – sawdust Jun 01 '14 at 02:51
  • @sawdust: Yes, you have a point there, but "sequential communication" isn't a term that has any recognisable meaning here. – Guffa Jun 01 '14 at 09:01