473,320 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Can someone help me with this serial protocol.

I am making a link between 2 systems. They communicate over a serial link. I know the protocol and all the commands.
However I can’t figure out what the “dn” is supposed to mean. Is it the number of d’s?
I also don’t know what the crc.high and crc.low is all about. I know that the crc is a checksum and I know how to calculate it but I don’t know what the high and low are.

Jul 29 '09 #1
13 2111
JosAH
11,448 Expert 8TB
dn is the last byte of data; d1 is the first data byte, d2 is the second data byte and when you have n bytes of data, dn is the last data byte. crc.high and crc.lo are the hi and lo bytes of the 16 bit wide crc number (16 bits == 2 bytes).

kind regards,

Jos
Jul 29 '09 #2
Thank you man, you are my hero.
Jul 29 '09 #3
JosAH
11,448 Expert 8TB
@kryptonite88
You're welcome of course; you still have to read the specification because from that picture I can't tell whether or not the 'cmd' byte (or the leading/trailing bytes) is/are included in the crc value.

kind regards,

Jos
Jul 29 '09 #4
Yes, they are. They are calculated in way I don’t rely understand. It says I have to divide the crc by 0x147A. But is crc the sum of the command and the trailing bytes or zero? I have included a screenshot of the calculation.

I’m not really used to the level of technical programming. I’m more involved in making windows apps and web apps. Though I really like this technical stuff, it’s just hard to understand without the proper education.

Jul 30 '09 #5
JosAH
11,448 Expert 8TB
@kryptonite88
You just have to initialize the crc value to 0x147a according to that picture. Then rotate it and do the funny add as described in step 2c. Perform those steps for the cmd byte and all the d bytes.

kind regards,

Jos
Jul 30 '09 #6
@JosAH
So I code the initial crc like this :

Expand|Select|Wrap|Line Numbers
  1. uint crc = 0x147A;           
  2. crc = crc << 1;
  3. crc ^= 0xFFFF;
  4.  
Then the example they gave in 2c would be :

Expand|Select|Wrap|Line Numbers
  1. uint test = 0xFEDC; // crc
  2. test += GetHighLow(test.ToString("X")).high; //crc.high
  3. test += 0xA9; // b
  4.  
Jul 30 '09 #7
JosAH
11,448 Expert 8TB
@kryptonite88
Not really; they want to rotate the crc value; you're just shifting it to the left one bit; change the second line to:

Expand|Select|Wrap|Line Numbers
  1. crc= (crc<<1)|((crc>>15)&1);
  2.  
this code snippet shifts the crc to the left one bit and 'or's bit 15 in as the new lo bit (bit #0); this mimics a rotate operation.

kind regards,

Jos
Jul 30 '09 #8
Thanks for finding that error in my code.
I think there is still something wrong with my calculation. In the example below I’m sending a command that requires 4 bytes of data. The protocol is designed to not give a reply if there is something wrong with the crc.

I confirmed that communicating through code is working when I use the system in monitoring mode. So the cable and the communication setting are correct. It has to be something in this piece of code.


Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             serialPort.Open();
  4.  
  5.             byte[] versturen = new byte[11];
  6.  
  7.             // Start
  8.             versturen[0] = 0xFE;
  9.             versturen[1] = 0xFE;
  10.  
  11.             // Command
  12.             versturen[2] = 0xE0;
  13.  
  14.             // 4 byte data
  15.             versturen[3] = 0x12;
  16.             versturen[4] = 0x34;
  17.             versturen[5] = 0x5F;
  18.             versturen[6] = 0xFF;
  19.  
  20.             // crc
  21.             versturen[7] = CalcCrc(0xE0 | 0x12 | 0x34 | 0x5F | 0xFF).high;
  22.             versturen[8] = CalcCrc(0xE0 | 0x12 | 0x34 | 0x5F | 0xFF).low;
  23.  
  24.             // End
  25.             versturen[9] = 0xFE;
  26.             versturen[10] = 0x0D;
  27.  
  28.             serialPort.Write(versturen, 0, versturen.Length);
  29.             MessageBox.Show(ReadData().ToString());
  30.             serialPort.Close();
  31.         }
  32.  
  33. private byte Hex(string hex)
  34.         {
  35.             return byte.Parse(hex, System.Globalization.NumberStyles.HexNumber);
  36.         }
  37.  
  38.         private HighLow CalcCrc(params byte[] list)
  39.         {
  40.             uint crc = 0x147A; // crc
  41.  
  42.             foreach (byte b in list)
  43.             {
  44.                 crc = (crc << 1) | ((crc >> 15) & 1); ; // crc left rotation
  45.                 crc ^= 0xFFFF; // crc / 0xFFFF
  46.  
  47.                 crc += GetHighLow(crc.ToString("X")).high;
  48.                 crc += b;
  49.             }
  50.  
  51.             return GetHighLow(((crc.ToString("X")).Substring((crc.ToString("X").Length - 4), 4)));
  52.         }
  53.  
  54.         private HighLow GetHighLow(string format)
  55.         {
  56.             return new HighLow(Hex(format.Substring(0, 2)),
  57.                 Hex(format.Substring(2, 2)));
  58.         }
  59.  
  60.         private struct HighLow
  61.         {
  62.             public HighLow(byte high, byte low)
  63.             {
  64.                 this.high = high;
  65.                 this.low = low;
  66.             }
  67.  
  68.             public byte high;
  69.             public byte low;
  70.         }
  71.  
Jul 30 '09 #9
JosAH
11,448 Expert 8TB
@kryptonite88
What do these lines do? If this is Java or C# the parameter (a single one!) certainly doesn't result in an array of bytes; the | operator bitwise-or's its operands which most certainly is not what you want.

kind regards,

Jos
Jul 30 '09 #10
@JosAH
Changed it to :
Expand|Select|Wrap|Line Numbers
  1. // crc
  2.             versturen[7] = CalcCrc(new byte[] {0xE0, 0x12, 0x34, 0x5F, 0xFF}).high;
  3.             versturen[8] = CalcCrc(new byte[] {0xE0, 0x12, 0x34, 0x5F, 0xFF}).low;
But still no result.
Jul 30 '09 #11
JosAH
11,448 Expert 8TB
@kryptonite88
Next I don't understand what your line #47 does:

Expand|Select|Wrap|Line Numbers
  1.  crc += GetHighLow(crc.ToString("X")).high;
  2.  
Better change that to:

Expand|Select|Wrap|Line Numbers
  1. crc+= (crc>>8)&0xff;
  2.  
That adds the high byte of the crc to the crc value itself, just as your documentation says so.

kind regards,

Jos
Jul 30 '09 #12
It finally worked. I found out that my ReadData() method had a check for bytes in the buffer which failed and told me the buffer was empty. If is just start reading I receive a reply.

I thank you very much Jos. I could not have begun to crack this without your help.

BTW. Can I assume that if
Expand|Select|Wrap|Line Numbers
  1. (crc>>8)&0xff
get me the high,
Expand|Select|Wrap|Line Numbers
  1. (crc<<8)&0xff 
gives me the low?
Jul 30 '09 #13
JosAH
11,448 Expert 8TB
@kryptonite88
Nope, the first expression shifts the crc value 8 bits to the right and masks everything away except for the lower 8 bits (that's the value 0xff). So if you want to keep just the lo byte there's no need to shift anything, just mask everything except for the lowest eight bits away; like this:

Expand|Select|Wrap|Line Numbers
  1. int lo= crc&0xff;
  2.  
kind regards,

Jos
Jul 30 '09 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

38
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser =...
2
by: Quan Nguyen | last post by:
All examples @ MS website for serial port communication are specifically indicated for RS-232 interface, such as: ...
13
by: Al the programmer | last post by:
I need to access the serial ports on my webserver from an asp.net page. I have no problem accessing the serial ports from a windows form application, but the code doesn't work in asp.net. I have...
5
by: Fiesta | last post by:
Hi All, I am working on a project regarding the serial COM port. I have to design my own protocol for the serial communication. In the protocol there are some bits for Read/Write, address(bank,...
0
by: herbert | last post by:
I have to develop a serial protocol to communicate with a device. The messages contain fields of bit groups, hex numbers, ascii, floating point numbers. in VB.NET, what is the best way 1) to...
3
by: Sundar | last post by:
how d you classify a serial com port application as a asynchronous or a synchronous operation...Meaning if i hve a handshakeing protocol t b implemented then is it async or sync?
18
by: Jon Slaughter | last post by:
"Instead of just waiting for its time slice to expire, a thread can block each time it initiates a time-consuming activity in another thread until the activity finishes. This is better than...
1
by: RaymondHe | last post by:
I want to transfer files over serial,but PySerial Module does not support any protocol such as xmodem/ymodem,and I find nothing about these protocol write in python What should I do?Thank you
4
by: Xavier | last post by:
Hi, I try to access to a Bluetooth GPS data-logger with Python. I use pySerial. Sending and receiving little messages (~100 char) works fine. However, when I ask the GPS to dump the trails,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.