Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# SerialPort Problem

Posted on 2010-08-26
7
Medium Priority
?
1,449 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 664 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 9

Accepted Solution

by:
puru1981 earned 668 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 668 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

926 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