![]() |
Forum Index : Microcontroller and PC projects : Sending commands from Windows to PICOMITE
Author | Message | ||||
pwillard Guru ![]() Joined: 07/06/2022 Location: United StatesPosts: 313 |
So, there is this really nice feature I like in MMCC that comes along with MMEDIT where you can issue some commands directly to the (in my case) the PICOMITE. For example, I'm talking about making a selection and it will perform an action like "SET TIME" or "SET RTC" for example. These are really handy and I like them a lot and you get quite near to the perfect setting of RTC time. But then, I started thinking about how I wanted *my* current CLOCK project to work and realized that I wanted to work in UTC time. But that MMCC action uses my PC's *local time*. I wanted to emulate the ACTION command with my own code on the PC but I wanted to send UTC-based values. Something MMCC doesn't do unless I changed my local TIME on my PC to UTC. Hmmm... maybe mot the best approach. So I started this morning to tackle the problem... One of my goals was to seek out the ACTIVE comports on my PC so I could choose which one I wanted to talk to. (Something that Bugs me a lot about PUTTY, since you have to know the com port to use before starting Putty) Yes, even Terterm knows how to do this. * Tried Python3... got into weeds super fast trying that... gave up. * Tried PureBasic... Probably possible... but I find Purebasic gets pretty aggravating pretty fast when trying something new. * Tried QB64PE - Man... if it's possible, I didn't see a way... * I even looked at MMBASIC-DOS... but it would have only worked as a child process being fed command line arguments such as "serial COM Port" to use. Finally, started with Windows Powershell... (Which I know literally nothing about... but found it... "possible"). So below are today's efforts to send UTC time to my Picomite. # A crude Powershell script to set the clock to UTC on the Picomite # because I never program in Powershell, that's why. ########################################################################### # Don't look to closely, I'm a pretty lazy coder. Only rudimentory error # handling... # # Pete Willard # September 1, 2022 # # Step one, Determine which COM ports are active and select one of them # Step two, Determine if we are setting the RTC or local DATE$ TIME$ values # Step Three, Connect to the selected port at 19200 baud and send strings ########################################################################### Clear-Host # $head1 = "PicoMite: Set Time to UTC at 19200 baud" $head2 = "=======================================" Write-Host -ForegroundColor Cyan $head1 Write-Host -ForegroundColor Cyan $head2 Write-Host # Using get-pnpdevice to retrieve known "ports" $portList = Get-PnpDevice -Class Ports -ea 0 # Make sure it's an array $portlist = @($portlist) # Create an array we can add values to with .ADD() $comports = New-Object System.Collections.Generic.List[System.Object] #Find all active serial ports in the list if ($portList) { foreach ($device in $portList) { if ($device.Present) { # Display the active devices and show what they are named # since it helps identify which one you want to use Write-Host "--- Active Device: " $device.Name $FriendlyName = $device.Name.split("(") $ComPort = $friendlyName[1].split(")") $ComPort = $ComPort.Trim() # Push known active COM ports into an array $ComPorts.Add($ComPort) } } } # A menu to select a specific COM port by number $selection = $null Write-Host Write-Host "Available COM Ports`r`n===================" do { Write-Host # empty line # List Choices for ($i = 0; $i -lt $ComPorts.count; $i++) { Write-Host -ForegroundColor Cyan " $($i+1)." $ComPorts[$i] } Write-Host # empty line $ans = (Read-Host 'Please make a choice ') -as [int] } while ((-not $ans) -or (0 -gt $ans) -or ($ComPorts.count -lt $ans)) # For clarity, not functionality $selection = $ComPorts[$ans - 1] $UseComPort = $selection # How do we want to update time? $heading = "Real Time Clock`r`n===============" $query = "Do you use a real time clock?" $choices = '&Yes','&No' $decision = $host.UI.promptforchoice($heading,$query,$Choices,1) if ($decision -eq 0) { Write-Host '- Setting the RTC time to UTC' } if ($decision -eq 1) { Write-Host '- Setting $TIME and $DATE to UTC' } # Finally, we setup the serial connection $Myport = New-Object System.IO.Ports.SerialPort $UseComPort,19200,None,8,one try { $Myport.Open() # $MyQuote = "$([char]34)" $ctrlz = "$([char]26)" # Wakeup the remote end if ($myPort.isOpen) { # Send a CTRL-Z to makesure we have a prompt $myPort.Writeline($ctrlz) Write-Host "- Serial Port open..." if ($Decision -eq 0) { Write-Host "- SET Real Time Clock" $utctime = Get-Date -AsUTC -Format "yyyy,MM,dd,HH,mm,ss" # For RTC $MyPort.Writeline("RTC SETTIME $utctime ") write-host "- Sending -- RTC SETTIME $utctime " } if ($Decision -eq 1) { Write-Host "- SET DATE And TIME STRINGS " $utcdate = Get-Date -AsUTC -Format "yyyy/MM/dd" # Date$ $MyLine = "DATE$ = " + $MyQuote + $utcdate + $MyQuote $myport.Writeline($myline) Write-Host "-- Sending -- $myline" # Start-Sleep -Seconds 1 # wait a moment $utcloctime = Get-Date -AsUTC -Format "HH:mm:ss" # Time$ $MyLine = "TIME$ = " + $MyQuote + $utcloctime + $MyQuote $myport.Writeline($myline) Write-Host "-- Sending -- $myline" } } else { Write-Host ("Sorry, Serial Port Access Failed") } } catch { { 1: Write-Host ("Sorry, Serial Port was not opened. Is it in use?") } } # Clean up and get out $myPort.close() Write-Host "`r`n- Serial Port Closed.`r`n- Work Completed." Edited 2022-09-02 10:06 by pwillard |
||||
stanleyella![]() Guru ![]() Joined: 25/06/2022 Location: United KingdomPosts: 2566 |
Quote "One of my goals was to seek out the ACTIVE comports on my PC so I could choose which one I wanted to talk to. (Something that Bugs me a lot about PUTTY, since you have to know the com port to use before starting Putty) Yes, even Terterm knows how to do this." I have been there with putty and teraterm serial com port set up. teraterm finds the port. It must be simple for win to send serial data to a microcontroller. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
@pwillard You could have used MMBasic for Windows to do the conversion to UTC or done the maths on the picomite. The commands DATETIME$ and EPOCH are handy for time/date maths. I have also added a macro in MMCC to give UTC time. \d and \h still expand to current date and time. In uppercase, \D and \H do the same but in UTC. The settime menu has not changed so you will have to create a macro for UTC. I have improved the macro management in the next update which will be in a day or two. In MMCC, I use the registry to get a current list and also monitor windows messages to look for a "device changed" message. Jim Edited 2022-09-02 13:09 by TassyJim VK7JH MMedit |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
For MMBasic for Windows or DOS: ' FOR n = 1 TO 16 ON ERROR SKIP OPEN "com"+STR$(n)+":19200" AS #2 'print mm.errno IF MM.ERRNO = 0 THEN PRINT "com"+STR$(n)+" available." CLOSE #2 ENDIF NEXT n Jim VK7JH MMedit |
||||
pwillard Guru ![]() Joined: 07/06/2022 Location: United StatesPosts: 313 |
As usual, I make the "simple things" difficult. And silly me, I mixed up sending CTRL-C with CTRL-Z $ctrlc = "$([char]3)" Edited 2022-09-02 22:43 by pwillard |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |