Serial Communction
Serial Communction
Serial Communction
Serial Communction
Serial Communction
Serial Communction Serial Communction Serial Communction Serial Communction Serial Communction Serial Communction Serial Communction Serial Communction
Serial Communction Serial Communction
Serial Communction
Go Back  Xtreme Visual Basic Talk > > > Serial Communction


Reply
 
Thread Tools Display Modes
  #1  
Old 08-07-2007, 02:47 AM
Zuffi Zuffi is offline
Junior Contributor
 
Join Date: Mar 2005
Location: Singapore
Posts: 272
Default Serial Communction


Dear All,

With reference to http://msdn.microsoft.com/msdnmag/is...NETSerialComm/

I have tried modifying this to sync with my device without success. However, if i tried to sync using the same data using the vb provided Serial Port component, i managed to get the device to response.
But when i tried to send the same data using code in the above url, I always get a Timeout Exception. As I'm very new to Serial communication, i would be glad if someone got explain some basic concept

Thanks and Best Regards,
Zuffi
Reply With Quote
  #2  
Old 08-07-2007, 03:56 AM
Zuffi Zuffi is offline
Junior Contributor
 
Join Date: Mar 2005
Location: Singapore
Posts: 272
Default

Hi all,

the funny thing is that if i comment the
throw new CommPortException(reason); i managed to get the respond from the devices. I didn't change any code to the CommBase and CommLine to the best of my memory.

Thanks for any advice,
Zuffi
Reply With Quote
  #3  
Old 08-07-2007, 04:24 AM
Zuffi Zuffi is offline
Junior Contributor
 
Join Date: Mar 2005
Location: Singapore
Posts: 272
Default

Any Question,

I have a byte array "b" of
{255,01,255,01,255,01}

i store the result of Encoding.ASCII.getString( b ) into str;

however when i called: Encoding.ASCII.getBytes( str ) ;
i getting back 63,1,63,1,63,1

can someone tell me where i go wrong?
Thanks and Best Regards,
Zuffi
Reply With Quote
  #4  
Old 08-11-2007, 07:44 PM
MKoslof's Avatar
MKoslofSerial Communction MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Can you show us more of your code? What populates the source byte array? Why are you storing the bytes within an ASCII string? And then you try to get the bytes BACK again? Is this within seperate procedures or within different clients/communicators? Are you passing the string but then re-evaluting the bytes elsewhere?
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #5  
Old 08-13-2007, 07:10 AM
Zuffi Zuffi is offline
Junior Contributor
 
Join Date: Mar 2005
Location: Singapore
Posts: 272
Default

Hi,

That conversion is due to legacy reason as I had attempted to covert the code directly into C#.

However, i think that may not be worth the effort as there are converted to and fro byte and string. I have convert them to store as byte array. Currently I'm unable to test what i had done, as I facing a new problem. However, I will post those code with "bug" soon.

The problem i'm facing is WriteFile throw an AccessViolationException and I'm will be looking into it next time.

However, as there are some other adhoc request, I have to handle those first.

Thanks and Best Regards,
Zuffi
Reply With Quote
  #6  
Old 08-13-2007, 09:23 AM
MKoslof's Avatar
MKoslofSerial Communction MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Quote:
I have convert them to store as byte array
Good, that is what I was driving at/questioning

Regarding an "AccessViolationException" it sounds like a permissions issue or the file is in use or is exclusively locked by another application. That is where I would like first in troubleshooting this problem.

Let us know what you find
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #7  
Old 08-13-2007, 10:54 AM
Zuffi Zuffi is offline
Junior Contributor
 
Join Date: Mar 2005
Location: Singapore
Posts: 272
Default

Hi, Thanks for the prompt reply.
I managed to finish the adhoc task and looking at the serial communication issue again.

The exception happened here:

WriteFile(lngHandle, strDataToWrite, lngBytesToWrite
, out lngBytesWrite, osWriter.hEvent )

the declaration for WriteFile is as follows:
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern Boolean WriteFile(IntPtr fFile, Byte[] lpBuffer, UInt32 nNumberOfBytesToWrite,
out UInt32 lpNumberOfBytesWritten, IntPtr lpOverlapped);

lngHandle value is assigned by createFile


//static AutoResetEvent write = new AutoResetEvent(false);

osWriter is the OVERLAPPED structure

osWriter.hEvent = writeEvent.SafeWaitHandle.DangerousGetHandle();

other than the writeEvent which is a ManualResetEvent

i have also tried
//Win32API.CreateEvent((IntPtr)(null), true, false, null);


Any suggestion where i go wrong?

Thanks and Best Regards,
Boon Hui
Reply With Quote
  #8  
Old 08-13-2007, 11:50 AM
Zuffi Zuffi is offline
Junior Contributor
 
Join Date: Mar 2005
Location: Singapore
Posts: 272
Default

Ops
Sorry.
I suppose to pass in a pointer of the OVERLAPPED struct.

Reply With Quote
  #9  
Old 08-13-2007, 05:03 PM
MKoslof's Avatar
MKoslofSerial Communction MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

One generic thing to think about: Are you attempting to open the file in question with exclusive access? The problem is, when you are dealing with the NTFS file system at the time of your code request you can't guarantee you are the only process attempting to use the file.

If you are going to modify an existing file, you need to verify you can get an exclusive lock on the file before continuing your processing.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #10  
Old 08-21-2007, 10:42 AM
Zuffi Zuffi is offline
Junior Contributor
 
Join Date: Mar 2005
Location: Singapore
Posts: 272
Default

Quote:
Originally Posted by MKoslof View Post
One generic thing to think about: Are you attempting to open the file in question with exclusive access? The problem is, when you are dealing with the NTFS file system at the time of your code request you can't guarantee you are the only process attempting to use the file.

If you are going to modify an existing file, you need to verify you can get an exclusive lock on the file before continuing your processing.
Hi, thanks for your reply.

ptrUWO = Marshal.AllocHGlobal(Marshal.SizeOf(osWriter));

Marshal.StructureToPtr(osWriter, ptrUWO, true);

I managed to solve the accessviolationException, however,
lngStatus = Win32API.WriteFile(lngHandle, strDataToWrite, lngBytesToWrite
, out lngBytesWrite, ptrUWO);

the lngBytesWrite is always returning 0, i believe that the data isnt send over, and i cant figure out what's wrong.
would appreciate any advices.

Thanks
Zuffi
Reply With Quote
  #11  
Old 08-28-2007, 02:33 AM
Zuffi Zuffi is offline
Junior Contributor
 
Join Date: Mar 2005
Location: Singapore
Posts: 272
Default

Hi all,

I not sure whether this had being asked before, I believe that the OpenFile API take a string such as \\.\COMX to connect to the com port, is there any ways to connect to the USB port? Currently, I'm using a USB-Serial cable.

Thanks
Zuffi
Reply With Quote
  #12  
Old 08-28-2007, 08:21 PM
stev stev is offline
Regular
 
Join Date: Nov 2003
Location: Queensland, Australia
Posts: 84
Default

When you connect a USB-serial port adapter to a USB port the OS sets up a virtual serial port. You can see this in operation by openning device manager and expanding the list of ports. When you plug and unplug the adapter it will appear and disappear from the list. You used to be able to name the comm port anything you like but it seems that you can now only pick a name from a drop down list in the advanced properties.

You can use

Dim ports As String() = IO.Ports.SerialPort.GetPortNames()

to get the names of all serial ports


If you are using the serialport control then set the portname property and use the .open() method
Reply With Quote
  #13  
Old 08-28-2007, 09:29 PM
Zuffi Zuffi is offline
Junior Contributor
 
Join Date: Mar 2005
Location: Singapore
Posts: 272
Default

Hi,

Thanks for the prompt reply. In the past, our application uses the virtual com port driver from the manufacture and due to the fact that they can be so many "virtual com port around". We have some customers that when they plug in the USB-Serial Convertor, the system allocated number as large as 13, with most of the remaining Com port no. already in use (I do not understand why).

In order for our application to connect to the correct com port, it will loop through all Com Port no. that are being allocated and attempts to sync with each of them until it get a correct response. However, as our device has a bulit in power saving mood, it will exit the mode that send data when there isnt any communication after a prefix duration. Therefore on those system with large com port, it usually failed to sync as by the time it reaches Port 13, the device had already exited it data com mode. (to be exact, the device will have exit its data transfer mode by the time the system had attempts to sync its 3rd port. e.g. Port 1, 3, 4. Therefore if the device is in Port 5, it won't be able to sync)

Recently, we are attempting to use the device in the USB mode, (to my limited understanding, each USB device have a unique PID, and a VID?? )
therefore it would really be nice if i can do something like,

FileOpen("\usb\VID_XXXX&PID_XXXX", .....)
I believe that this number are burn into the firmware for the USB-Serial cable.
With these unique identification, we can connect directly to the correct port.
I think it would be great to remove all customer complain that the device failed to auto detect the Com port. (hee hee)

Thanks and Best Regards
Zuffi

Last edited by Zuffi; 08-28-2007 at 09:31 PM. Reason: Update
Reply With Quote
  #14  
Old 08-29-2007, 05:24 PM
stev stev is offline
Regular
 
Join Date: Nov 2003
Location: Queensland, Australia
Posts: 84
Default

This is above my head but a couple of suggestions.

1. Start a new thread on this topic
2. The manufacturer would have supplied a driver. How does the device show up in Device Manager?
3. Does the manufacturer provide any other tools such as a dll with documentation or description of protocols for USB connection?
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Serial Communction
Serial Communction
Serial Communction Serial Communction
Serial Communction
Serial Communction
Serial Communction Serial Communction Serial Communction Serial Communction Serial Communction Serial Communction Serial Communction
Serial Communction
Serial Communction
 
Serial Communction
Serial Communction
 
-->