سؤال

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.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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.)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top