#WinUI Serial Monitor
1380 messages · Page 2 of 2 (latest)
You can restyle it
Tho this is a little tedious in this framework
Do you want to make it so that the background colour never changes?
Or do you only want to disable the orange underline?
uh I like theres an indicator that ure in the box
if it just changed colour slightly and had no underline it would be nice
It just doesn't fit your design tho
ye ill have to figure that one out
sorry for the late reply had to clean something up
where would that go?
Anywhere
doesnt accept that eitehr
Inside the grid
I also need to fix this
How should it be?
You need to override the accent colour just for the application
I think you can override SystemAccentColor under Application.Resources
btw do numberboxes return integers?
NumberBox.Value returns double
oh
But you can disable entering non-integers I think
tbf I think Ill just cast it to an integer
wait u can put letters in a numberbox wth
No?
ooh it does remove the contents afterward
outputText().Text() returns hstring tho?
How is it letting you assign to a std::string directly
oh no thats just temporary
Yeah but does this actually compile?
std::ofstream fileOut{ "data.txt" };
fileOut << data;
You can shorten this to two lines
Unlike Java, C# or other languages you need not close the file manually thanks to RAII
oh damn
is there any easy way to convert it to a regular string
or can I just save it in a textfiile as a hstring?
winrt::to_string
what would happen
That's also a possibility yes but the encoding is different (UTF-16)
UTF-8 is much more common for text files, even on Windows
ooh
Or well since you are already in the winrt namespace, can write just to_string
btw do I have to premake the data.txt file
No?
very nice
Wait though where do you want it to go?
This might put it in the app installation folder or maybe the folder it was launched from in the command line
Maybe you want the user to select
That's a better option
/home/user/ is only on Linux
oh ye this is just something I found on stack overflow
Not sure if this works, but could you try if it accepts %homepath% to refer to the current user's folder?
Otherwise you'd have to query for the current user's folder
trying
where would it direct it to then?
C:\Users\jaiga for me
but does it send it to where the program is?
No
cant find a file
either it didnt work or idk where it went lol
doesnt send it to the desktop either
You'd need to use a function like https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetknownfolderpath to get the paths of common folders
This should work:
#include <shobjidl_core.h>
#include <Microsoft.UI.Xaml.Window.h>
#include <winrt/Windows.Storage.Pickers.h>
HWND windowHandle;
check_hresult(try_as<IWindowNative>()->get_WindowHandle(&windowHandle));
Windows::Storage::Pickers::FileSavePicker picker;
check_hresult(picker.as<IInitializeWithWindow>()->Initialize(windowHandle));
picker.SuggestedFileName("data.txt");
picker.FileTypeChoices().Insert(L"Plain text", single_threaded_vector<hstring>({ L".txt" }));
auto const file = co_await picker.PickSaveFileAsync();
// use file.Path()
It's a bit tedious due to their bad API.
I saw this but it's in C#: https://learn.microsoft.com/en-us/uwp/api/windows.storage.pickers.filesavepicker?view=winrt-22621
But IInitializeWithWindow isn't in the code which is necessary in a non-UWP app; there's only a mention at the top.
Their documentation isn't very well organised nowadays
But in my experience, Apple's is often even worse
I could imagine
what do the -> do again
Oh you don't know it
ive heard it before I just forgot cuz Ive never used it
It's for accessing members of a type from a pointer
p->x is the same as (*p).x
With references, however, you just use dot like regular objects
damn didnt know this
so when I do something like:
StopbitPortBox().SelectedIndex()
this is a reference to the location of the selected index of the stopbitbox?
so then StopbitPortBox()->SelectedIndex() would output the location?
You use . here tho, not ->
what would happen if I did ->
It won't work
in what cases does it work with ->?
When you have pointers
C++/WinRT mostly exposes references and values rather than pointers
But the classic types like IWindowNative and IInitializeWithWindow are held by pointer
It's hard to explain
But that's how it is
damn
I always found pointers a bit confusing
but its mainly because I havent had to use them that often so its hard to get used to the concept
In C++ a pointer is just a nullable and reassignable reference
And references are aliases to existing objects
Now this isn't the definitions others in the server would give, but for simplicity that's all you need to know
thats fair
was this in one of the provided links?
Not exactly
This one is in C#
This one only shows the IInitializeWithWindow part
picker.FileTypeChoices().Insert(L"Plain text", single_threaded_vector<hstring>({ L".txt" }));
This one I took from the page for the property: https://learn.microsoft.com/en-us/uwp/api/windows.storage.pickers.filesavepicker.filetypechoices?view=winrt-22621
@night peak I just tested the serial monitor with an atmega
I cant receive data
with the UI
wait I need to verify the base code works first one sec
it used to work in my separate program
ye it still works
It works?
Put breakpoints and see what values you get
Put a breakpoint in a previous line?
nothing happens
Is the function even called?
doesnt seem like it
Ah there's a bug there
You need ==, not just =
so keeprunning should be called
= assigns false to the variable and then the if evaluates false which means nothing happens
You read 8 bytes but only have 2 bytes in the array?
Maybe you got confused with bit
ReadFile takes size in bytes
honestly I was kinda confused about the readfile altogether
in the reference they used an 8 byte buffer
and im pretty sure they read from the referenced dwbytesread
wait let me find it
dwBytesRead gets you how much bytes were actually rèad, after the call.
yea right??
I was talking with an irl about this
he said it gave the byte itself
like the data
this is what we read
It clearly says the number of bytes actually read
yea exactly lol
I showed him that and he still kept saying the other thing haha
oh shit it does something
The problem is that there are more than 2 bytes, for example 4 bytes, available.
Now ReadFile will say 4 is less than the max of 8 bytes given to me, so I will write dataReadBuff[3] and dataReadBuff[3], and put 4 into dwBytesRead - which corrupts the stack as there are no dataReadBuff[3] and [4]
TL;DR, change the 8 to a 2, so you don't run into the issue of it corrupting the stack
Are those the correct chars tho
In this you forgot to put a newline character when appending new output
ok fixed it 
it does output the correct initial value
but it doesnt seem to update
nvm it doe
does
But you cleared the string here?
the textbox just doesnt follow the text
like it just disappears super far down
and u have to scroll
Because there's no space?
I changed it to a 1
isnt that correct?
Because you have lots of data?
That's good too, just nothing larger than 2, as it may write to unallocated memory
a lot
Hmm
im running an atmega at 8 mhz
although an ADC which I think is 1 mhz iirc
I can show u my code for the atmega if u want
its just taking a potentiometer output voltage into an adc, converts it back to voltage and then into characters and sends them one by one with a terminator
I see
is there any way for the textbox to follow the bottom on its own?
https://stackoverflow.com/a/57099086/12025335
I found this
Tho it's not in C++/WinRT so you'll have to port
lemme look through it
the serial monitor can also send data 
it does crash though when I send b for begin
like if I send an e it does stop transmitting
but if I send a b to it it crashes
Hmm
Why do you need an array of 2 chars when you are only going to send or receive one
im really just following this guide
but does an array not always include the null character in the end?
Only strings do
aah
jaiganesh would u happen to know why get usernameA doesnt work in this project
but it does in another
nvm it works now

it seems like theres problems regarding resetting the serial device handle
even if I just doublepress confirm settings so it should just replace the handle with an identical one it stops receiving data
You shouldn't CloseHandle there it will continue to be used by keepRunning until a line terminator is reached.
Instead, close it here:
if (serialDeviceTemp != serialDevicePass)
{
CloseHandle(serialDeviceTemp);
serialDeviceTemp = serialDevicePass;
}
where is here?
setupButton_Click
but serialdevicetemp is local to the keeprunning isnt it?
Yes it is
wouldnt that be a problem?
No
I think this is the most messages I've seen in a thread lol
No after writing everything
alrightg
oh boy
sometimes it breaks at the scoped_lock
I saw it once break into readChar
the problem might be that it gets stuck trying to read while no data is transmitted
it is afterall always when theres no data transmitted from the atmega and I try to send something it crashes
the old program had the same problem I just checked
and that program only ran on 1 thread
I also got 1 bug where it started reading but all empty stuff so it only kept the '\r' newline and just spammed nothingness
but I cant recreate that one
but yea it seems like it gets stuck on reading nothing and then cant write at the same time
I imagine ReadFile waits for something to be sent from the other end
yea it really looks like that
problem is how to make it not read while nothing is being sent lol
because I kinda have to check with read to know if anythings been sent..
u reckon this could help
what do I type as the handle thread?
its the background thread id want to close
or cancel
Well since you used thread-pool threads (via resume_background), you'll want to call GetCurrentThread and store it globally
Note that it may change after every resume_background
what is the type of a thread?
Here it's just an handle
Global or class member
yep
Or well it might be better to create a dedicated thread than using the pool since you continously use it
But shouldn't be a big issue
instead of resume background?
Hit a breakpoint to where CancelSynchronousIo is called
Nah
it still dies
Cancel before the lock
You'd also have to modify the code in Keeprunning
To release the lock if the operation was cancelled
You've cancelled the thing but the lock still isn't unlocked
oh I thought itd just go through readChar and through the lock
Put a breakpoint after ReadFile and check if it hits
No it did the job
Otherwise it wouldn't reach here
Wait one thing, can you enclose ReadFile inside winrt::check_bool
That will throw the value of GetLastError as an exception
Or at least check the bool return value of ReadFile
Is it false when cancelled? I'm not sure
but its missing the arrow?
Ah no it doesn't reach then
like usually it looks like this when it breaks at my breakpoints
Note: change thread handle after the resume_background
Because they are thread-pool threads, which can change
Otherwise create a regular thread with std::thread or Win32 CreateThread
You might also want the backgroundThread variable to be atomic
ok yea it definitely gets stuck at readFile
I just used the step thing from while(true) down to readChar
and into readChar
when it hits readFile it just dies
Try this
I'm not sure if ReadFile gives a GetLastError error
Or maybe CancelSynchronousIo does
The docs page doesn't clearly say where these flags are sent from
Right
Win32 functions like these one return false on failure and then store additional error details in GetLastError
check_bool is an helper function in C++/WinRT that throws it as an exception
should I output it somewhere?
No, check_bool will throw an exception instead
I dont think it did anything
damn I tried this and it also didnt work
Remove the ReadFile and check what the return value is instead
Wait perhaps dwBytesRead is 0 when cancelled
You could use that to handle the failure case
You always to need to return something
yea I mean it just doesnt move out of readFile
So CancelSynchronousIo doesn't work
doesnt seem like it
This one is wrong tho — you have undefined behaviour
I also set the read timeouts down to 1
also didnt help
there is this
ReadFileEx
and cancello
?
cancelloEx
Yes but more complex
how complex is it?
Little
This question is being automatically marked as stale.
If your question has been answered, type !solved.
If your question is not answered feel free to bump the post or re-ask.
Take a look at !howto ask for tips on improving your question.
I was gone a bit had to focus on some other work but
Im genuinely not sure whether or not its necessary to add anything to my completion routine
it seems as though the completion routine is mainly for releasing memory by closing handles and/or processing the data
but that is all done outside of my read function