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. 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
Thank you man, you are my hero.
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
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. 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
@JosAH
So I code the initial crc like this : -
uint crc = 0x147A;
-
crc = crc << 1;
-
crc ^= 0xFFFF;
-
Then the example they gave in 2c would be : -
uint test = 0xFEDC; // crc
-
test += GetHighLow(test.ToString("X")).high; //crc.high
-
test += 0xA9; // b
-
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: -
crc= (crc<<1)|((crc>>15)&1);
-
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
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. -
private void button1_Click(object sender, EventArgs e)
-
{
-
serialPort.Open();
-
-
byte[] versturen = new byte[11];
-
-
// Start
-
versturen[0] = 0xFE;
-
versturen[1] = 0xFE;
-
-
// Command
-
versturen[2] = 0xE0;
-
-
// 4 byte data
-
versturen[3] = 0x12;
-
versturen[4] = 0x34;
-
versturen[5] = 0x5F;
-
versturen[6] = 0xFF;
-
-
// crc
-
versturen[7] = CalcCrc(0xE0 | 0x12 | 0x34 | 0x5F | 0xFF).high;
-
versturen[8] = CalcCrc(0xE0 | 0x12 | 0x34 | 0x5F | 0xFF).low;
-
-
// End
-
versturen[9] = 0xFE;
-
versturen[10] = 0x0D;
-
-
serialPort.Write(versturen, 0, versturen.Length);
-
MessageBox.Show(ReadData().ToString());
-
serialPort.Close();
-
}
-
-
private byte Hex(string hex)
-
{
-
return byte.Parse(hex, System.Globalization.NumberStyles.HexNumber);
-
}
-
-
private HighLow CalcCrc(params byte[] list)
-
{
-
uint crc = 0x147A; // crc
-
-
foreach (byte b in list)
-
{
-
crc = (crc << 1) | ((crc >> 15) & 1); ; // crc left rotation
-
crc ^= 0xFFFF; // crc / 0xFFFF
-
-
crc += GetHighLow(crc.ToString("X")).high;
-
crc += b;
-
}
-
-
return GetHighLow(((crc.ToString("X")).Substring((crc.ToString("X").Length - 4), 4)));
-
}
-
-
private HighLow GetHighLow(string format)
-
{
-
return new HighLow(Hex(format.Substring(0, 2)),
-
Hex(format.Substring(2, 2)));
-
}
-
-
private struct HighLow
-
{
-
public HighLow(byte high, byte low)
-
{
-
this.high = high;
-
this.low = low;
-
}
-
-
public byte high;
-
public byte low;
-
}
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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()
|
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...
|
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...
|
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
|
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 ?
| |
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?
|
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...
|
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
|
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.
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |