Solved

C# SerialPort Problem

Posted on 2010-08-26
7
1,416 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

617 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