|
-
Jul 31st, 2007, 09:29 AM
#1
Thread Starter
Junior Member
Re: Problems with COM ports
If I set the WriteBufferSize to 4096, it drops out to my Catch, and does not send the data.
Code:
Catch ex As Exception
MsgBox(com & " not found. Please try other com port(s)")
In the text files that it usually sends, there are no end of line characters. And to put these in would mean a re-write of lots of drivers in for the CAD software that we use. Which I am sure would put an end to this project.
The file size that works was 51 bytes (Size on disk 4,096), ones that don't work are 3.11 KB (3,187 bytes) (size on disk 4,096).
-
Jul 31st, 2007, 12:00 PM
#2
Re: Problems with COM ports
 Originally Posted by danweb
In the text files that it usually sends, there are no end of line characters.
By end of line characters, I mean carriage returns or linefeeds. If when you view the file in Notepad for example, you can see each CNC command or whatever on a separate line, then it is fairly safe to assume there are some of those. If the file looks like one long line, then there might not be any, or perhaps only a termionator at the end.
As long as it does not break any of your company rules, could you post (upload) an example of each file?
It looks like you are trying to send it correctly with the .Write() method though, so this should stop any problems with incorrect carriage returns and line feeds that might happen with .WriteLine().
The VB help says that by default, the .Write() method uses ASCII encoding, and that this "encodes all characters greater then 127 as (char)63 or '?'". Perhaps the files you have are a different encoding, maybe UTF8 or something.
Maybe you could consider changing the Try..Catch arrangement to give you a more explicit error when it fails. At least change it to this for a test, so you know exactly what the exception is:
Code:
Catch ex As Exception
Messagebox.Show(ex.Message)
At the line the byte array gets filled from the binary reader, set a breakpoint to see if the array is filled with the amount of bytes you expect, for each file.
Well, a few more things to look at there. Don't give up!
-
Aug 1st, 2007, 03:33 AM
#3
Thread Starter
Junior Member
Re: Problems with COM ports
 Originally Posted by Andy_P
Maybe you could consider changing the Try..Catch arrangement to give you a more explicit error when it fails. At least change it to this for a test, so you know exactly what the exception is:
Code:
Catch ex As Exception
Messagebox.Show(ex.Message)
I tried that and found out that I was trying to set the WriteBufferSize whilst the port was open, so I have closed it and re-opened before sending, and I can now set the buffer size!
So I shall test it on the machine today to find out if it has worked.
Also I have been testing another piece of software called DNC4U http://www.dnc4u.com/
With the following settings the program works perfectly at sending my files.

Here is an example of the files I am sending to the CNC (open in notepad or whatever)
http://www.mediafire.com/download.php?fe4zntyz91j
-
Aug 1st, 2007, 05:17 AM
#4
Thread Starter
Junior Member
Re: Problems with COM ports
On the second tab of the DNC software it shows a buffer size of 8192, so I have tried this along with 4096.
I have basically mirrored the exact settings as per the DNC software.
But I cannot get it to send, I have checked the size of the array using a breakpoint, and it is exactly the same as the file size.....
Any more ideas would be fantastic, I thank you for your patience and support
-
Aug 1st, 2007, 12:22 PM
#5
Re: Problems with COM ports
One things that stands out from looking at the docs for the DNC program, is that it says certain CNC machines require some characters to be sent before and after file transmission:
From the docs:
You can send a series of characters to your CNC both before and after file transmission. This is because many CNC need specific characters to "tell" it that comms is about to start or finish. dnc4U lets you setup these characters as a series of ASCII values (since many of these characters cannot be typed at the keyboard)
What do you have set for all the parameters on the 'Send' tab of the DNC program? Maybe you require these bytes to be added to your byte array that you transmit.
 Originally Posted by danweb
The file size that works was 51 bytes
Could you post an example of this file you say works?
The DNC docs also make mention of sending by block and packet size, so if your CNC machine requires transmission like this, then your code will have to be modified. Maybe the 51 byte file works because it is below the size for one block and so can be sent all in one go.
-
Aug 1st, 2007, 02:35 PM
#6
Thread Starter
Junior Member
Re: Problems with COM ports
I think I might have been confused about having one that worked....
Here are the settings from the send tab of DNC;

So I guess that it doesn't need before or after characters?
As for sending as a block send (as per the DNC settings) I cannot see how I would enable that?
Incidentally there is nothing set in the delays other than settings to wait after sending and an abort after inactivity setting.
-
Aug 1st, 2007, 03:59 PM
#7
Re: Problems with COM ports
 Originally Posted by danweb
So I guess that it doesn't need before or after characters?
It would appear so, yes.
 Originally Posted by danweb
As for sending as a block send (as per the DNC settings) I cannot see how I would enable that?
Well, as each 'block' is simply a line in its own right, defined by the end of block code 'CR/LF' (carriage return/line feed), it should be fairly simple. It would mean sending one line at a time with a pause in between each line that is sent.
Try this out:
Code:
' sets up comPort as SerialPort and opens the port
Using comport As System.IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort(com)
' displays the OpenFileDialog1 to the user
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
' defines sr as a FileStream and opens the filename from the OpenFileDialog1
Dim fs As New IO.FileStream(OpenFileDialog1.FileName, IO.FileMode.Open)
' reads the sr filestream ino the stream reader (sr)
Dim sr As New System.IO.StreamReader(fs)
' reads each line of the file into a list of strings
Dim fileLines As List(Of String) = New List(Of String)
Dim line As String
Do
line = sr.ReadLine
If line IsNot Nothing Then
fileLines.Add(line)
End If
Loop Until line Is Nothing
' close the readers
sr.Close()
fs.Close()
' setup the com ports
comport.DtrEnable = True
comport.RtsEnable = True
comport.BaudRate = 9600
comport.DataBits = 7
comport.Parity = System.IO.Ports.Parity.Even
comport.StopBits = System.IO.Ports.StopBits.One
comport.Handshake = System.IO.Ports.Handshake.None
' explicitly set Carriage return and Line feed as the end of line terminator
comport.NewLine = Convert.ToChar(&HD) & Convert.ToChar(&HA)
' writes each line of data to the comPort with pause after each block
For Each str As String In fileLines
comport.WriteLine(str)
System.Threading.Thread.Sleep(10) ' 10 = 10 milliseconds
Next str
End If
End Using
Two things to note. Firstly, the end of line terminator is set explicitly to the correct value of a carriage return and line feed, to stop any confusion over whether it is or not. Also, after each line is sent, there is a pause, which can be set in milliseconds should you need it.
As it stands, with this pause in place it may noticably make your UI a bit unresponsive, so should it prove to work, this would really need to be done another way, with the code running in another thread. But let's see if it works first!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|