Click here to Skip to main content
15,117,318 members
Home / Discussions / C#
   

C#

 
QuestionProgrammatically login to website that uses 2 steps. Pin
Vouksh10-Feb-16 3:18
MemberVouksh10-Feb-16 3:18 
AnswerRe: Programmatically login to website that uses 2 steps. Pin
OriginalGriff10-Feb-16 5:14
mveOriginalGriff10-Feb-16 5:14 
GeneralRe: Programmatically login to website that uses 2 steps. Pin
Vouksh10-Feb-16 5:30
MemberVouksh10-Feb-16 5:30 
GeneralRe: Programmatically login to website that uses 2 steps. Pin
Luc Pattyn11-Feb-16 1:29
sitebuilderLuc Pattyn11-Feb-16 1:29 
GeneralRe: Programmatically login to website that uses 2 steps. Pin
OriginalGriff11-Feb-16 1:35
mveOriginalGriff11-Feb-16 1:35 
SuggestionRe: Programmatically login to website that uses 2 steps. Pin
Richard Deeming10-Feb-16 5:16
mveRichard Deeming10-Feb-16 5:16 
AnswerRe: Programmatically login to website that uses 2 steps. Pin
V.10-Feb-16 21:21
professionalV.10-Feb-16 21:21 
QuestionIssue with Serial Port class Pin
Member 1206160010-Feb-16 0:13
MemberMember 1206160010-Feb-16 0:13 
I have a serial port class which looks something like this:

C#
public static class OldHaspCommunication
 {
    private static SerialPort m_port;
    private static string m_portName;
    private static bool m_isOpen = false;

    private static int m_baudRate;
    private static Parity m_parity;
    private static int m_dataBits;
    private static StopBits m_stopBits;
    private static bool m_xonxoff;
    private static bool m_dtr;
    private static bool m_rts;

    // ................................

    // Open port.
    public static void OpenPort(string portName,
                                int baudRate,
                                Parity parity,
                                int dataBits,
                                StopBits stopBits,
                                bool dtr,
                                bool rts,
                                bool xonxoff = false)
    {

        // Create new Serial Port object
        m_port = new SerialPort(portName, baudRate, parity, dataBits, stopBits);

        // .........................................
        // Set class member variables.

        m_portName = portName;
        m_baudRate = baudRate;
        m_parity = parity;
        m_dataBits = dataBits;
        m_stopBits = stopBits;
        m_xonxoff = xonxoff;
        m_dtr = dtr;
        m_rts = rts;

        // ............................................
        // Initialize some port object properties

        // XOnXOff - parameter
        if (xonxoff)
            m_port.Handshake = Handshake.XOnXOff;

        // Set DTR/RTS
        m_port.DtrEnable = dtr;
        m_port.RtsEnable = rts;

        // Set
        m_port.ReadTimeout = 500;
        m_port.WriteTimeout = 500;

        // ..............................................
        // Some final steps.

        // Open the port for communications
        m_port.Open();

        // If we get this far, mark port as opened.
        m_isOpen = true;
    }

    /// <summary>
    /// Just makes sure to read the specified number of bytes from the serial port.
    /// Typical reads may return fewer bytes then told to read. This method is used
    /// to avoid this problem.
    /// Based on a similar function from Jon Skeet: http://jonskeet.uk/csharp/readbinary.html
    /// </summary>
    /// <param name="data"> [OUT] Data read should be stored here</param>
    /// <param name="length"> [IN] How much data to read exactly </param>
    /// <param name="count"> [IN] Some kind of retry count parameter.</param>
    private static void ReadAll(byte[] data, uint length, int retryCount)
    {

        // Check data length parameter.
        if (length > data.Length)
        {
            throw new Exception("Wrong data length in ReadAll");
        }

        int offset = 0;
        int i = 0;
        int remaining = checked((int)length); // NOTE: Will throw OverflowException if length > Int32.MaxValue due to checked keyword.

        // Start reading.
        while (remaining > 0)
        {
            // Just a retry count check
            if (i >= retryCount)
            {
                throw new Exception("Exceeded retry count parameter during reading in ReadAll");
            }

            // Read Certain amount of bytes
            int read = m_port.Read(data, offset, remaining);

            // Was there error?
            if (read <= 0)
            {
                throw new EndOfStreamException(String.Format("ReadAll(OldHaspCommunication) - End of data reached with {0} bytes left to read", remaining));
            }

            // Advance offset, and decrease remaining count.
            remaining -= read;
            offset += read;

            i++;

        }


    }

    /// <summary>
    /// Write specified number of bytes to the serial port
    /// </summary>
    /// <param name="data"> [IN] Buffer from which to write data</param>
    /// <param name="length"> [IN] How many bytes to write</param>
    private static void WriteAll(byte[] data, uint length)
    {
        // TODO: Maybe we should add a retry count as in C++

        int len = checked((int)length); // NOTE: Will throw OverflowException if length > Int32.MaxValue due to checked keyword.

        // We can't write more data to serial port than available in the array.
        if (len > data.Length)
        {
            throw new Exception("Wrong data length in WriteAll");
        }

        // Do the write.
        m_port.Write(data, 0, len);
    }





    /// <summary>
    /// Close the serial port.
    /// NOTE: Don't forget to call this one.
    /// </summary>
    public static void ClosePort()
    {
        if (m_isOpen)
        {
            Reset(); // Calls our custom Reset function.
            m_port.Close(); // Close port
            m_isOpen = false; // Mark this object as closed.
        }
    }

    // ...
    private static void Reset()
    {
        m_port.DiscardInBuffer();
        m_port.DiscardOutBuffer();
    }


}



It seemed to be working fine, but I encountered problems in such a case.
When my device HASP was plugged into the computer everything went fine.
Then I unplugged the hasp and continued using the class, but I was getting error that I was calculating the data incorrectly (signature was wrong which I compute using write/read calls).

Then I plugged again my HASP and got such exception(with stack trace):


2016-02-10 13:01:14 - [LOG_ERROR] [THREAD ID: 11] General Exception: Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
at ProcessingLibrary.OldHaspCommunication.Reset() in c:\Users\g.\Documents\Visual Studio 2012\Projects\ProcessingLibrary\ProcessingLibrary\src\HASP\OldHaspCommunication.cs:line 270
at ProcessingLibrary.OldHaspCommunication.QueryParam(Byte[] in_buffer, UInt32 length, Byte command, Byte[] out_buffer, UInt32 szBuf, Byte& ret_code, Int32 l_entry, Int32 l_loop, Boolean quick_ser_no) in c:\Users\g.\Documents\Visual Studio 2012\Projects\ProcessingLibrary\ProcessingLibrary\src\HASP\OldHaspCommunication.cs:line 303
at ProcessingLibrary.OldHaspCommunication.Query(Byte[] in_buffer, UInt32 length, Byte command, Byte[] out_buffer, UInt32 szBuff, Byte& retcode) in c:\Users\g.\Documents\Visual Studio 2012\Projects\ProcessingLibrary\ProcessingLibrary\src\HASP\OldHaspCommunication.cs:line 277
at ProcessingLibrary.OldHaspCommunication.QueryCmd8(Byte[] in_buffer, Byte command, Byte[] out_buffer) in c:\Users\g.\Documents\Visual Studio 2012\Projects\ProcessingLibrary\ProcessingLibrary\src\HASP\OldHaspCommunication.cs:line 286
at ProcessingLibrary.CryptographyUtilities.SetSessionKey() in c:\Users\g.\Documents\Visual Studio 2012\Projects\ProcessingLibrary\ProcessingLibrary\src\Security\CryptographyUtilities.cs:line 94
at ProcessingLibrary.CryptographyUtilities.OldHASPSign(Byte[] buffer, UInt32 size, Byte[] outbuffer, Boolean session) in c:\Users\g.\Documents\Visual Studio 2012\Projects\ProcessingLibrary\ProcessingLibrary\src\Security\CryptographyUtilities.cs:line 137
at ProcessingLibrary.UpdateTerminalInfoMethods.TestConnection(ProcessingErrorInfo& errorInfo, String terminalID, UInt32 STAN) in c:\Users\g.\Documents\Visual Studio 2012\Projects\ProcessingLibrary\ProcessingLibrary\src\Processing Functions\UpdateTerminalInfoMethods.cs:line 70


Query functions (listed above) are also from HASP Class - I removed them for clarity they just call read/write methods in different ways and maybe Reset in some cases.

Can you please explain what problem happened and why? (Reset function got called by one of Query functions as it seems).
This must be related to the fact that I unplugged the HASP?
But why such exception?

How to modify my class such that if someone unplugs the HASP and then plugs it in, calls to the HASP are still successful?

Please note that initially my design was to call OpenPort once in application. And to call ClosePort also once in application.
SuggestionRe: Issue with Serial Port class Pin
Richard MacCutchan10-Feb-16 1:14
mveRichard MacCutchan10-Feb-16 1:14 
GeneralRe: Issue with Serial Port class Pin
Member 1206160010-Feb-16 1:23
MemberMember 1206160010-Feb-16 1:23 
GeneralRe: Issue with Serial Port class Pin
Richard MacCutchan10-Feb-16 1:30
mveRichard MacCutchan10-Feb-16 1:30 
GeneralRe: Issue with Serial Port class Pin
Member 1206160010-Feb-16 1:38
MemberMember 1206160010-Feb-16 1:38 
GeneralRe: Issue with Serial Port class Pin
Richard MacCutchan10-Feb-16 2:02
mveRichard MacCutchan10-Feb-16 2:02 
GeneralRe: Issue with Serial Port class Pin
Member 1206160010-Feb-16 2:18
MemberMember 1206160010-Feb-16 2:18 
GeneralRe: Issue with Serial Port class Pin
Richard MacCutchan10-Feb-16 2:30
mveRichard MacCutchan10-Feb-16 2:30 
GeneralRe: Issue with Serial Port class Pin
Member 1206160010-Feb-16 2:52
MemberMember 1206160010-Feb-16 2:52 
GeneralRe: Issue with Serial Port class Pin
Eddy Vluggen10-Feb-16 4:00
professionalEddy Vluggen10-Feb-16 4:00 
AnswerRe: Issue with Serial Port class Pin
Gerry Schmitz10-Feb-16 6:39
mveGerry Schmitz10-Feb-16 6:39 
QuestionWeb Services - Error in deserializing body of request message for operation Pin
FabeCode9-Feb-16 19:54
MemberFabeCode9-Feb-16 19:54 
AnswerRe: Web Services - Error in deserializing body of request message for operation Pin
Gerry Schmitz10-Feb-16 6:13
mveGerry Schmitz10-Feb-16 6:13 
Question'ctor design and extra "cost" of using nullable ValueTypes ? Pin
BillWoodruff9-Feb-16 1:31
mveBillWoodruff9-Feb-16 1:31 
AnswerRe: 'ctor design and extra "cost" of using nullable ValueTypes ? Pin
Pete O'Hanlon9-Feb-16 2:02
mvePete O'Hanlon9-Feb-16 2:02 
GeneralRe: 'ctor design and extra "cost" of using nullable ValueTypes ? Pin
BillWoodruff9-Feb-16 21:08
mveBillWoodruff9-Feb-16 21:08 
GeneralRe: 'ctor design and extra "cost" of using nullable ValueTypes ? Pin
Pete O'Hanlon9-Feb-16 22:30
mvePete O'Hanlon9-Feb-16 22:30 
SuggestionRe: 'ctor design and extra "cost" of using nullable ValueTypes ? Pin
Richard Deeming9-Feb-16 2:51
mveRichard Deeming9-Feb-16 2:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.