Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# SerialPort Problem

Posted on 2010-08-26
7
Medium Priority
?
1,429 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 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

705 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