|
So you just need to keep reading until you have all the data. As OriginalGriff points out below, the data does not all arrive at once.
|
|
|
|
|
Only the manufacturer of 41 bytes will deliver the future in a single time.
If it does not come in one go as you have stated, how can I get it in the order of the results?
So how do I get to 8 + 8 + 8 + 8 in order of incoming results?
|
|
|
|
|
Serial devices present their data in "chunks", and each chunk may be anything from a single byte to a complete message. This is a feature of serial IO that the programmer needs to understand and deal with. Your input routine needs to capture however much data is presented each time, and build the message buffer by appending the latest 'chunk' to whatever has already been read in. When you are certain that you have all the available data you can go and process it. If the manufacturer tells you that you will receive 41 bytes, then you must keep reading until your message has received that number of bytes.
|
|
|
|
|
The Problem I'm Having Problem With, What's The Value 8 Bytes Coming Up and How Can I Keep Reading the Restored Values? How Do I Write a Code?
|
|
|
|
|
ibrahimayhans wrote: How Do I Write a Code? First you need to understand the problem.
Set an index to the start of your receive buffer
Do
Read some data using the index to position it at the correct point in the buffer
Add the count of bytes read to the offset index
While index < 41
// when the index reaches 41 you have all the data (bytes 0 to 40)
|
|
|
|
|
Assuming you even hooked up the "data received handler", that "handler" in invoked on an "open" port.
The "open / close" logic in the handler (and elsewhere) implies that your whole approach is "faulty"; and that "printing a byte" is the least of your problems.
You need to (re)read the VS docs for "Serial port".
(Unless it's some weird "virtual" port; then you need to refer to those docs).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Control.CheckForIllegalCrossThreadCalls = false;
Why?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I have used the Thread Function, and now Delete
|
|
|
|
|
Doesn't matter - the DataReceived event is never fired on the UI thread, so you will get an cross thread exception anyway. See what I mean about needing to know this stuff?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi,
serial communication can be very hard, and it can be quite easy; it all depends on circumstances: how high the transmission rate is, how big the gap is in between messages, etc.
Here are three pointers, assuming the peripheral uses printable text:
1. Always use a terminal emulator to get acquainted with your peripheral's behavior. It will help you in checking the hardware aspects and overall settings of your port, and it will help you in understanding the message flow.
2. the DataReceived event fires whenever it wants, which could be after receiving 1 byte, or 39 bytes, or whatever. It does not care about what you expect as a message, it only knows about individual bytes and several levels of buffering inside Windows. As a result it isn't very useful except in simple cases:
If your messages are far apart, which gives you some time to waste, the easiest approach is to get started by the DataReceived event, then wait (with Thread.Sleep ) sufficiently long so the entire message should be received by now, and then use ReadExisting. The required delay equals maximum string length * character time, which obviously depends on the baud rate used (say 1 msec at 9600 baud). Warning: Windows timing isn't always very accurate, so add a spare 20 msec or so.
3. The DataReceived event gets handled on an arbitrary thread (actually one from the ThreadPool) but absolutely NOT on the GUI thread (read more: Asynchronous operations run on ThreadPool threads[^]); therefore, you are not allowed to touch any of your GUI Controls inside the DataReceived handler.
The proper solution is by using Control.InvokeRequired and Control.Invoke (read more: Invalid cross-thread operations[^]). NEVER use Control.CheckForIllegalCrossThreadCalls
In more complex situations I tend to avoid the DataReceived event, instead I'd use an explicit Thread and perform blocking reads and a buffering scheme. Sometimes a BackgroundWokrer can do the job.
Hope this helps.
PS: Don't trust the default settings of SerialPort, always explicitly set properties such as Encoding, NewLine, etc.
|
|
|
|
|
I am not a c# developer. I am creating an ETL in Visual Studio 2017 and trying to add a scripted transformation container to strip the /'s out of a name field. VS creates the base code and tells you where to add the code.
This is the beginning:
egion Help: Introduction to the Script Component
#endregion
#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
#endregion
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
#region Help: Using Integration Services variables and parameters
#endregion
#region Help: Using Integration Services Connnection Managers
#endregion
#region Help: Firing Integration Services Events
#endregion
public override void PreExecute()
{
base.PreExecute();
}
Where it says add your code here I have tried multiple suggestions I have found online, but it doesn't like the syntax. I don't know if I have to declare the field, or what I'm missing. The most promising looked like the Regex function, but it will not recognize it regardless of how I type it. Any help here would be extremely appreciated!
|
|
|
|
|
|
Assuming you've define an appropriate user variable in the ssis package, look at this:
Use SSIS Variables and Parameters in a Script Task - TechNet Articles - United States (English) - TechNet Wiki[^]
Once you get the current value of the variable, you can simply do this to remove all the "/" characters:
myVariable = myVariable.Replace("/", "");
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
https://ibb.co/iZmhUn
Dear all I can not handle this. Can you help me?
Thanks
Form1 has combobox and a button to open Form 2
Form 2 has textbox and a button to OK&Exit
I want to add text value from Form2 to Form1 combobox.
|
|
|
|
|
|
The library of L-Card release similar COM interface. The function CreateLInstance return pointer for class C++.
In C++ is
pModule = static_cast<ILE140 *>(CreateLInstance("e140"));
where
struct ILE140 : public ILUSBBASE
{
virtual BOOL WINAPI GET_ADC_PARS(ADC_PARS_E140 * const AdcPars) = 0;
virtual BOOL WINAPI SET_ADC_PARS(ADC_PARS_E140 * const AdcPars) = 0;
virtual BOOL WINAPI START_ADC(void) = 0;
.......
}
and
struct ILUSBBASE
{
virtual BOOL WINAPI OpenLDevice(WORD VirtualSlot) = 0;
virtual BOOL WINAPI CloseLDevice(void) = 0;
virtual BOOL WINAPI ReleaseLInstance(void) = 0;
virtual HANDLE WINAPI GetModuleHandle(void) = 0;
virtual BOOL WINAPI GetModuleName(PCHAR const ModuleName) = 0;
virtual BOOL WINAPI GetUsbSpeed(BYTE * const UsbSpeed) = 0;
virtual BOOL WINAPI GetLastErrorInfo(LAST_ERROR_INFO_LUSBAPI * const LastErrorInfo) = 0;
};
How can this be implemented on C#?
Function CreateLInstance can declarated as
[DllImport(@"Lusbapi.dll")]
static extern IntPtr CreateLInstance(string devname);
But how to declare an ILE140 class and convert a type IntPtr to ILE140?
|
|
|
|
|
Does anyone have a SIMPLE implementation of WeakEventHandler?
I've been Googling but all I see to find is the interfaces or some REALLLLY long complicated code.
I did find this MSDN page but the code doesn't compile:
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
modified 26-Mar-18 16:53pm.
|
|
|
|
|
Are you looking to write your own WeakEventHandler implementation or could you just use one of the pre-built ones, listed in the inheritance tree, here[^]?
The code probably doesn't compile because the example is not meant as a copy'n'paste example. You have to replace certain works in the source with either your own implementations or with interface names of the event type you're looks for. You'd probably even have to extend WeakEventManager with your own implementations of certain methods, depending on what you're doing.
There are no "simple" examples of this thing. About the only thing I could suggest is looking at the .NET Reference Source for a similar implementation of what you're trying to do and using that as a framework to write your own code. For example, the source for PropertyChangedEventManager can be found here[^].
modified 27-Mar-18 10:48am.
|
|
|
|
|
I'm looking to create an event aggregator and it seemed like a weak even manager is the way to go. I don't know anything about it so I'm looking for a simple example I can study.
For example, I know that Prism uses one, but Prism is a beast. WAY too over-engineered.
Again, I just want to put together something simple for handling events in my app.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
"an event aggregator"
You're going to have to explain what that means, exactly.
|
|
|
|
|
Event Aggregator
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
OK, I see what you're getting at. Unfortunately, I've never seen a "simple" implementation of this. What I've done in the past is something very similar to the implementation found in the Prism library. The latest source for the EventAggregator in there can be found here[^].
|
|
|
|
|
Thanks. I'll take a look
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
void FillCombo()
{
try
{
DateTime MONDAY = DateTime.Now;
DateTime FRIDAY = DateTime.Now.AddDays(5);
string query = "SELECT orderID,FinalDate,StartDate FROM Tbl_Auftrag Where [StartDate] BETWEEN '" + MONDAY + "' AND '" + FRIDAY + "' AND [FinalDate] BETWEEN '" + MONDAY + "' AND '" + FRIDAY + "'";
con.Open();
SqlCommand cmdDataBase = new SqlCommand(query, con);
SqlDataReader rdr = cmdDataBase.ExecuteReader();
while (rdr.Read())
{
String sName = rdr["orderID"].ToString();
listBox1Montag.Items.Add(sName);
listBox2Dienstag.Items.Add(sName);
listBox3Mittwoch.Items.Add(sName);
listBox4Donnerstag.Items.Add(sName);
listBox5Freitag.Items.Add(sName);
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
con.Close();
}
}
Hello ,
I have a problem and I dont know how to solve it. With this code I can see all orderID's between today and in the next five days. But what if for example orderID: 234324 , StartDate = 26.03.2018, FinalDate = 27.03.2018 exists.... I should only see it in the listbox Monday and Tuesday. But its everywhere ... -.-
I know why... but not how to change that :/
maybe someone can help me?
|
|
|
|
|
Start by not doing that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood' The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable; Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x'; A perfectly valid SELECT
DROP TABLE MyTable; A perfectly valid "delete the table" command
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.
So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
In this specific case it doesn't expose you to SQL Injection, but it does bring up a different probable problem which may be causing what you have found. When you concatenate strings, you cause an implicit ToString on your DateTime objects which will convert them using the default culture for the computer that is running that code. In most production systems the SQL server is a separate computer (which may not even be in the same country, let alone LAN segment) and that computer may well be configured for a different default date format. So when SQL parse your string, it can very, very easily convert it wrong: you supply dd/MM/yyyy and it reads MM/dd/yyyy for example.
So go through the whole of your app and fix it: remove all string concatenations, replace them with parameterized queries, and see if your problem disappears at the same time...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|