Solved

C# Serial Port Communication

Posted on 2009-07-09
11
3,276 Views
Last Modified: 2013-12-17
Im trying to communicate with a device via serial but am having a hard time understanding the protocol.  I am not too familiar with serial communications.  

Below is some of the protocol format:

Each protocol should follow the same format as below

SYN, SYN, STX, SA, CM, CD, XXXX, CS, ETX

Where:
SYN - Is sent in hex ($16)
STX - Is sent in hex ($02)
SA - Is the device Address sent as a hex
$07 = device #7
$1E = device #30
CM - Is the command sent as a HEX
$00  Normal (display)
$01  Display FULL
$02  Display OPEN
$03 - Display CLSD
CD - $00  Disables checksum (For one way communication)
$01  Enables Checksum
XXXX - Is the four digit number as ASCII (Note: preceding zeros must be present)
CS - Checksum is sent as HEX (see checksum routine)
ETX - Is sent in hex as ($03)

The checksum routine uses the XOR command on all significant data to ensure data
has not been corrupted. By using the following routine a checksum should be created
and transmitted with the data being sent.
SYN, SYN, STX, SA, CM,CD, XXXX, CS, ETX
Step 1: temp = SA XOR CM
Step 2 : temp = temp XOR CD
Step 2: temp = temp XOR X(1)
Step 3: temp = temp XOR X(2)
Step 4: temp = temp XOR X(3)
Step 5: CS = temp XOR X(4)
The control module will repeat this calculation and the results will be checked. If and
only if the results of the checksum match, and the sign address matches the field set
address will the data be displayed.
Example:
For sign 1 to display 0017 with a checksum being used the packet would be:
Bytes that are included in checksum calculation
$16, $16, $02, $01, $00, $01, 0, 0, 1, 7, $06, $03

If the data has been verified by the correct
unit, that sign will transmit:
SYN, SYN, STX, ACK, ETX
Where:
SYN is sent as hex ($16)
STX is sent as hex ($02)
ACK is sent as hex ($06)
ETX is sent as hex ($03)

Examples:

PACKET:
$16,$16,$02,$11,$00,$01,1234,$14,$03

PACKET:
$16,$16,$02,$11,$01,$01,0000,$10,$03



Can someone provide me with some sample C# code that will format a packet in this format to be sent to the device?
0
Comment
Question by:Alldaypilot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 300 total points
ID: 24826514
Assuming the commas are just there for display,

    private byte[] DoFormat(int sa, int cm, bool ck, int num)
    {
        byte[] packet = ck ? new byte[13] : new byte[12];
        int index = 0;
        packet[index++] = 0x16;
        packet[index++] = 0x16;
        packet[index++] = 0x2;
        packet[index++] = (byte)sa;
        packet[index++] = (byte)cm;
        packet[index++] = (byte)(ck ? 1 : 0);
        string digits = string.Format("{0:X4}", num);
        for (int i = 0; i < 4; i++)
        {
            packet[index++] = (byte)digits[i];
        }
        if (ck) {
            packet[index] = 0;
            for (int i = 3; i < index; i++) {
                packet[index] ^= packet[i];
            }
            index++;
        }
        packet[index++] = 0x3;
        return packet;
    }

0
 

Author Comment

by:Alldaypilot
ID: 24826540
All I really need with this is a code example of how to send out a serial communication command using this format and receive what the device is sending back.


0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 24826612
A sample of how a packet is actually sent is in the MSDN samples.
You can get those at

http://msdn.microsoft.com/en-us/library/c8zc5kah%28VS.100%29.aspx


Note that depending on the nature of the device you are addressing, you might need hardware to convert your serial port (not if you are talking to a normal async RS-232 port, but the nature of the packet suggests you might be addressing a syncronous or poll-select device).
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Author Comment

by:Alldaypilot
ID: 24826615
Thank you jensfiederer.  I will give it a shot and see if that works.
0
 

Author Comment

by:Alldaypilot
ID: 24827849
Would I send it to the device like this:

port.Write(packet, 0, packet.Length);


I connect to the device and the receive led lights when I click send but the message is not changing still.
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 24827972
Yes.
Do you know whether you have the baud rate, start bits, stop bits, parity settings all correct?  All of these are needed for successful async communication.

And, as I said, if your device is NOT async RS232 you might need special hardware.
0
 

Author Comment

by:Alldaypilot
ID: 24828029
This is from the documentation:

Our display unit is set up for 9600, N, 8, 1 serial communication. In
addition, each unit is field addressable for a total of 255 signs to be operated off of
one system

And here is what I am using:

  SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);



I will have to check into the asynch RS232 because it doesn't mention that anywhere.

0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 24828090
Looks good, actually.  
Any hardware flow control that could be holding you up?
0
 

Author Closing Comment

by:Alldaypilot
ID: 31601909
I still have not been able to get this device to communicate but I think it is partially at fault for poor documentation.

jensfiederer pointed me in the right direction on how this should work.
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 25002200
Thank you for coming back to the question.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question