Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This code is able to run, however it only return 0 even though the device has some value. The device is a roller machine where it should give the rolling meter value to the port number 3 however, my code always return the output as 0.

Please help me as soon as possible as this is a critical requirement.


What I have tried:

using System;  
using System.Text;  
using System.IO.Ports;  
using System.Collections.Generic;  
namespace ConsoleApplication3  
{  
    class Program  
    {  
        static SerialPort sp;  
        string InputData = string.Empty;  
        static void Main(string[] args)  
        {  
            sp = new SerialPort("COM3", 4800, Parity.None, 8, StopBits.One);  
            sp.DiscardNull = false;  
            sp.RtsEnable = true;  
            sp.ReadTimeout = 500;  
            sp.Handshake = Handshake.None;  
            sp.Encoding = Encoding.UTF8;  
            sp.DtrEnable = true;    
            sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);              
            sp.Open();  
            //sp.DataReceived += sp_DataReceived;  
            string InputData = sp.ReadExisting();  
            while (sp.IsOpen)  
            {  
                try  
                {  
                    System.Threading.Thread.Sleep(300);  
                    int bytes = sp.BytesToRead;  
                    byte[] buffer = new byte[bytes];  
                    Console.WriteLine("Value - " + sp.Read(buffer, 0, bytes));  
                }  
                catch (Exception ex)  
                {  
                    Console.WriteLine(ex.Message);  
                }  
            }  
        }  
        private static void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)  
        {  
            SerialPort sport = (SerialPort)sender;  
            string indata = sport.ReadExisting();  
            Console.WriteLine(indata);  
        }  
    }  
}  
Posted
Updated 24-Mar-17 20:42pm

1 solution

Um. Stop doing that!
You have two different ways to read data in the same code: a loop which removes data and an event which also removes data. Pick one method, and stick with it.

Second, check that the machine is outputting data at all, and that your PC is receiving it: use something like Hyperterminal to watch the data as it comes in and make sure you both receive and can understand the data it is sending - if it's sending a stream of bytes, they may have to be interpreted correctly in order to be a "rolling weight" - even if the value it sends is a single byte indicating a weight as a value between 0 and 255 which is all a byte can transfer. Almost certainly, it's not that which means that the values it sends take more than one byte, which means they are packaged in some way so you can tell where each value begins and ends.

When you have definite data coming in to Hyperterminal, read the manual for the machine and find out what that data format is: then manually interpret what is being received before you even try to leap into code!
 
Share this answer
 

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