c# 2 byte calculation checksum - c#

I need help with a checksum calculation.
This is (not my code!) but from specification
http://www.leupamed.at/?wpdmact=process&did=NC5ob3RsaW5r
private void CalcCheckSum(string msg, out byte checksum1, out byte checksum2)
{
byte cs1 = 0;
byte cs2 = 0;
// Always use "\n" as line break when calculating the checksum.
msg = msg.Replace("\r\n", "\n"); // Find and replace CR LF with LF
msg = msg.Replace("\r", "\n"); // Find and replace CR with LF.
for (int i = 0; i < msg.Length; i++)
{
cs1 += (byte) msg[i];
cs2 += cs1;
}
checksum1 = cs1;
checksum2 = cs2;
}
I must create a packet like this:
<!--:Begin:Chksum:1:--><!--:Ack:Msg:3:0:--><!--:End:Chksum:1:184:62:-->
The string <!--:Ack:Msg:3:0:--> is the actual data, I must calculate two checksum bytes (184 and 62) and insert these into the final packet (as seen above).
But my result from the calculation is 10 and 62
var msg = "<!--:Ack:Msg:3:0:-->";
byte checksum1 = 0;
byte checksum2 = 0;
CalcCheckSum(msg, out checksum1, out checksum2);
I don't now how to calculate correct checksum values.
This is checksum for response. Not for validating request.
I can't upload image due to low reputation, so look at last line in specification: https://drive.google.com/file/d/0B_Gs9q9SJteadVRwSVc1a2FmUTg/edit?usp=sharing
This acknowledge message is independent on request. Only it must be response to request message ID 3.
Solution?
After calculating checksum:
checksum1 = 256 - (10 + 62) = 184
checksum2 = 62
Device communicating without problem, now.

After calculating checksum:
checksum1 = 256 - (10 + 62) = 184
checksum2 = 62
Probably this question is too specific and no one has experience with this type of checksum calculation.
Device communicating without problem, now.

Related

Send text as binary data from socket

I have a device emulator which accept the data as text from socket.Below code works fine until I send from 0x00 to 0x7F means upto Ascii limit (0 to 127).
Issue arise when I try to send beyond the Ascii limit like 0x80,0x81. It send to emulator 0x3F('?') and it change the whole meaning of command. because it does not able to understand this.
So What may be the possible solution to send the data beyond Ascii limit
Send data code:
//string data = textBox1.Text;
string d1 = ConvertHex("35"); //getting exact byte in socket
byte[] buffer = Encoding.ASCII.GetBytes(d1);
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
ConverHex function:
public static string ConvertHex(String hexString)
{
try
{
string ascii = string.Empty;
for (int i = 0; i < hexString.Length; i += 2)
{
String hs = string.Empty;
hs = hexString.Substring(i, 2);
uint decval = System.Convert.ToUInt32(hs, 16);
char character = System.Convert.ToChar(decval);
ascii += character;
}
return ascii;
}
catch (Exception ex) { Console.WriteLine(ex.Message); }
return string.Empty;
}
but when I send more than 79 then I get 3F in emulator.
7F is in fact the upper bound. Because that's 127 in decimal, the highest code point supported by the ASCII encoding. Code points higher than that get decoded to a question mark, having the code point of 63 or 3F in hexadecimal.
That's because you're using text to transmit binary data. Don't do that. See How can I convert a hex string to a byte array? for a proper implementation of "hex string to byte array".

null or Hex serial port encoding

I'm writing a C# application that will receive serial data from 3 different COM ports configure with 8-bit UART with no parity. The other devices will be sending and receiving binary encoded HEX ex. AF01h = 10101010 00000001 two characters for each byte. I have set up virtual COM ports and a simple application for testing purposes and am sending data back and forth before I hook the devices up. I found that data is ASCII encoded by default on transmission and reception but I need both to be binary encoded HEX. I do not see that option in the encoding class and would rather not have the application using a complete different encoding than the 3 other devices. Right now I am using this code to convert the string when it is sent
string binarystring = String.Join(String.Empty, hexstring.Select(c => Convert.ToString(Convert.ToInt32(c.ToString(), 16), 2).PadLeft(4, '0')));
sport.Write(binarystring);
txtReceive.AppendText("[" + dtn + "] " + "Sent: " + binarystring + "\n");
This works for testing transmission for now but i will eventually change the code to place the two digit hex number directly into a byte array.
This code will allow me to enter AF01h = 1010101000000001, but on the receiving end of the application I get 16 bytes of ASCII encoded characters. Is there a way I can get the app on the same page as the other devices?
Figured out a way to do it. Just needed to convert the long string of hex to two hex character byte integers
string hex = txtDatatoSend.Text; //"F1AAAF1234BA01"
int numOfBytes = HEX.Length;
byte[] outbuffer = new byte[numOfBytes / 2];
for (int i = 0; i < numOfBytes; i += 2)
{
outbuffer[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
}
sport.Write(outbuffer, 0, outbuffer.Length);
sport.DiscardOutBuffer()
The only caveat is you have to enter in the an even number of characters
On the other end the data gets placed right back in the Byte[] and i can decode it like this.
byte[] inbuffer = new byte[sport.BytesToRead];
sport.Read(inbuffer, 0, inbuffer.Length);
txtReceive.AppendText("[" + dtn + "] " + "Received: " + inbuffer.Length + " bytes ");
for (int i = 0; i < inbuffer.Length; i++)
{
string hexValue = inbuffer[i].ToString("X2");
txtReceive.AppendText(inbuffer[i] + " is " + hexValue + "HEX ");
}
txtReceive.AppendText("\n");
sport.DiscardInBuffer();

Random arithmetic overflows when reading from serial port

What I'm doing is taking a user entered string, creating a packet with the data, then sending the string out to a serial port. I am then reading the data I send via a loopback connector. My send is working flawlessly, however my receive is randomly throwing an arithmetic overflow exception.
I say randomly because it is not happening consistently. For example, I send the message "hello" twice. The first time works fine, the second time outputs nothing and throws an exception. I restart my program, run the code again, and send hello only to receive "hell" and then an exception. On rare occasion, I'll receive the packet 3 or 4 times in a row without error before the exception.
Here is my relevant code:
public void receivePacket(object sender, SerialDataReceivedEventArgs e)
{
byte[] tempByte = new byte[2];
int byteCount = 0;
while (serialPort1.BytesToRead > 0)
{
if (byteCount <= 1)
{
tempByte[byteCount] = (byte)serialPort1.ReadByte();
}
if (byteCount == 1)
{
receivedString = new byte[tempByte[byteCount]];
receivedString[0] = tempByte[0];
receivedString[1] = tempByte[1];
}
else if (byteCount > 1)
{
byte b = (byte)serialPort1.ReadByte();
receivedString[byteCount] = b;
}
byteCount++;
}
int strLen = (byteCount - 3);
tempByte = new byte[strLen];
int newBit = 0;
for (int i = 2; i <= strLen+1; i++)
{
tempByte[newBit] = receivedString[i];
newBit++;
}
string receivedText = encoder.GetString(tempByte);
SetText(receivedText.ToString());
}
I'm well aware that my implementation using byteCount (which I use to traverse the byte array) is rather sloppy. When I step through the code, I find that when I get the error byteCount == 1, which is making strLen a negative number (since strLen is byteCount - 3, which is done because the packet contains a header, length, and CRC i.e. byteCount - 3 == # of actual data bytes received). This leads to by tempByte having a size of -2, which throws my exceptions. I, however, am having a very hard time figuring out why byteCount is being set to 1.
The code after this basically just traverses the data section of the array, copies it into the tempByte, then is sent off to a function to append the text in another thread.
I am guessing that byteCount is 1 because you only received one byte - or rather, you processed the first byte before the second one arrived in the buffer.
The ReadByte function will wait for a certain amount of time for a byte to arrive if there isn't one waiting.
Maybe if instead of checking BytesToRead, you did something more like this:
byte headerByte = serialPort1.ReadByte();
byte length = serialPort1.ReadByte();
receivedString = new byte[length];
receivedString[0] = headerByte;
receivedString[1] = length;
for (int i = 2; i < length; i++) {
receivedString[i] = serialPort1.ReadByte();
}

C# Find offset of byte pattern, check specific byte, change byte, export part of byte array

This could be long one. I do have a binary file, that contains some information.
What I want to do:
File (Binary) is read from OpenFileDialog
I'm now searching for specific bytes in this file
I'm getting offset of that byte, and then I'm checking byte value of offset+2
Basic if for (if offset+2 value is 0x08, then do this, if not, then do something else)
Now, search for offset for another byte pattern.
Copy everything from that offset till the end of file
Save copied byte array to file.
So, here're my codes for every step.
Step one:
1.
Byte[] bytes;
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
path = ofd.FileName;
bytes = File.ReadAllBytes(path);
Step two, search specific pattern in this file. I used some help here on Stackoverflow, and end up with this:
VOID from stackoverflow:
static public List<int> SearchBytePattern(byte[] pattern, byte[] bytes)
{
List<int> positions = new List<int>();
int patternLength = pattern.Length;
int totalLength = bytes.Length;
byte firstMatchByte = pattern[0];
for (int i = 0; i < totalLength; i++)
{
if (firstMatchByte == bytes[i] && totalLength - i >= patternLength)
{
byte[] match = new byte[patternLength];
Array.Copy(bytes, i, match, 0, patternLength);
if (match.SequenceEqual<byte>(pattern))
{
positions.Add(i);
i += patternLength - 1;
}
}
}
return positions;
}
My void to search for pattern:
void CheckCamera()
{
Byte[] szukajkamera = { 0x02, 0x00, 0x08, 0x00, 0x20};
List<int> positions = SearchBytePattern(szukajkamera, bytes);
foreach (var item in positions){
MessageBox.Show(item.ToString("X2"));
IndexCamera = item;
}
int OffsetCameraCheck = IndexCamera + 2;
}
Item is now my offset, where 02 00 08 00 20 is in file.
Now, how do I check, if bytes(offset=IndexCamera+2) == 0x08 ?
I can do array.IndexOf, but there's plenty of 08 before that 08 I'm looking for.
For step 5 I'm also doing the thing, but it gets impossible for me, when Buffer.BlockCopy ask me for length.
For step 5 and forward I need to search again in this same file for another pattern, get it's offset and copy from that offset till the end. If I want so, then I need to buffer.blockcopy to non-empty byte array, but I just need it empty! I totally lost it. Please, help me.
Thank you!
how do I do bytes(offset=IndexCamera+2) == 0x08 ?
if(bytes[IndexCamera+2] == 0x08)....
When doing pattern searching the above answer does work, however you need to adapt it to search for more of the pattern.
Eg:
If you are looking for the location of 08 1D 1A AA 43 88 33
then you would need something like:
public static unsafe long IndexOf(this byte[] haystack, byte[] needle, long startOffset = 0)
{
fixed (byte* h = haystack) fixed (byte* n = needle)
{
for (byte* hNext = h + startOffset, hEnd = h + haystack.LongLength + 1 - needle.LongLength, nEnd = n + needle.LongLength; hNext < hEnd; hNext++)
for (byte* hInc = hNext, nInc = n; *nInc == *hInc; hInc++)
if (++nInc == nEnd)
return hNext - h;
return -1;
}
}
Note : Credit to Dylan Nicholson who wrote this code.

How can I convert an int to byte array without the 0x00 bytes?

I'm trying to convert an int value to a byte array, but I'm using the byte for MIDI information (meaning that the 0x00 byte which is returned when using GetBytes acts as a separator) which renders my MIDI information useless.
I would like to convert the int to an array which leaves out the 0x00 bytes and just contains the bytes which contain actual values. How can I do this?
You've completely misunderstood what you need, but luckily you mentioned MIDI. You need to use the multi-byte encoding that MIDI defines, which is somewhat similar to UTF-8 in that less than 8 bits of data are placed into each octet, with the remaining providing information about the number of bits used.
See the description on wikipedia. Pay close attention to the fact that protobuf uses this encoding, you can probably reuse some of Google's code.
Based on the info Ben added, this should do what you require:
static byte[] VlqEncode(int value)
{
uint uvalue = (uint)value;
if (uvalue < 128) return new byte[] { (byte)uvalue }; // simplest case
// calculate length of buffer required
int len = 0;
do {
len++;
uvalue >>= 7;
} while (uvalue != 0);
// encode (this is untested, following the VQL/Midi/protobuf confusion)
uvalue = (uint)value;
byte[] buffer = new byte[len];
for (int offset = len - 1; offset >= 0; offset--)
{
buffer[offset] = (byte)(128 | (uvalue & 127)); // only the last 7 bits
uvalue >>= 7;
}
buffer[len - 1] &= 127;
return buffer;
}

Categories

Resources