0

i have a BarcodeScanner here which im telling to make a picture for me. Im sending a command as byte to the scanner, waiting for the response and then im trying to read it.

if i just use a serial monitor and send the bytes manually it works just fine, i get my whole jpeg and im happy.

if i try it in C# i try to read the bytes with a simple method (was the bread and butter method which i found on google. normal i used (serialport.ReadExisting() which crashes the same way).

here is the whole event :

 public void serial_datareceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                usescanner = (SerialPort)sender;
                if (sender is SerialPort)
                {
                    string port = ((SerialPort)sender).PortName;
                    int count = ((SerialPort)sender).BytesToRead;
                    int returnAscii = 0;
                    string message = "";
                    while (count > 0)
                    {
                        returnAscii = ((SerialPort)sender).ReadByte();
                        message = message + Convert.ToChar(returnAscii);
                        count--;
                    }
                    ScanPort sport = new ScanPort(port, true);
                    scanner.ScannerPort = sport;
                }

            ((SerialPort)sender).Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

i Always get 4096 bytes to read back. and then i can read till about 70-90 bytes so yes my messagestring is that long then so it does work ! just simply crashes in the middle of it.

the scanner is not the problem as im working with the scanner for a long time. and i can send data back and forth. also ive tested it on a demoprogram which the manufacturer handed me so it does work.

any ideas? maybe i just dont know how to read bytes.

thanks for the help

  • What line throws that error? You have some weird casting going on, my guess is you casted something incorrectly. – Baddack Jan 20 '20 at 21:00
  • The Castin is correct, the sender is the SerialPort so i just take the Port which is receiving data and Using it as a SerialPort which it is. I also am Reading bytes, its just at a certain port the bluescreen gets me. which i doesnt understand, i am asking for bytestoread, and in a serial port monitor (ext program) i see my whole string which i am reading out, but at a certain point it just crashes my whole pc – Nico Walsemann Jan 21 '20 at 07:05

1 Answers1

0

You set usescanner as your SerialPort but then you continue to call ((SerialPort)sender) throughout the code and don't reference usescanner. What was the point of setting it?

Anywho, you should just setup a very simple example and see if you can get the error to happen. Sometimes when you add too much complicated code its hard to find the issue. I recommend going simple to prove that is the problem and not your code. Create a new form with a open button and a close button.

public partial class Form2 : Form
{
    SerialPort sp;

    public Form2()
    {
        InitializeComponent();
    }

    private void buttonOpen_Click(object sender, EventArgs e)
    {
        sp = new SerialPort("COM1", 9600);  //initialize our serial port
        sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived); //create our data received event
        sp.Open(); //Open the port
    }

    void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        string Data = sp.ReadExisting();

        Console.Write(Data);
    }

    private void buttonClose_Click(object sender, EventArgs e)
    {
        sp.Close(); //close the port
    }
}
Baddack
  • 1,947
  • 1
  • 24
  • 33