Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9119157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:19:41+00:00 2026-06-17T05:19:41+00:00

I have a weighing machine that is connected to a computer using a serial

  • 0

I have a weighing machine that is connected to a computer using a serial port. It is a very old machine and we are trying to get the weights off it and save in a database.
The weight returned by the machine has some invalid characters like ?, and the weight is displayed as ??2?0, where it should have been 02220.

I understand it has something to do with encoding as results on a web search suggest. But I can not figure out what exactly am I missing.

Here is my code:

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    // This method will be called when there is data waiting in the port buffer
    // Read all the data waiting in the buffer
    // string data = comport.ReadExisting();
    // Display the text to the user in the Rich Text Box
    Log(LogMsgType1.Incoming, s); 
}

public void OpenThisPort()
{
    bool error = false;

    // If the port is open, close it
    if (comport.IsOpen)
    {
        comport.Close();
    }
    else
    {
        comport.BaudRate = int.Parse("1200");
        comport.DataBits = int.Parse("8");
        comport.StopBits = StopBits.One;
        comport.Parity = Parity.None;
        comport.PortName = "COM1";
        delStart = 0;
        delLength = 9;
        comport.RtsEnable = true;
        comport.DtrEnable = true;
        comport.Encoding = System.Text.Encoding.GetEncoding(28591);
    }  

How do I determine exactly which encoding will be applied?
Any idea what I’m missing here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:19:43+00:00Added an answer on June 17, 2026 at 5:19 am

    It may not by the encoding issue but error on hardware.
    Try detecting it:

        #region comPort_ErrorReceived
        /// <summary>
        /// This method will be called when there's data waiting in the buffer
        ///  and error occured.
        /// DisplayData is a custom method used for logging
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void comPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
        {
            SerialError sr = e.EventType;
            switch (sr)
            {
                case SerialError.Frame:
                    DisplayData(newLog, EventLogEntryType.Error, "On port " + comPort.PortName
                        + " the hardware detected a framing error.\n", 45);
                    break;
                case SerialError.Overrun:
                    DisplayData(newLog, EventLogEntryType.Error, "On port " + comPort.PortName
                        + " a character-buffer overrun has occurred. The next character is lost.\n", 46);
                    break;
                case SerialError.RXOver:
                    DisplayData(newLog, EventLogEntryType.Error, "On port " + comPort.PortName
                        + " an input buffer overflow has occured. There is either no room in the input buffer,"
                        + " or a character was received after the End-Of-File (EOF) character.\n", 47);
                    break;
                case SerialError.RXParity:
                    DisplayData(newLog, EventLogEntryType.Error, "On port " + comPort.PortName
                        + " the hardware detected a parity error.\n", 48);
                    break;
                case SerialError.TXFull:
                    DisplayData(newLog, EventLogEntryType.Error, "On port " + comPort.PortName
                        + " the application tried to transmit a character, but the output buffer was full.\n", 49);
                    break;
                default:
                    DisplayData(newLog, EventLogEntryType.Error, "On port " + comPort.PortName
                        + " an unknown error occurred.\n", 50);
                    break;
            }
        }
    

    At first glance it looks like framing error, as part of the data seem to be correct. For starters make sure that your cable is good. Also you may try connecting to your device with some other application, e.g putty.

    Also reading your data as bytes may be a good idea (and then you may convert it to hex before displaying). This way you will find out what is actually getting sent.

    private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
    int btr = comPort.BytesToRead;
    byte[] comBuffer = new byte[btr];
    comPort.Read(comBuffer, 0, btr);
    Console.WriteLine(ByteToHex(comBuffer));
    }
    
    private string ByteToHex(byte[] comByte)
            {
                StringBuilder builder = new StringBuilder(comByte.Length * 3);
                foreach (byte data in comByte)
                    builder.Append(Convert.ToString(data, 16).PadLeft(2, '0').PadRight(3, ' '));
                return builder.ToString().ToUpper();
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a serial port app that read weighing machine. public void Read() {
I have a USB weighing scale that I'm trying to import the reading into
I have a stored procedure that I am calling using EXECUTE IMMEDIATE. The issue
After much research I am still stumped. I have a serial port object which
I have a USB HID scale that I need to fetch the weighing reports
Have a painfully simple blog Post creator, and I'm trying to check if the
have 2 questions : A computer with 32-bit address uses 2-level page table (9
have a nice day. I got problem when trying to create an image from
I have a Windows Service that performs a number of periodic activities, and I
I'm working at a development shop that is using outdated bug tracking software. They're

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.