japsu2
csharp
a month ago
2.1 kB
1
Indexable
Never
using System.IO.Ports; using System.Security; using System.Security; using Wapice.IoTTicket.RestClient; using Wapice.IoTTicket.RestClient.Model; serialize(); void serialize() { 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(); } void sendData(string value) { var username = "xxxx@xxxx.FI"; var pwd = new SecureString(); "xxxxx!".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 = value; data.Unit = "c"; data.Timestamp = DateTime.Now; var writeResult = client.WriteDatapointAsync("xxxxx", data).Result; } 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); sendData(bufferStrmod); //Console.ReadKey(); //return bufferStrmod; }