Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I implement a serial communication protocol in a object-oriented form?

I’m working on with an embedded device that is connected to PC using RS232.

I need to do a software to communicate with this embedded device.

I program in Delphi. I never used to use object-oriented in the past. But I'm trying to change this.

I am not able to think in an object oriented manner to solve this problem.

I have this protocol:

<STX><STX><COMMAND>[<DATA><DATA>...]<CHKSUM><ETX>

where:

<STX> is the Start of TeXt (0x55);
<COMMAND> can be 0x01 for read, 0x02 for write, etc;
<DATA> is any value;
<CHKSUM> is the checksum;
<ETX> is the End of TeXt (0x04).

The software computer will send a command via serial, and the device will answer, using the same protocol.

For example:

Reset command
PC sends     : <STX><STX><0x09><0x00><CHKSUM><ETX>
Device answer: <STX><STX><0x09><0x00><CHKSUM><ETX>

Get Version
PC sends     : <STX><STX><0x00><0x02><CHKSUM><ETX>
Device answer: <STX><STX><0x00><0x00><VER_L><VER_H><CHKSUM><ETX>

I have to send a file stream to the device.

I'd like to obtain suggestions and/or examples of the best way to implement this in an object oriented manner. I'd like to be able to do unit-test too.

Thanks

like image 324
Daniel Grillo Avatar asked Nov 30 '10 12:11

Daniel Grillo


People also ask

Which protocol is used for serial data communication?

The most common standard used for serial data transmission is called RS232C. It was set by the Electronics Industry Association and includes an assignment of the conductors in a 25-pin connector. It has also been used widely for data transfer over a modem.

What are the two methods used in serial data communication?

Serial transmission has two classifications: asynchronous and synchronous. Data bits can be sent at any point in time. Stop bits and start bits are used between data bytes to synchronize the transmitter and receiver and to ensure that the data is transmitted correctly.

What are the three protocols of serial communication?

The synchronous type of serial protocols such as SPI, I2C, CAN and LIN are used in different projects because it is one of the best resources for onboard peripherals. Also these are the widely used protocols in major applications.

How do you set up serial communication?

To configure the Serial Port for your device, on your computer go to Control Panel - Device Manager, select “High-Speed USB Serial Port (Com X)”, right click and select Properties. Click the Features tab. This tab is used to change the COM port number and configure the port.


1 Answers

You should look at other serial send/receive communication models, such as HTTP. In .NET, the HTTPWebRequest object is where you gather together all the pieces of info to be sent across the wire - including command (HTTP METHOD: GET, PUT, POST, etc) and byte stream. The HTTPWebRequest object (and the HTTP stack) internally deals with the "paperwork" of calculating checksums of the data, chunking large data into smaller packets, etc. All your code has to do is construct the request object, set the command, assign a stream of data to the request object's property, and send.

Another reason why you should look at existing communication object models like .NET HTTP is that serial comms are generally asynchronous from the perspective of your host CPU. A lot of CPU time can pass by while transmitting the characters of the request on the serial port, and while waiting for the response. Use an async model for your request/response so that you don't block the calling thread and potentially freeze your UI.

To continue the .NET HTTP example, HTTPWebRequest has a GetResponse method which sends the request and will block the calling thread until a response is received. HTTPWebRequest also has a BeginGetResponse()/EndGetResponse() pair so that you can send the request and provide a callback to be executed when the response arrives at some later time.

Even if your immediate design is ok with a thread-blocking synchronous call model, you should at least investigate asynchronous coding patterns and consider implementing your object as such. You can always call an asynchronous method in a thread-blocking synchronous way, but it's much more difficult to call a synchronous method in an asynchronous way. Invest a little time now to give yourself more options down the road.

like image 170
dthorpe Avatar answered Oct 17 '22 06:10

dthorpe