Example #1
0
    public bool Send(byte[] buffer)
    {
        if (writeState > UARTWritingState.free)
        {
            return(false);
        }
        if (buffer.Length > 255 || buffer.Length == 0)
        {
            return(false);
        }
        if (!device.IsOpen)
        {
            return(false);
        }
        writeState = UARTWritingState.writing;
        var  buf = new byte[buffer.Length + 3];
        byte crc = 255;

        buf[0] = (byte)ASCII.SOT;//Sized
        buf[1] = (byte)buffer.Length;
        buffer.CopyTo(buf, 2);
        for (int i = 0; i < buffer.Length; i++)
        {
            crc ^= buffer[i];
        }
        buf[buf.Length - 1] = (byte)ASCII.EOT;
        uint bwrite = 0;

        device.Write(buf, 0, buf.Length);
        writeState = UARTWritingState.free;
        return(true);
    }
Example #2
0
    public bool Send(byte[] buffer, byte sizeByteLength, bool useCRC)
    {
        if (writeState > UARTWritingState.free)
        {
            return(false);
        }
        if (buffer.Length > 255 || buffer.Length == 0)
        {
            return(false);
        }
        if (!device.IsOpen)
        {
            return(false);
        }
        writeState = UARTWritingState.writing;
        var bufAddLen = 2;

        bufAddLen += sizeByteLength;
        if (useCRC)
        {
            bufAddLen += 1;
        }
        var buf = new byte[buffer.Length + bufAddLen];

        buf[0] = sizeByteLength > 0 ? (byte)ASCII.SOH : (byte)ASCII.STX;
        if (sizeByteLength > 0)
        {
            buf[sizeByteLength] = (byte)buffer.Length;
        }
        buffer.CopyTo(buf, 1 + sizeByteLength);
        if (useCRC)
        {
            byte crc = 255;
            for (int i = 0; i < buffer.Length; i++)
            {
                crc ^= buffer[i];
            }
            buf[buf.Length - 2] = crc;
        }
        buf[buf.Length - 1] = (byte)ASCII.EOT;
        uint bwrite = 0;

        device.Write(buf, 0, buf.Length);
        writeState = UARTWritingState.free;
        return(true);
    }
Example #3
0
 public bool Send(byte[] buffer)
 {
     if (writeState > UARTWritingState.free) return false;
     if (buffer.Length > 255 || buffer.Length == 0) return false;
     if (!device.IsOpen)
     {
         return false;
     }
     writeState = UARTWritingState.writing;
     var buf = new byte[buffer.Length + 3];
     byte crc = 255;
     buf[0] = (byte)ASCII.SOT;//Sized
     buf[1] = (byte)buffer.Length;
     buffer.CopyTo(buf, 2);
     for (int i = 0; i < buffer.Length; i++)
     {
         crc ^= buffer[i];
     }
     buf[buf.Length - 1] = (byte)ASCII.EOT;
     uint bwrite = 0;
     device.Write(buf, 0, buf.Length);
     writeState = UARTWritingState.free;
     return true;
 }
Example #4
0
 public bool Send(byte[] buffer, byte sizeByteLength, bool useCRC)
 {
     if (writeState > UARTWritingState.free) return false;
     if (buffer.Length > 255 || buffer.Length == 0) return false;
     if (!device.IsOpen)
     {
         return false;
     }
     writeState = UARTWritingState.writing;
     var bufAddLen = 2;
     bufAddLen += sizeByteLength;
     if (useCRC) bufAddLen += 1;
     var buf = new byte[buffer.Length + bufAddLen];
     buf[0] = sizeByteLength > 0 ? (byte)ASCII.SOH : (byte)ASCII.STX;
     if (sizeByteLength > 0)
     {
         buf[sizeByteLength] = (byte)buffer.Length;
     }
     buffer.CopyTo(buf, 1 + sizeByteLength);
     if (useCRC)
     {
         byte crc = 255;
         for (int i = 0; i < buffer.Length; i++)
         {
             crc ^= buffer[i];
         }
         buf[buf.Length - 2] = crc;
     }
     buf[buf.Length - 1] = (byte)ASCII.EOT;
     uint bwrite = 0;
     device.Write(buf, 0, buf.Length);
     writeState = UARTWritingState.free;
     return true;
 }
 protected void setWriteState(UARTWritingState state){
     writeState = state;
 }