Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
When the computer restart or shutdown does he sends any message to the serial port
(before restating or shtting down )
I have a microcontroller circuit connected to the computer through a serial port in which I let two led’s exchange their works after every second and this continues in an endless loop ,but if the user restart the computer I need to let the microcontroller program restart from the top of its program (prog.asm) too
How can I do that?
i am using window 7, the microcontroller is pic 16f883,i have two program one is c# and the other is assembly pic program the c# program sends messege to the pic program (and is located in the StartUp directory),assume the pic program is to do job A followed by job B. Job B is to alternat two led's forever,but if the user restart the computer the pic would be still working in Job B whic is not my desired ,i have to added a command inside the endless loop(in pic assembly program)or may be start an interrupt in which i could write
goto top_of_Pic_program ;
therefore i need some message from computer to inform the pic that the computer
has started from over so the pic program must start from the top too.i am using pin 2 and 3 of the serial port to read and write connected to rs232 ic which is connected to the 16f883. no error in either program but i want to improve it
Posted
Updated 1-Dec-12 15:45pm
v7
Comments
Nelek 1-Dec-12 13:49pm    
How can you do what?
To know that the computer is going to be restarted? to send the command to the serial port? to restart the mC when the command comes?

What kind of computer have to be checked? Windows 2000, XP, 7, 8? Linux? What kind of microcontroller? programmed how?

As you see, there are some things you haven't explain, please use the "improve question" widget and give a bit more information.
Khalid Sabtan 1-Dec-12 21:48pm    
if you still require more information i will post part of the two program
after 9 hours

1 solution

You need to develop some application-level protocol with some "Restart" command. As you need to do the restart on PC restart/shutdown, you can do this by using the class Microsoft.Win32.SystemEvents. You will need to handle the event SessionEnding or SessionEnded:
http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.aspx[^],
http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.sessionending.aspx[^],
http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.sessionended.aspx[^],
http://msdn.microsoft.com/en-us/library/microsoft.win32.sessionendedeventargs.reason.aspx[^],
http://msdn.microsoft.com/en-us/library/microsoft.win32.sessionendreasons.aspx[^].

[EDIT]

It could be something like this:

C#
Microsoft.Win32.SystemEvents.SessionEnded += (sender, eventArgs) {
    if (eventArgs.Reason ==   Microsoft.Win32.SessionEndReasons.SystemShutdown
        && IsMicrocontrollerConnected()) // whatever it is
            SendShutdownNotification(); // whatever it is
}; // SessionEnded


You may or may not want to check the Reason. Another reason could be log off, and, if your application is a Windows Service, it is not a sufficient reason for resetting of the microcontroller, because your code can continue functioning without anyone logged on. A "regular" application will be terminated by logging off, so you would need to remove this check.

Also, you may not need the check IsMicrocontrollerConnected (whatever it is) or something like that, because the code shown above can add the shown event handler to the invocation list of this system event only when you already know that the microcontroller is connected and its state is ready to accept shutdown notification.

See also:
http://msdn.microsoft.com/en-us/library/microsoft.win32.sessionendedeventhandler.aspx[^],
http://msdn.microsoft.com/en-us/library/microsoft.win32.sessionendedeventargs.aspx[^].

[END EDIT]

The microcontroller program should receive and recognize this command and handle it properly. What is "the top of the program" for your "prog.asm" depends on the logic of this program. This should be pretty easy to do.

—SA
 
Share this answer
 
v4
Comments
Khalid Sabtan 2-Dec-12 8:54am    
Hellow Sergey

There is no illustration for EventsThreadShutdown in msdn (i guess)anyway

Before I ask you again , I have to inform you that the circuit is at my work and now i am at home. Therefor i can not test the following code on my circuit ,please let me know if I insert the following line

Send2PicNumber(9);//9 means to the pic that the pc is now being shuting off

Inside the method ShutDown0() [see both methods below]Would this works ?

----------------------------------------------------------------------------

public static void Send2PicNumber(int Num)
{
Byte[] encodedBytes = new Byte[10];// ascii.GetBytes(str);
encodedBytes[0] = (Byte)Num;sp.Write(encodedBytes, 0, 1);
}
private void ShutDown0()
{ ManagementBaseObject mboShutdown = null;

ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");

mcWin32.Get();
Send2PicNumber(9);//<==================================

mcWin32.Scope.Options.EnablePrivileges = true;

ManagementBaseObject mboShutdownParams = mcWin32.GetMethodParameters("Win32Shutdown");


// Flag 1 means we want to shut down the system. Use "2" to reboot.

mboShutdownParams["Flags"] = "1";
mboShutdownParams["Reserved"] = "0";

foreach (ManagementObject manObj in mcWin32.GetInstances())
{
mboShutdown = manObj.InvokeMethod("Win32Shutdown", mboShutdownParams, null);

}
}
public enum ShutDown {

LogOff = 0, Shutdown = 1, Reboot = 2, ForcedLogOff = 4,

ForcedShutdown = 5,ForcedReboot = 6, PowerOff = 8, ForcedPowerOff = 12
}
Sergey Alexandrovich Kryukov 2-Dec-12 13:41pm    
Well, what you are writing might work, too, but this is not what I recommended. I recommended something way simpler and more reliable -- please see my update, after [EDIT].

--SA
Khalid Sabtan 3-Dec-12 12:33pm    
Ok your answer is clear now,i can trace the shutdown message by the c# program using what you have suggested above.

But i still wonder if i can trace a shutdown or restart using the prog.asm (the microcontroller program) ignore the c# program just imagin the microcontroller is working alone and a serial port is connected between the computer and the PIC board (i mean the microcontroller) (through rs232 IC).There are 9 pins,pin number 2 ,3 and 5 are connected to rs232 IC if i could trace the shutdown or reset message through pin number 3 this will be a great idea if however the operating system sends message through any other pins (of those 9 pins of the serialport) i can connect PORTA(any bit ,bits 0 to 7) or PORTB(any bits,bits 0 to 7) or any other port and receive this valuable message from the operating system.

i ought to notefy you that i am not using a Windows Service ( i do not know what a window service is) acutally i am using a window form and c# Visual studio 2008 to send numbers to a microcontroller through a serialport

many thanks in advance
Sergey Alexandrovich Kryukov 3-Dec-12 12:37pm    
Serial port has standard pin assignment; you can only send data (bytes, text, whatever) and hardware handshake. You don't trace anything on you PC host by the microcontroller, you just handle certain "command"...
--SA

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