|
ya i took those two lines of code but i dont know how to use it.
Can you give me any example how to use it......plzzzzzz.
|
|
|
|
|
Hi everybody,
I have a simple project with a form and a class in another file to manage the COM port (from the article "Smart Device Print Engine for Compact Framework" by Orkun GEDiK).
I open the COM port and I start its thread for receiving data.
I want to write the received data in a textbox in the form.
To do this, every byte I receive, I assign the value of this byte to a byte variable of the form called byt and I call from inside the loop of the thread a function in the form that stores all the bytes in a string dati (in hexadecimal format):
Public Sub ScriviTesto2()
Dim str As String
If InvokeRequired Then
Invoke(New Action(AddressOf ScriviTesto2))
Return
End If
Try
str = Hex(byt)
dati &= str & " "
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
This seems to work because if I run the code step by step I see that the string dati grows byte by byte and contains the correct values.
When there are no more bytes to receive, always from inside the loop of the thread, I call another function of the form that should write my string in a text box:
Public Sub ScriviTesto3()
If InvokeRequired Then
Invoke(New Action(AddressOf ScriviTesto3))
Return
End If
TB1.Text &= dati & vbCrLf
End Sub
...this function write nothing in the text box... why?
The string data contains the right values, is not empty.
I tried another way: I don't call the last funcion (ScriviTesto3() ) but, when there are no more bytes to receive, I click a button of the form that do the same:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TB1.Text &= dati & vbCrLf
End Sub
In this case, when I click the button the string dati is empty... I don't know why.
I add the declaration of the involved variables:
Public Class Form1
Public byt As Byte
Protected dati As String
...
Can anyone help me?
Thanks in advance.
|
|
|
|
|
steve_9496613 wrote: I call another function of the form that should write my string in a text box: Pass the data to display there as a parameter.
steve_9496613 wrote: In this case, when I click the button the string dati is empty... I don't know why. Access to the form's field isn't thread-safe. You could lock it in a property, but that slow down things nicely. Hence, I'd suggest to create a list of bytes on your receiver-thread, and to pass this as a paramater to the form, say, once a second.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Thanks Eddy.
I have modified the first function as you suggested to do for the second one:
Public Sub ScriviTesto2(ByVal dato As Byte)
Dim str As String
If InvokeRequired Then
Invoke(New Action(Of Byte)(AddressOf ScriviTesto2), dato)
Return
End If
Try
str = Hex(dato)
dati &= str & " "
TB1.Text &= Hex(dato) & " "
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
In the receiver thread I call this function every byte I receive, passing to it the byte just received.
As before the string dati grows byte by byte so I can say that I correctly pass the parameter dato to this function but nothing is written in the text box TB1 .
Writing byte by byte in a textbox is not the fastest way but I just wanted to see if passing data as parameter to the function could be the solution.
Perhaps the text box is not the right objet to use or, more likely, I'm missing something important.
I did another try: as you suggested I created a byte array in the receiver thread, I fill it with the bytes received and at the end of data reception I call another function in form1 passing to it the byte array:
Public Sub ScriviTesto4(ByVal datiIn As Byte())
Dim i As Int32
If InvokeRequired Then
Invoke(New Action(Of Byte())(AddressOf ScriviTesto4))
Return
End If
For i = 0 To (datiIn.Count - 1)
TB1.Text &= Hex(datiIn(i)) & " "
Next
TB1.Text &= vbCrLf
End Sub
Running step by step in debug I see that the array datiIn contains the right bytes and also the TB1.Text contains the right values but I see nothing in the text box in the form!
What I'm doing wrong?
|
|
|
|
|
steve_9496613 wrote: Perhaps the text box is not the right objet to use or, more likely, I'm missing something important. It's not optimal, but it should display some text. That's what it's there for.
steve_9496613 wrote: Running step by step in debug I see that the array datiIn contains the right bytes and also the TB1.Text contains the right values but I see nothing in the text box in the form! Did you create the textbox on the mainthread?
For i = 0 To (datiIn.Count - 1)
string x = Hex(datiIn(i)) & " "
TB1.Text = TB1.Text + x
Next Can you write the string x to the debugger?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: Did you create the textbox on the mainthread? I added the textbox to the form1 in the designer, it should be in the main thread.
In form1 I declared an instance of the class to manage the COM:
Dim NewCom As CommDev
In the Load event of the form I create the new object:
NewCom = New CommDev()
In the New function of the class some parameters are set, like baud rate, parity, etc.
When I open the COM port, I call an Init function that opens the COM port, sets buffer sizes, sets the device control block, sets timeouts, creates and starts the receiver thread.
Eddy Vluggen wrote: Can you write the string x to the debugger? I used your code instead of mine and I can see the value of the string x , I mean that I can "add an expression control" (I don't know if this is the right translation of the italian "Aggiungi espressione di controllo" that appear in the right click menu over a variable during debug) to the string x and see its right value in the bottom windows of Visual Studio.
Nothing appears in the textbox...
I made also another try:
Public Sub ScriviTesto4(ByVal datiIn As Byte())
Dim i As Int32
If InvokeRequired Then
Invoke(New Action(Of Byte())(AddressOf ScriviTesto4))
Return
End If
For i = 0 To (datiIn.Count - 1)
TB1.Text &= Hex(datiIn(i)) & " "
Next
ScriviLabel(TB1.Text)
TB1.Text &= vbCrLf
End Sub
Private Sub ScriviLabel(ByVal stringa As String)
Label1.Text = stringa
End Sub
What I can see in the debug is that TB1.Text , stringa and Label1.Text all contains the right values but nothing appear in the window of my application.
It seems to be a "visualization" problem, perhaps related to threads, but I'm not able to see the error.
|
|
|
|
|
Can you add another label on the form?
Private Sub ScriviLabel(ByVal stringa As String)
Label1.Text = "Hello world"
Label2.Text = stringa
End Sub
This is the point where I'd paranoidly check the colour of my font.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi Eddy,
thanks for your efforts.
I add the second label but no label takes the right content in the form; in the debug the Text properties contain my string and the "Hello world" string.
I also call the Refresh method of labels and form but nothing changes.
Before I did another project where I used the SerialPort object of .NET, everything was in the form1 and I used the DataReceived event of the COM to write in the textbox the received data, always using the Invoke method, and everything was OK.
In this project I don't use the SerialPort object but the API in coredll.dll (CreateFileW , ReadFile , SetCommMask , etc.) because I need to catch different information from the COM port and all this is in a separate class/file.
This is the main difference between the two projecs, in addition to the fact that one works and the other not...
Sorry but I really don't know what is THE fundamental information I'm missing to solve this problem, if you have any questions, please feel free to ask.
|
|
|
|
|
Greetings,
I have a text file that include english characters and arabic characters as well..These characters are mixed with each others..
I have to read this file chars by chars including even the spaces..Though I used the following code but it didn`t get me the write result:
Dim objReader As New System.IO.StreamReader(pfile)
Dim strAll As String
strAll = objReader.ReadToEnd
In this code I read all the content of the file; But unfortunately the length of the Varaible strALL was less than the number of chars in the file...
Please help me to read this file properly in order to store it in a database correctly...
Thanks in advance..
|
|
|
|
|
dgthecodeproject wrote: But unfortunately the length of the Varaible strALL was less than the number of chars in the file... How did you determine the amount of chars in the file?
Download Notepad++ and check the encoding of your file. Assuming it's saved in Unicode, below should work;
Console.WriteLine(System.IO.File.ReadAllText(pfile, Encoding.Unicode))
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi All,
I have a web application called Web, that is referencing an assembly called Lab, lab is using cplustools.dll and Calinx.dll assemblies.
When we run the application both the dlls are going to be copied in to the web applications bin folder. Upto this its normal. But the Web application throws an error as below. And when we go to the web applications bin folder and remove the "cplustools.dll" dll file and refresh the browser the application runs normally. I am not sure why we need to use cplustools.dll and Calinx.dll
Libraries. But the most important thing here for me is, how can I avoid deleting the cplustools.dll from bin folder and run the application directly without deleting it. What is causing this problem I am unable to understand. I ran the cplustools.dll with assembly walker application to find the dependencies. There is one warning with delay loading of IRFrame.dll except that nothing seems wrong.
Could the delay loading of IEFrame.dll be a problem, please give your advice any kind of help a link, or an advice or snippet, anything would be helpful. Thank you very much in advance.
I am not understanding what could be the reason for this error. Please help me in resolving the error. Any kind of help any link is very helpful. Please help me I am in urgent need. Thanks in advance.
Server Error in '/BPN' Application.
--------------------------------------------------------------------------------
The specified module could not be found. (Exception from HRESULT: 0x8007007E)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.FileNotFoundException: The specified module could not be found. (Exception from HRESULT: 0x8007007E)
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[FileNotFoundException: The specified module could not be found. (Exception from HRESULT: 0x8007007E)]
System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection) +0
System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection) +43
System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +127
System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +142
System.Reflection.Assembly.Load(String assemblyString) +28
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +46
[ConfigurationErrorsException: The specified module could not be found. (Exception from HRESULT: 0x8007007E)]
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +613
System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +203
System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +105
System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +178
System.Web.Compilation.BuildProvidersCompiler..ctor(VirtualPath configPath, Boolean supportLocalization, String outputAssemblyName) +54
System.Web.Compilation.CodeDirectoryCompiler.GetCodeDirectoryAssembly(VirtualPath virtualDir, CodeDirectoryType dirType, String assemblyName, StringSet excludedSubdirectories, Boolean isDirectoryAllowed) +600
System.Web.Compilation.BuildManager.CompileCodeDirectory(VirtualPath virtualDir, CodeDirectoryType dirType, String assemblyName, StringSet excludedSubdirectories) +125
System.Web.Compilation.BuildManager.CompileCodeDirectories() +265
System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() +320
[HttpException (0x80004005): The specified module could not be found. (Exception from HRESULT: 0x8007007E)]
System.Web.Compilation.BuildManager.ReportTopLevelCompilationException() +58
System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() +512
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters) +729
[HttpException (0x80004005): The specified module could not be found. (Exception from HRESULT: 0x8007007E)]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +8986035
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +85
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +333
Thanks & Regards,
Abdul Aleem Mohammad
St Louis MO - USA
|
|
|
|
|
indian143 wrote: Could the delay loading of IEFrame.dll be a problem Looks more of a side-effect than a cause.
indian143 wrote: FileNotFoundException: The specified module could not be found. That's what's causing the error.
indian143 wrote: And when we go to the web applications bin folder and remove the "cplustools.dll" dll file and refresh the browser the application runs normally. Sounds like it's copying an outdated version to the output-folder. And it runs normal if the assembly is deleted from that folder? Then there's probably a correct version on the system, probably in the GAC.
indian143 wrote: I am not sure why we need to use cplustools.dll and Calinx.dll Presumably the "Lab" assembly uses them. Pharmacy and laboratory stuff, according to Google.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I got kind of half understanding about the problem from the above answer, but you didn't confirm me about the reason of the problem. Can you please give me a little more hint for how to solve the problem.
Yes may be we are referencing the old version of the dll, and system may contain the newer version of the dll, but still if I reference the dll from the system32 directory the problem still persists.
I am trying my head braking to resolve this issue, not a lot of help over the internet too, may be because the this component is specific for this client only.
Please help me it would be really great.
Thanks & Regards,
Abdul Aleem Mohammad
St Louis MO - USA
|
|
|
|
|
indian143 wrote: Can you please give me a little more hint for how to solve the problem. Not really; you'll need to find out what version of the DLL is being expected.
indian143 wrote: Yes may be we are referencing the old version of the dll, and system may contain the newer version of the dll, but still if I reference the dll from the system32 directory the problem still persists. Create a clean build-environment, and make sure there's only one version of the assembly.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi,
As it was a product of another company, I don't have much knowledge or documentation for the dll, it is difficult to find the which is the correct version.
And another thing I doubt is, the dll is created on different OS, like may be xp or 2003 server etc. So may be this dll is referencing to some OS files that are not available in current OS or may be different versions of OS dlls.
Can we fix it by using any other means other than the above you mentioned? Its making everybody painful.
Thanks for your help.
Thanks & Regards,
Abdul Aleem Mohammad
St Louis MO - USA
|
|
|
|
|
Find "a" working verion, and test it for full compatibility with your product. Delete all other instances of it, anywhere on the network. It'll be quite some hunting-work, but the only alternative is to download random versions of the internet. That sounds like a worse idea.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hello everybody,
I need to use the function DeviceIoControl to monitor the status of a COM port (RS485) but I don't know how to do.
I have declared the functions:
<DllImport("CoreDll.dll")> _
Shared Function DeviceIoControl(ByVal hDevice As Integer, ByVal dwIoControlCode As Integer, _
ByVal lpInBuffer As String, ByVal nInBufferSize As Integer, _
ByVal lpOutBuffer() As Byte, ByVal nOutBufferSize As Integer, _
ByRef lpBytesReturned As Integer, ByVal lpOverlapped As Integer) As Integer
End Function
Private Declare Function CreateFile Lib "coredll.dll" _
(ByVal lpFileName As String, ByVal dwDesiredAccess As Int32, _
ByVal dwShareMode As Int32, ByVal lpSecurityAttributes As IntPtr, _
ByVal dwCreationDisposition As Int32, ByVal dwFlagsAndAttributes As Int32, _
ByVal hTemplateFile As IntPtr) As IntPtr
Declare Function CloseHandle Lib "coredll.dll" (ByVal hObject As IntPtr) As Boolean
I use CreateFile (it works) to get an handle to the COM port and I pass it to DeviceIoControl:
Const FILE_SHARE_READ As Integer = &H1
Const FILE_SHARE_WRITE As Integer = &H2
Const GENERIC_READ As Integer = &H80000000
Const GENERIC_WRITE As Integer = &H40000000
Dim portPtr As IntPtr
Dim ris As Boolean
Dim InBuffer As String
Dim OutBuffer(100) As Byte
Dim BytesReturned, Overlapped As Int32
portPtr = CreateFile(com.PortName & ":", GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)
ris = DeviceIoControl(portPtr.ToInt32, IOCTL_SERIAL_GET_COMMSTATUS, InBuffer, InBuffer.Length, OutBuffer, OutBuffer.Count, BytesReturned, Overlapped)
CloseHandle(portPtr)
I don't know what must be the value of IOCTL_SERIAL_GET_COMMSTATUS and I'm not sure that I'm using DeviceIoControl in the right way.
I have found some information only in C++ like:
#define IOCTL_SERIAL_GET_COMMSTATUS CTL_CODE(FILE_DEVICE_SERIAL_PORT, 27, METHOD_BUFFERED, FILE_ANY_ACCESS)
but how can I use it in VB.NET?
And then, DeviceIoControl should fill a SERIAL_STATUS structure:
typedef struct _SERIAL_STATUS {
ULONG Errors;
ULONG HoldReasons;
ULONG AmountInInQueue;
ULONG AmountInOutQueue;
BOOLEAN EofReceived;
BOOLEAN WaitForImmediate;
} SERIAL_STATUS, *PSERIAL_STATUS;
but a structure is not just a byte array as in my declaration of DeviceIoControl (that I have found googling around).
Can somebody help me?
Thanks in advance.
|
|
|
|
|
|
Thank you David,
the reason I need to use DeviceIoControl is to catch the UART framing error that happens when I receive a DMX512 data packet.
I use the SerialPort class in trasmission without problem because I can create a correct DMX data packet with the starting break (line low - zero - at least for 88 µs) but when I receive data I can't get the break that starts the packet, I just get one zero, it doesn't metter how long the break is, but this break causes a framing error, so, if I get the framing error I get the start of the data packet.
|
|
|
|
|
|
Yes, I tried to use the ErrorReceived event but it doesn't seem to work.
I use the DataReceived event to receive data and it works but I get no error, on the other hand I read in the help that:
...
PinChanged, DataReceived, and ErrorReceived events may be called out of order, and there may be a slight delay between when the underlying stream reports the error and when code can when the event handler is executed. Only one event handler can execute at a time.
...
However, I found another way using API and p/invoke: I use the function WaitCommEvent() to get directly the break instaed of the framing error.
This solution was suggested by Bruce Eitman in the MSDN Windows Embedded Compact forum and I found a good example in the article "Smart Device Print Engine for Compact Framework" by Orkun GEDiK here in CodeProject.
Thanks anyway for your interest and for your time.
|
|
|
|
|
How to use keypress event inside timer?
i have to use
Asc(e.keypress)=102 '102-Ascii value of "f"
e.handled=true 'stop the key from processing
all these has to be done even when the application is minimized
|
|
|
|
|
What you want is not possible.
First, the keypress event is raised on the UI-thread. Whatever timer is being used, it will not be raised during the timer-event. You'd need to buffer them keys somewhere.
If the application is minimized, then no keypress-events will be generated - since your app does not have input-focus. You'd need a keylogger for what you'd want - which brings us back to your previous question.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I have a keylogger and it is detecting the key pressed.
but i want that the pressed key should not be processed to display on the screen what ever be the current application.
And How to have input-focus to my application.
Help me to solve these problems.......
|
|
|
|
|
You're NOT going to use the normal form events for this, nor a timer.
You have to use a (Google for this "global keyboard hook".
Since you just admitted to making a keylogger, you're on your own from now on.
|
|
|
|
|