|
Use the line number to identify the function where the error occured and check the code of that function.
I don't want to count lines and your posted code might be out of sync, but the RoomCollides function does not return a value on all pathes:
bool RoomCollides(DRoom r)
{
foreach (DRoom r2 in rooms)
{
if(r.CollidesWith(r2))
{
return true;
}
}
}
|
|
|
|
|
Jochen has probably sorted that one fro you, but VS does make it really easy to find where an error is:
1) In the Errors pane, you can double click on an error and VS will take you directly to the offending line.
2) If you press CTRL+G, VS will popup a "go to line number" dialog allowing you to specify exactly which line in the file you want to look at.
When you get a compiler error, they are normally in reasonable english and pretty explanatory: "not all paths return a value" is pretty clear and self explanatory: not all possible routes through the current function reach either a return or a throw statement. You should be able to work out from that what is missing. Most other errors are equally understandable - just look at the line above if it isn't obvious, because sometimes missing out a semicolon, or a comma, or an open bracket, etc. can mean that the problem isn't detected until the following line.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I have two issues with speech recognition in C#.
I think I have installed all necessary packages for speech recognition, what I know of:
* Microsoft Server Speech Platform runtime (x64)
* Microsoft Speech Platform SDK (x64) v11.0
* Microsoft Server Speeech Recognition Language - TELE (en - GB)
* Microsoft Server Speeech Recognition Language - TELE (en - US)
* Microsoft Server Speeech Recognition Language - TELE (sv - SE)
I'm from Sweden so my Win7x64 Home Premium is set to Swedish - Sweden. So I think my culture is sv - SE. (Keep that in mind if culture may cause my problems).
I copied the code from this page into a console application, added a reference to system.Speech.Recognition and it compiled.
Problem 1
When I ran it it crashed on this line:
foreach (RecognizerInfo ri in SpeechRecognitionEngine.InstalledRecognizers()) A NullReferenceException was thrown.
So I'm stuck there.
Problem 2
If I comment away the foreach loop for the RegognizerInfo and change the using statement to:
using (SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine()) It throws an UnauthorizedAccessException on this line:
recognizer.LoadGrammar(new DictationGrammar()); I ran VS2015 as administrator and I got the real exception: PlatformWasNotSupported. The exception says: "No interpreter installed" or "No recognizer installed". (I'm translating Swedish error messages to English in the best way I can).
So I'm stuck there as well.
Any suggestions what to no next?
|
|
|
|
|
Hi community,
so I am trying to insert the data from one database to another. My problem is I cannot insert the data with my data adapter. So following Code I am having right now:
SqlDataAdapter dadapter = new SqlDataAdapter() { AcceptChangesDuringFill = false };
dadapter.SelectCommand = new SqlCommand("SELECT X AS A, Y AS B FROM Tab1", cn1);
DataTable dt = new DataTable();
dadapter.Fill(dt);
OracleDataAdapter da = new OracleDataAdapter("SELECT A, B from Tab2", connOracle)
{
AcceptChangesDuringFill = false
};
OracleCommandBuilder cb = new OracleCommandBuilder(da);
da.Update(dt);
Im getting the error, that I need to to have an update statement for an insert.
But even when I try to add
da.InsertCommand = new OracleCommand("SELECT A, B from Tab2", connOracle);
it doesnt show any error but I it doesnt insert into my oracle db. Anyone knows what to do?
Thanks!
|
|
|
|
|
You are using a SELECT command, which reads from the database: you need either INSERT or UPDATE .
|
|
|
|
|
Member 13466176 wrote:
da.InsertCommand = new OracleCommand("SELECT A, B from Tab2", connOracle);
Erm, don't you think the insert command should perhaps insert something, rather than select something?
Something like this should work:
da.InsertCommand = new OracleCommand("INSERT INTO Tab2 (A, B) VALUES (:A, :B)", connOracle); (Assuming I've got the Oracle parameter syntax right...)
NB: You'll need to change all of the rows in your source table to the "Added" state so that the DataAdapter knows what to do with them.
...
dadapter.Fill(dt);
foreach (DataRow row in dt.Rows)
{
row.SetAdded();
}
...
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi Richard,
doesnt the CommandBuilder automatically convert the SELECT statement into an Insert statement when I call da.Update(dt)?
Anyways, could you tell me how to bind the columns of the dataset dt to a parameter like you showed?
Thank you!
|
|
|
|
|
Member 13466176 wrote: doesnt the CommandBuilder automatically convert the SELECT statement into an Insert statement when I call da.Update(dt)?
IIRC, it should do. But the error you're getting suggests that's not working for the OracleCommandBuilder for some reason.
Member 13466176 wrote: Anyways, could you tell me how to bind the columns of the dataset dt to a parameter like you showed?
It's a while since I used a DataAdapter to modify data, but something like this should work:
da.InsertCommand = new OracleCommand("INSERT INTO Tab2 (A, B) VALUES (:A, :B)", connOracle);
var p = da.InsertCommand.CreateParameter();
p.ParameterName = "A";
p.SourceColumn = "A";
p.SourceVersion = DataRowVersion.Current;
da.InsertCommand.Parameters.Add(p);
p = da.InsertCommand.CreateParameter();
p.ParameterName = "B";
p.SourceColumn = "B";
p.SourceVersion = DataRowVersion.Current;
da.InsertCommand.Parameters.Add(p);
da.Update(dt);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
YOU ARE THE BEST!!!!
It worked!
I thank you alot, Richard
|
|
|
|
|
Hi,
Im trying to send "1C0" thru serial port to a device, and the device is responding and are sending back "52" in TeraTerm.
But with this code, the device is responding to the sent request, but I get this error and the answer of "52" is not shown in the program.
What is wrong?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ArduinoSerialTemp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
serialPort1.BaudRate = 9600;
serialPort1.PortName = "COM44";
serialPort1.Open();
serialPort1.Write("*1C0");
serialPort1.Write("\r");
serialPort1.DataReceived += serialPort1_DataReceived;
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
string line = serialPort1.ReadLine();
this.BeginInvoke(new LineReceivedEvent(LineReceived), line);
}
private delegate void LineReceivedEvent(string line);
private void LineReceived(string line)
{
textBox1.Text = line;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort1.IsOpen) serialPort1.Close();
}
private void serialPort1_ErrorReceived(object sender, System.IO.Ports.SerialErrorReceivedEventArgs e)
{
serialPort1.Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
|
|
|
|
|
Without running your code, we can't really tell - but at a guess it's the ReadLine call that is timing out that causes the problem. You send just a '\r' as the "end of line" code, so it's possible that the device response is not terminated with what the system regards as a newline. Me? I'd read the data that is available, and store it in a buffer instead of trying to read a line. That way, I get to process the data any way I need, and can look at exactly what is being returned.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
In which line of your code does the error occur?
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
It happens in this line:
string line = serialPort1.ReadLine(); ;
|
|
|
|
|
Hi,
serial ports are tricky; and SerialPort documentation is missing a lot of info. I have lots of experience with SerialPort, and I happen to have read four articles about them just yesterday (more later).
I don't know for sure what is wrong in your situation, however I'll give you some pointers to critical aspects. I do expect you have two different issues, a receive issue and a close issue, and they are probably linked.
1. Encoding: as long as everything is ASCII (i.e. bit7 is zero in every byte), all is well. Whenever you need 8-bit (or 16-bit or 32-bit) characters, make sure SerialPort.Encoding is set correctly, it is used in Read() and Write(), unless you use the byte[] ones. Do not trust the default encoding.
You can use ISO 8859-1 for pass-thru:
port.Encoding=Encoding.GetEncoding(28591);
2. EndLine: you don't use WriteLine() yet you use ReadLine()? That is odd. And may well indicative to the cause of your receiver problem: whatever SerialPort.EndLine is set to will be sent as terminator in WriteLine(), and will be looked for in the incoming data to determine when ReadLine() gets satisfied.
Since you did not use WriteLine() I am guessing you didn't like the endline terminator, but yet you are waiting for it to be sent in DataReceived???
It is very well possible tht your peripheral uses ASCII CR (you send it explicitly!) but never sends ASCII LF, and if the default EndLine is set to CR-LF, ReadLine will never return a thing...
Remember, a terminal emulator isn't very good at reporting non-printable characters such as CR and LF
3. The DataReceived event: this is the most misunderstood event I ever saw; people seem to think it will fire on every incoming byte. It won't. People like to think it will fire after every incoming message (whatever that means), it doesn't. Here is the one correct statement: provided some data got received (any number of bytes), it will fire at least once; everything else is speculation.
As a result, when your peripheral sends three bytes ("5" "2" CR) you might get two DataReceived events, one with data "5", the other with "2" CR.
4. the DataReceived handler should not be blocking; most examples correctly consume "whatever is available" and make sure the handler is done right away, so the thread gets freed, and a new DataReceived event can be processed. (Behind the scenes, SerialPort made sure no two handlers can be active at the same time, someting they fail to tell explicitly).
Your handler does the opposite: it contains a blocking ReadLine().
There is another weakness: as you display the datain a TextBox, you'll see at most one line, so if [5 2 CR LF CR LF] were to be received, you would see nothing, the empty line would overwrite the 52 right away.
The correct way to handle DataReceived in general is like so:
- create a buffering scheme (it will need a lock!)
- add incoming data to the buffer; this should be the only data manipulation inside the DataReceived handler;
- make sure the lock is mostly free, if not your handler becomes blocking which it shouldn't!
- create a way to process the buffer, do this outside the DataReceived handler (in a background thread), possibly signalled by the DataReceived handler.
In specific cases, a much simpler approach can work just fine. A simple case would be: only short messages (with known upperbound lenght) and a guaranteed gap in between messages. If so, I would create a DataReceived handler that is very blocking: it starts with a Thread.Sleep to make sure the message is completely received, then process it right away (preferrably with Read, not ReadLine, as that may block forever).
5. Closing the port. Quite often one has a permanent receiver, your program wants to catch whatever comes in, whenever it does. That makes it harder to close the port, as you cannot perform a Close() while a Read or Write is in progress. That would throw the exception you are getting. Quite often the solution is not to close the port at all. Or set a timeout on the port so a Read doesn't block forever, and wait for at least that time just before closing the port (don't expect the timeout to work accurately though, it can be of a lot). Of course, a receiver with a timeout, will detect a "no data" situation and may be inclined to launch another Read, a boolean should prevent that when you intend to close the port. I also have seen, but never felt the need for, code that created a background thread just to close the serial port (catching and ignoring any exception when that fails).
6. Thread safety: the doc is a joke; statics are thread safe, and the other methods aren't. If true, SerialPort would be useless, e.g. Read and ReadLine would not be allowed in the DataReceived handler!!!
In summary: it is my guess your ReadLine fails on a mismatched EndLine situation, and your program bombs on closing the port while a read is in progress.
What I tend to do except for the simplest cases (this was true long before .NET, the native serial port has similar pitfalls):
- use binary port operations (i.e. byte[]) rather than text as much as possible;
- use an explicit endline, i.e. avoid using ReadLine and WriteLine;
- use a dedicated background thread to periodically consume (i.e. move to my buffer) incoming data, foregoing DataReceived alltogether.
Two extra suggestions:
- it doesn't hurt putting difficult code inside a try-catch construct, and reporting any exception that might occur.
- I tend to always log exceptions to a text file, saves lots of time while developing a program, and helps in diagnosing a problem later on too.
Hope this helps.
Additional reading:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/e36193cd-a708-42b3-86b7-adff82b19e5e/how-does-serialport-handle-datareceived?forum=netfxbcl
https://blogs.msdn.microsoft.com/bclteam/2006/10/10/top-5-serialport-tips-kim-hamilton/
https://blogs.msdn.microsoft.com/bclteam/2006/05/26/serialport-encoding-ryan-byington/
https://blogs.msdn.microsoft.com/bclteam/2006/05/15/serialport-and-datareceived-event-ryan-byington/
modified 12-Jan-18 11:33am.
|
|
|
|
|
Luc,
Thanks for the help, it start working when I used
WriteLine
|
|
|
|
|
So that tells me:
1. the peripheral is doing something similar to ReadLine and never received a complete command!
2. your terminal emulator, which worked well with your peripheral, must be sending CR LF upon hitting Enter; that too isn't always true for terminal emulators! It probably is a program's setting.
Glad you got it solved.
Mind you, you still could get into trouble with the Close issue, e.g. when something goes wrong (e.g. the peripheral misbehaves or a cable insertion/removal) a character might be received without a full message, hence DataReceived fired and ReadLine() waiting forever, hence Close throwing.
General advice:
- increase observability by adding LEDs to external hardware, and logging to code in general;
you then might have noticed the command never got accepted;
- code defensively, with try-catch, timeout, etc.
- don't assume anything that isn't documented.
Have fun
|
|
|
|
|
How can I center the PictureBox (or other control) to the parent (or form) direct from the code? I've tried so much, but I'm not successful, I hope someone can help, grateful
modified 11-Jan-18 18:33pm.
|
|
|
|
|
WPF or Windows Forms?
This space for rent
|
|
|
|
|
|
You could start by NOT SHOUTING AT US.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Sorry for that, my native language is not english, so my writing could look a bit rude
|
|
|
|
|
With thanks to Pete O'Hanlon and OriginalGriff:
You can use this method to center a control in its parent control:
private void CenterControl(Control control)
{
control.Left = (control.Parent.ClientRectangle.Width - control.Width) / 2;
control.Top = (control.Parent.ClientRectangle.Height - control.Height) / 2;
}
example use:
CenterControl(pictureBox1);
|
|
|
|
|
I will test it on my project, my thanks to you and Pete O'Hanlon and OriginalGriff
|
|
|
|
|
New to C#,
The plan:
1. Parse a log file of over 250,000 records daily.
2. Storage the outcome of the file parsing somewhere (file, Array, DB, other), the average # of records is about 20,000.
3. Use a DataGrid to bind the result of the parsing and present to user.
I will like some suggestion on #2 (Pros/Cont, performance, etc.), the temporary storage to be bind to the Datagrid. Keep in mind the number of records could be 25K or more.
Thanks in advance.
|
|
|
|
|
I'd probably use a DataTable - they are designed for exactly that - or a List of a custom class.
But I wouldn't present 25,000 rows of anything directly to a user: that's just silly. Page it, filter it, search it: but just showing him the data and letting him pick the bones out is a recipe for unhappy users...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|