japsu

mail@pastecode.io avatar
unknown
csharp
a year ago
1.8 kB
3
Indexable
using System.IO.Ports;
using System.Security;
using System.Security;
using Wapice.IoTTicket.RestClient;
using Wapice.IoTTicket.RestClient.Model;



SerialPort port = new SerialPort();
port.PortName="COM3";
port.Parity=Parity.None;
port.BaudRate=9600;
port.DataBits = 8;
port.StopBits=StopBits.One;

if (port.IsOpen)
{
    port.Close();
    port.Dispose();
}
port.Open();
byte[] bytesToSend=new byte[3]{0x3c,0x02,0x3e};
port.Write(bytesToSend,0,bytesToSend.Length);
port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
Console.ReadKey();
port.Close();
port.Dispose();


var username = "xxxx@xx.FI";
var pwd = new SecureString();
"xxx!".ToList().ForEach(pwd.AppendChar);

var client = new IoTTicketClient(new Uri("https://my.iot-ticket.com/api/v1/"), username, pwd);

var data = new DatanodeWritableValue();

data.Name = "TestJM";
data.Path = "Path";
data.Value = "100";
data.Unit = "c";
data.Timestamp = DateTime.Now;

var writeResult = client.WriteDatapointAsync("xxxx", data).Result;


static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
    //Console.WriteLine("In data receive");
    SerialPort port = (SerialPort)sender;

    int bytes = port.BytesToRead;
    byte[] buffer = new byte[bytes];

    if (port.BytesToRead > 1)
    {
        port.Read(buffer, 0, bytes);
    }
    
   // foreach (byte item in buffer)
    //{
   //     Console.WriteLine(Convert.ToChar(item));
   // }

    string bufferStr="";
    for (int i = 0; i < buffer.Length; i++)
    {
        bufferStr += Convert.ToChar(buffer[i]);
    }
    string bufferStrmod = bufferStr.Substring(1, bufferStr.Length - 2);
    
    Console.WriteLine("string ilman päätteitä "+bufferStrmod);
    //Console.ReadKey();
    //return bufferStrmod;
}