ExcelPackage.Dispose() blocks input from serial port

ExcelPackage.Dispose() blocks input from serial port



My application needs to open a Serial port and wait for data to be transmitted. After opening the serial port, I use EPPlus to read data from an excel sheet and store it in a list of dictionaries.



However, when I added the Read method, I stopped receiving data from the Serial port. After some testing, the serial port seems to break when .Dispose() is called on the ExcelPackage I use to read the excel sheet.



My Excel reading code is as follows:


public List<Dictionary<string, string>> Read(string fileName)

List<Dictionary<string, string>> dictList = new List<Dictionary<string, string>>();
FileInfo file = Utils.GetFileInfo(fileName, false);

// using statement automatically calls Dispose() which closes the package.
using (ExcelPackage package = new ExcelPackage(file))

// Reads the first worksheet of the workbook
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
int endColumn = worksheet.Dimension.End.Column;
int endRow = worksheet.Dimension.End.Row;

// Start loop from 2nd row to avoid headers
for (int row = 2; row <= endRow; row++)

Dictionary<string, string> dictionary = new Dictionary<string, string>();

for (int column = 1; column <= endColumn; column++)

// Add key value pair to dict for every column
// key = column header
// value = cell value to string

// key is set to "" if cell is empty
string key = worksheet.Cells[1, column].Value == null ? "" : worksheet.Cells[1, column].Value.ToString();
// value is set to "" if cell is empty
string value = worksheet.Cells[row, column].Value == null ? "" : worksheet.Cells[row, column].Value.ToString();

dictionary.Add(key, value);

dictList.Add(dictionary);


return dictList;



And my Serial Port is opened in a different class:


public BarcodeScanner()

try

SerialPort mySerialPort = new SerialPort(SearchComPort());
mySerialPort.Open();
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
isAvailable = true;


catch (Exception ex)

Debug.WriteLine("Error: 0", ex.Message);




The code seems to work if I use


ExcelPackage package = new ExcelPackage(file)



And omit the .Dispose() call at the end, but from my understanding that would cause issues down the line since it makes the Excel worksheet unavailable to other processes.





Hmm, no, that would be the Butterfly Effect and C# does not suffer from that. As posted, the serial port is going to behave completely randomly. The code does not provide the essential configuration. Baudrate, Stopbits, Parity, Databits. And Handshake in particular, an easy way to get Read() to block. Switching back-and-forth to a terminal emulator program is an easy way to get the defaults to change without noticing.
– Hans Passant
Aug 17 at 19:01





What exactly do you mean? The barcode scanner gets a string from SearchComPort(), for example "COM4". I checked the code used by SerialPort and Baudrate, Parity, etc have default values, such as 9600 for baudrate. Using these default value's has worked without issue thus far.
– Louis
Aug 17 at 20:10





1 Answer
1



What if you read all the data into a byte array using System.IO.File.ReadAllBytes(fileName), get that into a stream, then open the excel file using one of the (stream) overloads of EPPlus's ExcelPackage?


System.IO.File.ReadAllBytes(fileName)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

Henj