I'm working to program a MCU board over RS485. I have the section of code done to access the bootloader no problem. My problem is in this code:
int xon_off = ComPort.ReadChar();
if (xon_off == send_data) {
int counter = 0;
string line;
System.IO.StreamReader file = new System.IO.StreamReader("C:/Users/user/Desktop/x.hex");
while ((line = file.ReadLine()) != " ") // reads until end of file
{
write_line: line = file.ReadLine();
if (xon_off == send_data) {
ComPort.Write(line);
//System.Threading.Thread.Sleep(500);
counter++;
xon_off = ComPort.ReadByte(); // should be x_off
error_check = ComPort.ReadByte(); // will give line complete or error
xon_off = ComPort.ReadByte(); // should be x_on
} else if (xon_off == stop_data) {
read_again: xon_off = ComPort.ReadByte();
if (xon_off == send_data) {
goto write_line;
} else {
goto read_again;
}
}
}
My issue is with the flow control (x_on/x_off/eof/etc). The way the current code is, it can send a page error, and the tool keeps sending like it was nothing, so obviously my read/compare statements are off. Can someone help me find out why when it sends a page error, my code thinks it's sending an x_on?
NOTE: x_on is a variable above set to 0x11 and x_off is a var above set to 0x13 just to clarify.
NOTE: Once I get this figured out, my next step is to remove the goto statements...they're gross I know, but they worked for here.
Ok so from what I can understand, here's what the better option is:
First off, do remove the goto statements, they're horrindious and should never be used. As for the issue of not being able to mix chars/hex values, every char when brought in as an int will be assigned it's hex value (convert to int32 in the program). When the bootloader sends the 0x11, it will more than likely send it as a char, which will come into your software as an unprintable ASCII char, but still have the 0x11 if you use your debugger and see what it's actually coming in as. So my suggestion is to do a readbyte, then do a convert to int32 in software, and do a switch statement/state machine for what you want done.
Related
I'm writing a visual studio extension based on the Concord Samples Hello World project. The goal is to let the user filter out stack frames by setting a list of search strings. If any of the search strings are in a stack frame, it is omitted.
I've got the filter working for a hardcoded list. That needs to be in a non-package-based dll project in order for the debugger to pick it up. And I have a vsix project that references that dll with an OptionPageGrid to accept the list of strings. But I can't for the life of me find a way to connect them.
On the debugger side, my code looks something like this:
DkmStackWalkFrame[] IDkmCallStackFilter.FilterNextFrame(DkmStackContext stackContext, DkmStackWalkFrame input)
{
if (input == null) // null input frame indicates the end of the call stack. This sample does nothing on end-of-stack.
return null;
if (input.InstructionAddress == null) // error case
return new[] { input };
DkmWorkList workList = DkmWorkList.Create(null);
DkmLanguage language = input.Process.EngineSettings.GetLanguage(new DkmCompilerId());
DkmInspectionContext inspection = DkmInspectionContext.Create(stackContext.InspectionSession, input.RuntimeInstance, input.Thread, 1000,
DkmEvaluationFlags.None, DkmFuncEvalFlags.None, 10, language, null);
string frameName = "";
inspection.GetFrameName(workList, input, DkmVariableInfoFlags.None, result => GotFrameName(result, out frameName));
workList.Execute();
CallstackCollapserDataItem dataItem = CallstackCollapserDataItem.GetInstance(stackContext);
bool omitFrame = false;
foreach (string filterString in dataItem.FilterStrings)
{
if (frameName.Contains(filterString))
{
omitFrame = true;
}
}
The CallstackCollapserDataItem is where I theoretically need to retrieve the strings from user settings. But I don't have access to any services/packages in order to e.g. ask for WritableSettingsStore, like in You've Been Haacked's Example. Nor can I get my OptionPageGrid, like in the MSDN Options Example.
The other thing I tried was based on this StackOverflow question. I overrode the LoadSettingsFromStorage function of my OptionPageGrid and attempted to set a static variable on a public class in the dll project. But if that code existed in the LoadSettingsFromStorage function at all, the settings failed to load without even entering the function. Which felt like voodoo to me. Comment out the line that sets the variable, the breakpoint hits normally, the settings load normally. Restore it, and the function isn't even entered.
I'm at a loss. I really just want to pass a string into my Concord extension, and I really don't care how.
Ok, apparently all I needed to do was post the question here for me to figure out the last little pieces. In my CallstackCollapserDataItem : DkmDataItem class, I added the following code:
private CallstackCollapserDataItem()
{
string registryRoot = DkmGlobalSettings.RegistryRoot;
string propertyPath = "vsix\\CallstackCollapserOptionPageGrid";
string fullKey = "HKEY_CURRENT_USER\\" + registryRoot + "\\ApplicationPrivateSettings\\" + propertyPath;
string savedStringSetting = (string)Registry.GetValue(fullKey, "SearchStrings", "");
string semicolonSeparatedStrings = "";
// The setting resembles "1*System String*Foo;Bar"
if (savedStringSetting != null && savedStringSetting.Length > 0 && savedStringSetting.Split('*').Length == 3)
{
semicolonSeparatedStrings = savedStringSetting.Split('*')[2];
}
}
vsix is the assembly in which CallstackCollapserOptionPageGrid is a DialogPage, and SearchStrings is its public property that's saved out of the options menu.
alright I'm doing something that should be rather simple, I believe I am overlooking something here.
Alright I and using a HttpWebRequest and a WebResponse to detect if a Robots.txt exists on a server (and that works perfectly fine). However, I am trying to add to do myList.Add(reader.ReadLine()); Which (works). But problem is, it keeps skipping the very first line.
https://www.assetstore.unity3d.com/robots.txt < That is the one I started noticing the problem on (just so you know what I'm talking about). It is just for testing purposes. (Look at that link so you can get an idea as to what I'm talking about).
Anywho, it is also not adding the reader.ReadLine to my list either (first line only). So I'm not exactly understanding what's going on, I've tried looking this up and the only things I'm finding is to purposely want to skip a line, I don't want to do that.
My Code Below.
Console.WriteLine("Robots.txt Found: Presenting Rules in (Robot Rules).");
HttpWebRequest getResults = (HttpWebRequest)WebRequest.Create(ur + "robots.txt");
WebResponse getResponse = getResults.GetResponse();
using (StreamReader reader = new StreamReader(getResponse.GetResponseStream())) {
string line = reader.ReadLine();
while(line != null && line != "#") {
line = reader.ReadLine();
rslList.Add(line);
results.Text = results.Text + line + Environment.NewLine; // At first I thought it might have been this (nope).
}
// This didn't work either (figured perhaps maybe it was skipping because I had to many things.
// So I just put into a for loop, - nope still skips first line.
// for(int i = 0; i < rslList.Count; i++) {
// results.Text = results.Text + rslList[i] + Environment.NewLine;
// }
}
// Close the connection sense it is no longer needed.
getResponse.Close();
// Now check for user-rights.
CheckUserRights();
Image of the results.
Change when next you call the read line
var line = reader.ReadLine(); //Read first line
while(line != null && line != "#") { //while line condition satisfied
//perform your desired actions
rslList.Add(line);
results.Text = results.Text + line + Environment.NewLine;
line = reader.ReadLine(); //read the next line
}
I am developing a .NET program using VSTO 2010 running .NET 4.0 to find a specific subheading in a set of word documents and copy all content under that subheading (say "Requirements") using Word.Interop. I succeeded by means of a for loop that matched words, using which I search for this word and then the starting word of the next section (say "Functionality").
Now the documents also have a contents page so i found that simple word matching wouldn't do as it would return the first seen occurrence which was definitely in the contents section. So I tried finding the second occurrence an was successful but then realized that it could even be that the word might repeat itself much before the subheading. Hence I resorted to finding the sentence. here I was successful here in finding both the words (I had to modify the search string to "Requirements\r" because thats how it was being read)
Anyhow. The problem i am facing now is that after I get the starting and ending sentences, I selected the entire document and using MoveStart and MoveEnd , i reduced down the selection before copying it and pasting it in another word document,(as i dont know about using Range or Bookmark)
However , while i was successful in moving the start and though the end position was correct, the MoveEnd always moves to some text that is at least 10 sentences beyond the actual. I've been at this for 2 weeks now and any help in this matter would be greatly appreciated. I dont mean any disrepect to all the programmers out there in the world.
I've shown the code I'm using.
The variables used are self explanatory.
//SourceApp and SourceDoc - Word application that reads source of release notes
//DestinationApp and DestinationDoc = Word application that writes into new document
private void btnGenerate_Click(object sender, EventArgs e)
{
int startpos = findpos(SourceDoc, 1, starttext, sentencecount);
int endpos = findpos(SourceDoc, startpos, endtext, sentencecount);
object realstart = startpos - 1; // To retain the subheading
object realend = -(sentencecount - (endpos - 1)); // to subtract the next subheading
SourceDoc.Activate();
SourceDoc.ActiveWindow.Selection.WholeStory();
SourceDoc.ActiveWindow.Selection.MoveStart(WdUnits.wdSentence, realstart);
SourceDoc.ActiveWindow.Selection.MoveEnd(WdUnits.wdSentence, realend); // the problematic bit
SourceDoc.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();
string allText = data.GetData(DataFormats.Text).ToString();
DestinationDoc.Activate();
DestinationDoc.ActiveWindow.Selection.WholeStory();
DestinationDoc.ActiveWindow.Selection.Delete();
DestinationDoc.ActiveWindow.Selection.Paste();
DestinationDoc.Save();
((_Application)SourceApp).Quit();
((_Application)DestinationApp).Quit();
textBox1.AppendText(allText);
}
int findpos(Document docx, int startpos, string txt, int sentencecount)
{
int pos = 0;
string text;
for (int i = startpos; i <= sentencecount; i++)
{
text = docx.Sentences[i].Text;
if (string.Equals(text, txt))
{
pos = i;
break;
}
}
return pos;
}
I would also be extremely grateful if there was a way to extract specific subheading only (like 3.1 , 5.2.3 etc.) which is what I'm trying to achieve. The question is just my way of doing things and I'm open to a better way as well.
Many thanks in advance.
i'm having trouble searching a virtual listview in c#.
what i am doing right now is reading a large log file. here is what i have implemented so far
I read the file one line at a time and note the position of the start of the line. Add these positions to a List and when RetrieveVirtualItem is called - look up the position in the file using the index of the item and the List and then read the line from file.
So there is no lag when reading the file.
I want to now search for items. Here is what I have thought of so far, but I have not been able to implement it successfully.
I am not actually searching the listview, but during my filereading, i mark the position of hits of the specific string, say "INFO". if it hits, i add the position to a List.
When RetrieveVirtualItem is called, I just read the line back with all the hits.
Seems that through debugging - if (line.IndexOf("INFO", StringComparison.OrdinalIgnoreCase) >= 0) is not hitting any matches. I'm not sure why, anyone help?
using (var sr = new myStreamReader("test.log"))
{
while ((line = sr.ReadLine()) != null)
{
if (line.IndexOf("INFO", StringComparison.OrdinalIgnoreCase) >= 0)
{
position = sr.BytesRead;
Search.Add(position);
searchcount++;
}
}
newMessageView.VirtualListSize = searchcount;
}
I don't know how your "myScreamReader" class works, but if it similar to StreamReader this code could be working for you:
using (var sr = new StreamReader("test.log"))
{
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
if (line.IndexOf("INFO", StringComparison.OrdinalIgnoreCase) >= 0)
{
// line contains "info"
}
}
}
You should debug your code by setting a breakpoint at line.IndexOf... check to see if value of line is changing and look correct.
Please can anyone help me? I search some example how can i get information about speeching text in TTS through SAPI (I am programming my aplication in C# but it is not needed, SAPI is the same in C++, etc.)
Information what I need is for example:
User will write in textbox:
"This is a Text"..
tts.Speak("This is a text"); // this will "read" it..
ok, nice... but I need too get informations about "timing"..
for example:
"Th" (first sound (phoneme) of "This") was "read" in 0.01ms..
"i" (first sound of "is") was "read" in 0.5ms..
"e" (second sound of "Text") was "read" in 1.02ms..
when I save the .wav file generated by SAPI, I need to get information about the timing in the .wav for subsequent "processing" of the wav file.
Sorry for my english and sorry for my bad description of my problem but the problem is i think very simple and all will understand it. If not I will try to describe the problem again :) ^^..
I have used C++ and SAPI 5.1 to synthesize speech and have a virtual character move its lips accordingly. Here is some code that works with visemes. According to the documentation at http://msdn.microsoft.com/en-us/library/ms720164(v=vs.85).aspx, phonemes work the same, except replace SPEI_VISEME with SPEI_PHONEME.
DWORD WINAPI Character::sayMessage(LPVOID lpParam){
HRESULT hres;
try{
::CoInitialize(NULL);
ThreadParam * param = (ThreadParam *)lpParam;
wstring s = param->message;
//first check the string for null
if (s == L"") return false;
//http://msdn.microsoft.com/en-us/library/ms720163(VS.85,classic).asp is my source for this
//set up text to speech
//get the voice associated with the character
ISpVoice * pVoice;
pVoice = param->sceneObject->characterVoice;
if (pVoice != NULL){
pVoice->Speak( NULL, SPF_PURGEBEFORESPEAK, 0 );
SPEVENT event;
ULONG ul;
pVoice->SetInterest(SPFEI(SPEI_VISEME)|SPFEI(SPEI_END_INPUT_STREAM),SPFEI(SPEI_VISEME)|SPFEI(SPEI_END_INPUT_STREAM));
pVoice->SetNotifyCallbackFunction(&eventFunction,0,0);
pVoice->WaitForNotifyEvent(INFINITE);
if (param->sceneObject->age == CHILD){
s = L"<pitch middle=\"+10\">" + s + L"</pitch>";
}
hres = pVoice->Speak(s.c_str(),SPF_ASYNC,NULL);
bool isDone = false;
while(!isDone && pVoice != NULL && !FAILED(hres)){
if(pVoice->GetEvents(1,&event, &ul) == S_OK){
if(event.eEventId==SPEI_VISEME){
//get the viseme
int vis = LOWORD(event.lParam); //handle it however you'd like after this
}
else if(event.eEventId== SPEI_END_INPUT_STREAM){
isDone = true;
s = L"";
return true;
}
}
}
}
}
catch(...){
return false;
}
return !FAILED(hres);
}