Results 1 to 30 of 30

Thread: Problems with COM ports

Hybrid View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    17

    Re: Problems with COM ports

    Quote 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

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    17

    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

  3. #3
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    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.

    Quote 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.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    17

    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.

  5. #5
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: Problems with COM ports

    Quote Originally Posted by danweb
    So I guess that it doesn't need before or after characters?
    It would appear so, yes.

    Quote 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!
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    17

    Re: Problems with COM ports

    Thanks Andy, I will try this code today, and see what happens.

    Thanks again for all your help

    Feedback to follow...

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    17

    Re: Problems with COM ports

    Andy_P = Vb Guru!

    That's cracked it as far as I can tell. Wow you rock!

    I have picked through the code, and feel that I pretty much understand what you have done...

    The operator is going to run it for a bit and see how we get on.

    As for freezing up the GUI, it is, but no more than the previous incarnations.

    That was why I am displaying the label that tells the user "Sending to CNC"

    If all pans out okay, I hope to improve this by giving the user a progession bar.

    Thanks again Andy_P

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width