Click here to Skip to main content
16,015,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to get data out of the machine via serial port communication. The folloeing is the code.
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SerialPortListener.Serial;
using System.IO;

namespace SerialPortListener
{
    public partial class MainForm : Form
    {
        SerialPortManager _spManager;
        public MainForm()
        {
            InitializeComponent();

            UserInitialization();
        }

      
        private void UserInitialization()
        {
            _spManager = new SerialPortManager();
            SerialSettings mySerialSettings = _spManager.CurrentSerialSettings;
            serialSettingsBindingSource.DataSource = mySerialSettings;
            portNameComboBox.DataSource = mySerialSettings.PortNameCollection;
            baudRateComboBox.DataSource = mySerialSettings.BaudRateCollection;
            dataBitsComboBox.DataSource = mySerialSettings.DataBitsCollection;
            parityComboBox.DataSource = Enum.GetValues(typeof(System.IO.Ports.Parity));
            stopBitsComboBox.DataSource = Enum.GetValues(typeof(System.IO.Ports.StopBits));

            _spManager.NewSerialDataRecieved += new EventHandler<serialdataeventargs>(_spManager_NewSerialDataRecieved);
            this.FormClosing += new FormClosingEventHandler(MainForm_FormClosing);
        }

        
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            _spManager.Dispose();   
        }

        void _spManager_NewSerialDataRecieved(object sender, SerialDataEventArgs e)
        {
            if (this.InvokeRequired)
            {
                // Using this.Invoke causes deadlock when closing serial port, and BeginInvoke is good practice anyway.
                this.BeginInvoke(new EventHandler<serialdataeventargs>(_spManager_NewSerialDataRecieved), new object[] { sender, e });
                return;
            }

            int maxTextLength = 1000; // maximum text length in text box
            if (tbData.TextLength > maxTextLength)
                tbData.Text = tbData.Text.Remove(0, tbData.TextLength - maxTextLength);

            // This application is connected to a GPS sending ASCCI characters, so data is converted to text
            string str = Encoding.ASCII.GetString(e.Data);
            tbData.AppendText(str);
            tbData.ScrollToCaret();

        }

        // Handles the "Start Listening"-buttom click event
        private void btnStart_Click(object sender, EventArgs e)
        {
            _spManager.StartListening();
        }

        // Handles the "Stop Listening"-buttom click event
        private void btnStop_Click(object sender, EventArgs e)
        {
            _spManager.StopListening();
        }

  
        {

        }
    }
}

The output that I am getting is
--------------------------------JL206F             ID:VDE40050


2015/05/28    12:02:08
JBatch_No.:057
JOperator ID:15
--------------------------------
         Mixed Counting
W--------------------------------Deposit Amount:             0.00
--------------------------------
denom       count          value
UVKW50D1          50.00
JUVK|????D     20.00
JUVK    1          10.00
J--------------------------------
Total:          3          80.00
--------------------------------
Coin:                       0.00
--------------------------------
Balance:                    0.00
--------------------------------
Now in denumination category i am getting uvkw I want it to be 50, 20 and 10. The data is not correct. I sthere any way that I can get correct data and also if i want to put it in datagrid is it possible.
Posted
Updated 28-May-15 1:45am
v3
Comments
Richard MacCutchan 28-May-15 7:24am    
You need to check the documentation for the device you are reading from, to see how to get the correct data.
Richard Deeming 28-May-15 7:38am    
What's with the "netseer" script block at the bottom of the question? This looks like a failed attempt to inject third-party ads into the site.

1 solution

I see this is the third question you have asked on this same subject. You have tried hyperterm, UDP and now serial port. Maybe your time would be better spent reading the device documentation.
 
Share this answer
 
Comments
Florian Braun 28-May-15 8:28am    
+5 for the best solution so far

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