473,614 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can someone help me with this serial protocol.

10 New Member
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 2131
JosAH
11,448 Recognized Expert MVP
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
kryptonite88
10 New Member
Thank you man, you are my hero.
Jul 29 '09 #3
JosAH
11,448 Recognized Expert MVP
@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
kryptonite88
10 New Member
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 Recognized Expert MVP
@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
kryptonite88
10 New Member
@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 Recognized Expert MVP
@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
kryptonite88
10 New Member
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 Recognized Expert MVP
@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

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

Similar topics

38
3512
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 = serial.Serial()
2
2922
by: Quan Nguyen | last post by:
All examples @ MS website for serial port communication are specifically indicated for RS-232 interface, such as: http://msdn.microsoft.com/netframework/programming/interop/default.aspx?pull=/msdnmag/issues/02/10/NETSerialComm/TOC.ASP http://www.microsoft.com/downloads/details.aspx?FamilyID=075318ca-e4f1-4846-912c-b4ed37a1578b&displaylang=en Would these same examples work with RS-485 port w/o change? If code modifications are...
13
4814
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 been told it is not possible to access the serial ports from asp.net. The application is used to control custom hardware. The hardware is connected to a PC through serial ports. Our customer wants to control the hardware from a remote...
5
3811
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, offset) and data to be transferred. Would anybody please tell me where I can find the Serial COM port driver, especially the driver that provides the funcation calls/APIs that allow me to call them by using my own protocol? Thanks a lot for
0
1287
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 define the message format (STRUCTURE, .XSD Schema, ...)? 2) to define field constraints and validate them? 3) to serialize into byte stream? Is there a .NET class to compute CRCs ?
3
2397
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
2253
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 spinning in a polling loop waiting for completion because it allows other threads to run sooner than they would if the system had to rely solely on expiration of a time slice to turn its attention to some other thread." I don't get the "a thread...
1
4286
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
2735
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, it returns some Mbytes and here is the problem : in the stream of bytes, I randomly losts chunks of ~100bytes.
0
8124
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8621
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8576
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8427
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5538
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4049
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1712
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.