Solved

C# SerialPort Problem

Posted on 2010-08-26
7
1,378 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now