BlueScreen[Error : 0x0000003b / c0000005] Reading bytes from SerialPort

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

c#
serial-port
blue-screen-of-death
asked on Stack Overflow Jan 20, 2020 by Nico Walsemann

1 Answer

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
    }
}
answered on Stack Overflow Jan 22, 2020 by Baddack

User contributions licensed under CC BY-SA 3.0