1

i am working on read and write the Mitsubishi PLC via C# application i need to read the date and time of plc and display in the datagridview in application. If i store a date example Year,Month and Day in D111,D112,D113 then i read the date sucessfully but the problem is year is stored in D111,Month and Day are store in switch D112 bytes 0-7 and bytes 8-15. if i read D111 and D112 then the date will be displayed like 2022/0309 but i need 2022/03/09 . how can i read the bytes 0-7 and 8-15 separately .

int count;
       plc.GetDevice("D22", out count);
       int result;
       plc.GetDevice("D22", out result);
       int read_;
       plc.GetDevice("D22", out read_);
       int read1;
       plc.GetDevice("D22", out read1);
       int year;
       plc.GetDevice("D220", out year);
       int month;
       plc.GetDevice("D221", out month);
       int day;
       plc.GetDevice("D222", out day);
       int hour;
       plc.GetDevice("D223", out hour);
       int minute;
       plc.GetDevice("D224", out minute);
       int second;
       plc.GetDevice("D225", out second);
       SqlCommand cmd;

       cmd = new SqlCommand("insert into [Table](date,time,count,tool,up,down) values(@date,@time,@count,@tool,@up,@down)", con);
       con.Open();



       {

           cmd.Parameters.AddWithValue("@date", year.ToString() + "/" + month.ToString() + "/" + day.ToString());
           cmd.Parameters.AddWithValue("@time", hour.ToString() + ":" + minute.ToString() + ":" + second.ToString());
           cmd.Parameters.AddWithValue("@count", count.ToString());
           cmd.Parameters.AddWithValue("@tool", read1.ToString());
           cmd.Parameters.AddWithValue("@up", result.ToString());
           cmd.Parameters.AddWithValue("@down", read_.ToString());
           cmd.ExecuteNonQuery();
           con.Close();
           DisplayData();
           ClearData();
       }

1 Answer 1

1

I don't know if you really need to directly read a BYTE from the PLC, but this can be easily achieved by manually splitting the data into bytes:

int monthAndDay; 
plc.GetDevice("D112", out monthAndDay);

//monthAndDay will get something like x0903 (hex)
//Then we split it into bytes 
byte[] bytes = BitConverter.GetBytes(monthAndDay);

//And then we get the data. Just check if the first byte really is the Month in the PLC
int month = bytes[0];
int day = bytes[1];

Recommendation: Change the description of your question from "Bits" to "Bytes", because that's what your question is about. ;)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.