Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am reading binary data image from a serial port device how can i write it to a black and white image file. The project is a bill counting machine which solution. the device scans images of serial number of bill counted including the data counter

Below is the data returned from the device as read and written to a text file the unreadable strings are the binary data of the serial numbers of each bill counted. i want to read those data and write the raw binary data to image

NO.0012 SERIAL NUMBER

DATE 17.10.2017 21:21:24
EUR OP1

DENOMI. PCS. AMOUNT

50        1            50

20        1            20

10        4            40

5        2            10
TOTAL 8 120

DENO. VER. SERI NUMBER

10A *ÆxÿðÿðÿðððþàðßàÿÿàÿÿàÿÿàäÀ;ÀÿÀ{ÿÀxÿxàøøðÿðxÿðøÿðpÿÀÿÀÿÀÇÀ<çÀ<øøøøÀàðüðþñáþñáþÿàÿà<ÿãüÿÿüÿÿüüüüÀðwððÿÿþÿÿÿñ÷þññþñùþÿÿþÿÿþþÿüüÀxxGxÿðÿÿþÿÿÿqöø<ü<ø<|xxxøø°üÿüÿþþü<><><<|xÿxÿøøøø@ððÀðþð þðßþÿÿìÿàÿàÿàÿÀ<

5A *Æðÿþÿþþÿÿÿÿÿÿàððððxøxøÿxÿþxÿðxðxðððÿðÿÿÿÿøÿøxxÿxÿxøøøðøÿøÿøÿøø{øøøx øøø<<<<ø<ø<øÿøþÀÀÀÀ<8<ü<üþüþüþüþ>|ÿü<ÿü<ü<À<à8Àÿüÿüÿüÿüà<ÿà<ÿà<áÿüáÿüüàÀàð<ð<ñà>ÿà>ÿà>ÿà>ÿáþÿÿþÿÿþÿüààáàÿüÿÿþÿñþüðððÿþÿÿþÿÿþÿàþþðþðþðþððÿððÿðÿððððÿÿÿxÿxÿxðxÿðÿðÿOøðÿÿøðxxððø 5A *Æÿÿÿÿÿÿøðÿÿÿÿÿÿÿÿÿÿÿÿðððÿÿÿÿÿÿÿÿÿððÿðÿðÿðøðøÿÿÿxÿÿÿÿÿðøøøøxøxøÿxÿÿxÿÿxÿxðxxxÿxÿxÿxÿÿÿøøxøÿÿÿÿÿÿÿÿðÿÿÿÿÿÿxxxxÿÿÿÿÿÿðøøøøøxøÿxøÿxÿÿxÿÿxððÿÿÿÿÿÿÿÿÿxøÿÿÿÿÿÿÿÿððøÿÿÿÿÿøÿøøðÿÿÿÿÿÿÿðÿxxxxÿxÿxÿxÿðxÿøø

10A *Æxÿÿÿÿðÿÿÿÿðÿðø~ø0ÿÀÿÀÇÀÀüüøÀÀàþÿþðþðþðàðàðÿàÿÿàÿÿøðÿÿÿÿ|ø8øø<>x>ü>üøüüàÀþðþÿÿøÿðþðþøðøðÿÿÿðøÿøÿøøxøxÀx<8x<|x<øøÀàà|ãüÿÿüÿÿþáü0ðøøÿøÿxÿðyøøxøÿðÿøÿøðxðððÿ>ÿÿþÿþÿàððÿÿxÿxÿxðxÀÿÿÿø

20A *ÆpxÿÿðÿÿÿÿÿÿÿÿøÿÿÿÿÿÿÿøxxxxÿxÿxÿÿxÿøðxxxxxÿxÿxÿxÿøøðxxxxpxøxøøÿÿÿÿxÿðøðððÿðøðøðþpÿÿðððøÿÿÿOÿþðÿðÿðÿðÿÿÿÿÿÿÿÿðpÿÿÿÿÿþÿðÿðÿpþðÿðÿÿÿÿÿÿÿÿÿðÿðÿþðÿÿÿÿÿáÿÿðÿðÿþððÿñññÿñÿñÿÿÿÿàÿðññÿÿÿñÿÿþÿðÿð

50 *ÆÀÿÀÿüÿüÃüü<<<ÿüÿüÿÀÃÀÀÀ<ÇøÿøÿÿÇÿÿÿxüÿÿpÿxÿxÿxÿxxøÿÿðÿðððþÿÿþÿÿþÿÿñÿñþÿÿÿÿþÿþþàððüðþðþñÿüÿþÿàÿàþ<à<ÿà<ÿà<ÿà<À<ü<>ü>ü>ÈÀ8;üüÿ<ø<ø<ø<ø<øÿÿÿwøøøÿøøøøxð8ÿðððpxxxðøðøðÿðÿÿÿÿÿðøðpðððÿàÿþÿþÿþÿþÿþÿþñÿþüààà àààÿüÿüÿüÿàÀ

10A *Æ8ÿøÿøÿøÿÿ88ÿÿÿøÿð8xðøÿÿÿøøøxø8ÿÿÿøðøøàÿÿÿÿÿÿÿøøøððððüÿÿÿÿÿÿðþþÁþÿþþ~ÿÿððàðààþþþþ>þþÿÁààÀþÿþÿüþ<þ<þþ>üüüÀÀüüÿÿüüü<><üüüÀÀÿüÿÿÿÿÀ<<<<ÿÿÿÿÿøüüüü<|<|<ÿ<ø<ø8ø||||ÿ|gÿ||

10A *ÆÿðÿøÿÿÿÿÿøÿðÀ<ÿ<ÿ<<<<|üüøøÿøüxüü<<<|üüøÀÃÀÃüÿüÿüü<><<><<ü<üÿÿÿüÃüÀÀÀÿÀÿÀãàÃàüüüàÀáþÿþÿþþ>þ>>þ>þÿþÿþáüàààÿþÿþÿþþàðÿðÿðøðøðøðøððøðøðàðÿÿ|xxÿxðxðÿðÿøøðøøxxxxxxxxøxøxø8ø8|||x

SIGNATURE


What I have tried:

I have tried several ways to write to file 100 x 24 image but i only get black image with some blur with pixels across it. below is my code
C#
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        try
        {
            SerialPort spL = (SerialPort)sender;
            int bufSize = 20;
            Byte[] dataBuffer = new Byte[bufSize];
            StringBuilder sb = new StringBuilder();
            int lk = 0;
            byte[] lowbytes = { 0x03, 0x02, 0x0D, 0x0A, 0x0D, 0x0A };

            int bytes = spL.BytesToRead;
            byte[] buffer = new byte[bytes];
            spL.Read(buffer, 0, bytes);

            string fiFolderPath = ConfigurationManager.AppSettings["imagePath"] + "\\" + DateTime.Now.ToString("yyyyMMdd");
            if (!Directory.Exists(fiFolderPath))
            {
                Directory.CreateDirectory(fiFolderPath);
            }

            receiveBuffer.Clear();// = null;//  string.Empty;
            //SetReceivedText(spL.ReadExisting());
            var vImageByte = new List<byte>();
            Thread.Sleep(5000);


            receiveBuffer.Append(Environment.NewLine);
            int bByteCount = 0;
            //char readChar = "";

            //Task k = new Task()
            foreach (byte myByte in buffer)
            {
                char c = (char)myByte;
                if (char.IsWhiteSpace(c))
                {
                    switch (c)
                    {
                        case '\t':
                            receiveBuffer.Append("  ");
                            break;
                        case ' ':
                            receiveBuffer.Append(" ");
                            break;
                        case '\n':
                            receiveBuffer.Append(Environment.NewLine);
                            break;
                        case '\r':
                            receiveBuffer.Append(Environment.NewLine);
                            break;
                        case '\v':
                            receiveBuffer.Append("\v");
                            break;
                        case '\f':
                            receiveBuffer.Append("\f"); //"\\f";
                            break;
                    }
                    //readChar = c.ToString();
                }
                else if (char.IsControl(c) || myByte.ToString() == "33" || myByte.ToString() == "63")
                {
                    receiveBuffer.Append("");
                }
                else
                {

                    //if character is string * start reading image data
                    if (c.ToString() == "*")
                    {




                        while (true)
                        {

                            //MessageBox.Show(c.ToString(), "Teller App", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            //byte singleByte = buffer[bByteCount];
                            char buffCharData = (char)buffer[l];
                            //MessageBox.Show(buffCharData.ToString(), "Teller App", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            if (buffCharData == '\n')
                            {
                                int b = l - 2;
                                buffCharData = (char)buffer[b];
                                //MessageBox.Show(buffCharData.ToString(), "Teller App", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                                break;
                            }
                            bool jdata = false;
                            switch (buffCharData)
                            {
                                case '\t':
                                    jdata = true;
                                    break;
                                case '\n':
                                    jdata = true;
                                    break;
                                case '\r':
                                    jdata = true;
                                    break;
                            }
                            if (jdata)
                            {
                                break;
                            }
                            vImageByte.Add(buffer[l]);
                            //singleByte.CopyTo(imageByte, imageByte.Length);
                            l++;
                        }

                        byte[] imageByte2 = vImageByte.ToArray();

                        string fiPath = ConfigurationManager.AppSettings["imagePath"] + "\\" + DateTime.Now.ToString("yyyyMMdd");
                        Random r = new Random();
                        fiPath = fiPath + "\\" + transID + "_" + r.Next(0, 1000) + ".bmp";
                        //WriteBitmapFile(fiPath, 224, 24, imageByte2);
                        //SaveBitmap(fiPath, 100, 20, imageByte2);
                        //File.WriteAllBytes(fiPath, imageByte2);

                        //calculate stride 
                        //PixelFormat pf = PixelFormat.Format4bppIndexed;
                        //int bitsPerPixel = ((int)pf & 0xff00) >> 4;
                        //int bytesPerPixel = (bitsPerPixel + 3) / 4;
                        //int stride =  4 * (100 * bitsPerPixel + 3)/4; //4 * ((111 * bytesPerPixel + 3) / 4);
                        //unsafe
                        //{
                        //    fixed (byte* ptr = imageByte2)
                        //    {
                        //        using (Bitmap image = new Bitmap(111, 24, stride, PixelFormat.Format4bppIndexed, new IntPtr(ptr)))
                        //        {
                        //            image.
                        //            image.Save(fiPath);
                        //        }
                        //    }
                        //}


                        unsafe
                        {
                            fixed (byte* pBytes = imageByte2)
                            {
                                int width = 50; // Your width
                                int height = 24; // Your height
                                int stride = width * 4; // Your stride
                                Bitmap bitmap = new Bitmap(width, height, stride, PixelFormat.Format32bppArgb, new IntPtr(pBytes));
                                bitmap.Save(fiPath); // Could save to another format like .jpg
                            }
                        }

                        storeImageToFile(ByteArrayToBitmap(imageByte2, 100, 24), fiPath);
                    }
                    string data = "";
                    data = string.Format("{0}", c.ToString());// System.Text.ASCIIEncoding.ASCII.GetChars(buffer).ToString());
                    receiveBuffer.Append(data);

                }

                bByteCount++;
            }
            receiveBuffer.Append(Environment.NewLine);
            receiveBuffer.Append("----------------------------------------------------------------");
            receiveBuffer.Append(Environment.NewLine);

            SetReceivedText(receiveBuffer.ToString());
            writeToLog(receiveBuffer.ToString());

        }
        catch (Exception ex)
        {
            MessageBox.Show("check general log :-" + ex.Message, "Teller App", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

    }
Posted
Comments
Richard MacCutchan 5-Jan-18 4:50am    
You first need to know the format of the input data, and how to create a viewable image from that data.
Amedora Ahmed 7-Jan-18 13:59pm    
thanks for your comment @Richard, the information i got from the vendor support personnel is that it is a binarized serial number data
Richard MacCutchan 8-Jan-18 4:08am    
Well I am afraid that means nothing.
Amedora Ahmed 23-Feb-18 8:25am    
@Richard you're right... it means nothing

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900