Solved

C# SerialPort Problem

Posted on 2010-08-26
7
1,398 Views
Last Modified: 2013-12-17
Hello guys,
I have a Fiskal Cash Register Printer, which has got the following communication protocol for sendin data to it.
<01><LEN><SEQ><CMD><DATA><05><BCC><03>
So I wrote a function called getCommand that has got as parameters the CMD, SEQ and DATA. Everything else is being calculated By the function which returns a byte array.
then I do the following

serialPort serial = new serialPort("COM1", 115200);
serial.Open();
serial.Write(getCommant(CMD, SEQ, DATA), 0, Size of returned by getCommand);
Serial.Close();

private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            MessageBox.Show(serialPort.ReadLine());
        }

private void serialPort_ErrorReceived(object sender, System.IO.Ports.SerialErrorReceivedEventArgs e)
        {
            MessageBox.Show(serialPort.ReadLine());
        }

I also make an event for DataRecieved and ErrorRecieved, which have the following codes.
And when I send the data to the fiskal printer it doesn't do anything. I doesn't even give me an error.
0
Comment
Question by:IncognitoMan
7 Comments
 
LVL 5

Assisted Solution

by:greatsubash
greatsubash earned 166 total points
ID: 33539422
0
 
LVL 9

Expert Comment

by:puru1981
ID: 33540429
you need to add these two lines
serial.DataReceived+=new System.IO.Ports.SerialDataReceivedEventHandler(serial_DataReceived);
serial.ErrorReceived+=new System.IO.Ports.SerialErrorReceivedEventHandler(serial_ErrorReceived);

after that these event will fire.
0
 

Author Comment

by:IncognitoMan
ID: 33540510
greatsubash, thank you for the useful link.
puru1981, thank you too :), but I don't code from yesterday and I know how to handle events :).

I have found the problem. The problem is int the BCC. It's wrong. I am calculatng the BCC using the following algorithm.
 private byte calculateBCC(byte[] arr)
            {
                byte checkSum = 0x00;
                foreach (byte b in arr)
                {
                    checkSum ^= b;
                }
                return checkSum;
            }

So this returns one byte. But in the documentation it's written the following:
<BCC> - Control Checksum
Lenght - 4 bytes?!?!!?! WTF????

The sum is for the things between <LEN> ... <05> including them. Every digit from the two bytes is being calculated as 0x30 is being added to it. For examplethe sum of 0x1AE3 is 0x31 0x3A 0x3E 0x33. How do I calculate those two bytes?
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 9

Accepted Solution

by:
puru1981 earned 167 total points
ID: 33541574
congrates you solved it yourself.

I am extremely sorry if i have hurt you in any manner. I was just pointing out if it is missing. Most of the time people does this type of mistake if they create a variable this way. What i have concluded from the given data i responded to that.
0
 

Author Comment

by:IncognitoMan
ID: 33541987
puru1981, you haven't hurt me in any way. I was just kidding. How can you know if I am some "ooooh what does thiiis button dooo", or some "super compuer god guy" or something else :). But I will be vary happy if someone gives me the solution :).
0
 
LVL 3

Assisted Solution

by:bkokx
bkokx earned 167 total points
ID: 33546034
If your BCC is 4 bytes you need to add it to a uint32.
Why dont you pass the serialport as argument to the getCommant() function?
If nothing happens validate the following issues:
1) Do you have a control application so you known for sure the hardware is working (e.g. you have a correct cable).
2) You did not specify the lenght, parity and stopbits; note that this defaults to 8,N,1
3) Check your BCC calculation is giving the correct results
0
 

Author Comment

by:IncognitoMan
ID: 33552415
While I was calculating :) I found the solution. I downloaded a COM port monitoring program and began watching what info about BCC the original program is sending and after one hour of calculations and atempts to figure out the BCC algorythm by myself, I finally found it. Now I sell and everything works just fine :). I will give you guys points, because of your will to help. I value the will to help. Cheers :)

PS: The algorythm is the following:
You add all the bytes from LEN to 05 to an unsigned short and then divide it to four tetrads. Put the tetrads into four bytes and add 0x30 to each byte. :)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 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