Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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 Bit 0-7 and Bit 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 bits 0-7 and 8-15 separately .

What I have tried:

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();
           }
Posted
Updated 9-Mar-22 13:17pm
v2
Comments
[no name] 8-Mar-22 23:33pm    
var month = 0309 / 100;
var day = 0309 % 100;
string s = $"{month}/{day}";
n.deny 9-Mar-22 0:48am    
Thankyou for your comment @Gerry Schmitz. could you please tell me about 0309.if possible can you please provide little more sample code.
n.deny 9-Mar-22 3:47am    
thanks Gerry Schmitz .. i tested sucessfully
[no name] 9-Mar-22 11:17am    
You're welcome.
Member 13566383 9-Mar-22 1:56am    
Why do you need more code?
The three lines of code Gerry Schmitz provided is all you need.

1 solution

Quote:
Thankyou for your comment @Gerry Schmitz. could you please tell me about 0309.if possible can you please provide little more sample code.
Oh come on!

You give an example using specific values "2022/03/09" and then when you are given sample code using the same specific values you can't work out what you need to do to make it generic?

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

But we aren't here to hand-hold you and tell you exactly what keys to press in what sequence!
 
Share this answer
 
Comments
[no name] 9-Mar-22 11:18am    
And thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900