Author Topic: Simple VB.Net RS-232 program to capture serial port data to a text file  (Read 85135 times)

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
Re: Simple VB.Net RS-232 program to capture serial port data to a text file
« Reply #45 on: September 23, 2012, 11:15:12 am »
I've been looking at the old posts on my thread and am thankful for your program Jan. I have been able to get data into the CNC milling machine, whether it be a straight file, or a bunch of files from a hard-drive or a network if need be (more a future part of this). I have had to put a while loop to possibly wait when control signals such as DC1 or DC3 are sent from the controller Xon/Xoff or ASCII 17 and ASCII 19 respectively. That's a must and will test this. I have been just partially successful in making an in process change. This has been tricky. And the guy working on the probe to test a milled hole has done very well on his probe. I have put in a routine to compare the RS-232 raw data as it comes in. This continues to be a painstaking adventure, since I am Regex, from a library book I borrowed. One new instance, for example is data that came in as .0000000E 9 [oops] didn't know the machine was capable of scientific notation. Not a biggie, but will have to add.

NOW, I am finding that I really didn't stop the text coming into MainForm, but rather just hide it. I like seeing the data in the RichTextBox for testing, and just hide it for now when I don't want to see it. BUT was is happening at the end is the text in the textbox is going into the AccessForm method, which I am just trying to get a feel for. I don't want that sent back to the CNC since it is the data that I wanted and not the what the machine needs.

So the first thing the machine sends me is a DNC which is the first thing I send for DripFeed which is what you have to do start DNC DripFeeding. So when the UpdateStatusLabel sends DNC after all the current data has been sent to the CNC controller, it starts to send what is in the RichTextBox. NOT what I want!

I suppose I could just delete the text in the textbox when the data sent to the CNC controller is presently done, but maybe just not sending data to the textbox at all is best! Not sure! I know this will make the company's day and Dakota who is the smart young man who has done the electronics on the probe happy. The routine to compare the data to a reference file I have spent a lot of time on and I am anxious to get this incorporated, so I can have the machine make the intelligent choice of 'what to do next'.
« Last Edit: September 23, 2012, 11:19:47 am by btcomp »

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 2844
    • Lakeview Research
Re: Simple VB.Net RS-232 program to capture serial port data to a text file
« Reply #46 on: September 23, 2012, 11:53:26 am »
Just comment out the code that sends the text to the textbox.

It sounds like you've made a lot of progress in your project!

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
Re: Simple VB.Net RS-232 program to capture serial port data to a text file
« Reply #47 on: September 23, 2012, 12:09:55 pm »
Thanks Jan. That will be the answer, I will put a toggle in so that for testing I can see the data otherwise just cancel.

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
Re: Simple VB.Net RS-232 program to capture serial port data to a text file
« Reply #48 on: October 09, 2012, 09:09:50 pm »
Lots of progress made on this project. And I do have the project working where the milling machine will do one of two things depending on the data received. This is cool. What I mean is two weeks ago I ran six points with the wireless probe made by Dakota, young college student. And I compared them to values with a reference file to within 0.001 inches. It checked and the machine sent a PRINT statement, "SUCCESS" and stopped the milling machine. Repeated the test and manually touched the probe with my hand during testing, effectively causing the run to fail, and it sent "DATA NOT WITHIN TOLERANCE" and it ran one more probe point.

Okay, hardcoding worked. But I can't get the MainForm to work. Code below.

What happens with the code is that it is ignored, because I don't think it has any values. I get this error message in the textbox of the MainForm.
'Exception: Object reference not set to an instance of an object Module ComPorts Method DripFeeedCNCDataParts

I commented out the direct WriteToComPort method. It will work if I put in local values, I just can't get it to work with the MainForm values I have in arrays!



 
Code: [Select]
'******************************************************************************************BT-25Sept2012********************************
            'MessageBox.Show(BoolMustBePerfect & " x = " & x & "UBound(file2Data) = " & UBound(file2Data))
            If BoolMustBePerfect = True And x = UBound(file2Data) - 1 Then
                '  MessageBox.Show("This should be what happens")
                WriteToComPort("#PRINT" & Chr(34) & "THIS IS BEGINNING TO BE A SUCCESS" & Chr(34) & vbCrLf)

                WriteToComPort("#PRINT" & Chr(34) & "JUMP TO NEXT PART OF THE PROGRAM" & Chr(34) & vbCrLf)

                ' WriteToComPort(MainForm.strStoreRestString) 'FOR TESTING ONLY!! BT-21Sept2012


                WriteToComPort("M02" & vbCrLf)

            ElseIf BoolMustBePerfect = False And x = UBound(file2Data) - 1 Then
                '  MessageBox.Show("Why did this happen?")
                WriteToComPort("#PRINT" & Chr(34) & "THE DATA WAS NOT WITHIN TOLERANCES!" & Chr(34) & vbCrLf)

                WriteToComPort("#PRINT" & Chr(34) & "RUN THE NEXT INCREMENT" & Chr(34) & vbCrLf)

                '  WriteToComPort("T2M6" & vbCrLf)

                '  WriteToComPort("G0 H99 Z0.100" & vbCrLf)
                ' WriteToComPort("G0 X1.8333 Y-1.5975" & vbCrLf)
                ' WriteToComPort("G01 Z-0.187 F30.0" & vbCrLf)
                ' WriteToComPort("G01 G31 F1.0 X1.8333 Y-1.7475 P1" & vbCrLf)
                ' WriteToComPort("G0 X1.8333 Y-1.5975" & vbCrLf)
                'WriteToComPort("T1M6" & vbCrLf)

                'WriteToComPort("M02" & vbCrLf)
             
               
                 Dim strProbeAgain As String = MainForm.DripFeedCNCDataParts(MainForm.strProbe(1, 1))
                strProbeAgain = MainForm.DripFeedCNCDataParts(MainForm.strProbe(1, 1))

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
Re: Simple VB.Net RS-232 program to capture serial port data to a text file
« Reply #49 on: October 18, 2012, 08:26:46 pm »
I have found that XModem has been used by the company's software successfully. I looked into that option. Most say to use Xon/Xoff, but the parameters are given for both. Is it possible to do that with the SerialPort object?

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 2844
    • Lakeview Research
Re: Simple VB.Net RS-232 program to capture serial port data to a text file
« Reply #50 on: October 18, 2012, 08:51:56 pm »
Xmodem is a file-transfer protocol that adds error checking.

Xon/Xoff is a flow-control protocol to manage when the sender can send to the receiver.

You can use both together.

What line of code causes the Object reference not set to an instance of an object error?

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
Re: Simple VB.Net RS-232 program to capture serial port data to a text file
« Reply #51 on: October 18, 2012, 09:30:26 pm »
In the case of the CNC I can use either but not both at the same time. I have been sending in Xon/Xoff but I don't think it would be too difficult to put in a separate module to do this in Xmodem and have both options. DNC is used for Xon/Xoff and DNCX is used for Xmodem. Most of the guys in the shop use the Xmodem, though the programmers have told me they mostly use Xon/Xoff. The problem is on the older CNC controller, I am working with one of the older ones from the 1990s I am thinking that it isn't working properly in Xon/Xoff, since I can't get the DC1/DC3 from the controller, only on rare occasion. The newer machines seem better able to handle both. The below was run on one of the newer CNC machines. I had the machinist run both Xon/Xoff so my port sniffer could take a look. Below is the Xmodem results.

Request: 10/16/2012 3:42:21 PM.39364 (+0.0000 seconds)


 40 0D 44 4E 43 58 2C 30 2C 30 2C 30 0D 0A         @.DNCX,0,0,0..


Answer: 10/16/2012 3:42:23 PM.01864 (+2.0000 seconds)


 44 4E C3 D8 AC 30 AC 30 AC 30 8D 0A 15            DN秘񦏎0..   


Request: 10/16/2012 3:42:23 PM.01864 (+0.0000 seconds)


 01 01 FE 25 0D 0A 0D 0A 0D 0A 47 31 37 47 38 30   ..%......G17G80
 47 39 30 47 38 45 31 0D 0A 4F 32 28 44 45 54 5F   G90G8E1..O2(DET_
 30 36 30 32 29 0D 0A 28 54 4F 4F 4C 30 2E 31 38   0602)..(TOOL0.18
 37 35 44 49 41 30 2E 30 39 33 37 52 41 44 29 0D   75DIA0.0937RAD).
 0A 47 31 54 32 4D 36 46 31 30 30 2E 0D 0A 53 34   .G1T2M6F100...S4
 32 30 30 4D 33 0D 0A 47 34 33 48 32 5A 33 2E 37   200M3..G43H2Z3.7
 30 38 31 4D 38 0D 0A 47 31 58 2D 30 2E 33 37 35   081M8..G1X-0.375
 59 31 2E 38 37 35 46 31 30 30 2E 0D 0A 5A 33 2E   Y1.875F100...Z3.
 37 30 38 88                                       708ˆ   
       


Answer: 10/16/2012 3:42:23 PM.04964 (+0.0000 seconds)


06                                                .               


Request: 10/16/2012 3:42:23 PM.04964 (+0.0000 seconds)


 01 02 FD 31 0D 0A 5A 33 2E 32 39 0D 0A 47 31 5A   ..1..Z3.29..G1Z
 33 2E 30 39 46 32 30 2E 0D 0A 47 31 58 2D 30 2E   3.09F20...G1X-0.
 33 35 34 33 46 33 30 2E 0D 0A 58 2D 30 2E 32 33   3543F30...X-0.23
 36 32 0D 0A 58 32 2E 38 37 35 0D 0A 47 31 59 31   62..X2.875..G1Y1
 2E 38 37 31 34 5A 33 2E 30 39 30 36 46 32 30 2E   .8714Z3.0906F20.
 0D 0A 58 32 2E 38 37 34 38 59 31 2E 38 36 37 38   ..X2.8748Y1.8678
 5A 33 2E 30 39 0D 0A 47 31 58 2D 30 2E 33 37 35   Z3.09..G1X-0.375
 46 33 30 2E 0D 0A 47 31 59 31 2E 38 36 34 32 5A   F30...G1Y1.8642Z
 33 2E 30 15                                       3.0. 
         


Answer: 10/16/2012 3:42:23 PM.08064 (+0.0000 seconds)


 06                                                .               

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
Re: Simple VB.Net RS-232 program to capture serial port data to a text file
« Reply #52 on: October 19, 2012, 11:01:21 am »
Okay, thanks Jan. I guess what I need to do is write another module, so that my program can do both Xon/Xoff and add an XModem file-transfer protocol. I am at work today, so I am going to try to simulate the Xmodem on the old machine. One guy was running in DNC XModem, but unfortunately my port sniffer program wasn't on, otherwise I could have captured it. But I am going to try to do a dry run with it.

What line of code causes the Object reference not set to an instance of an object error?

Okay, I first have strProbeAgain setup here at the beginning of the ComPorts class.
 
Code: [Select]
[Public Class ComPorts
    Dim FileName As String  'Make this variable available to all the methods in this class. BT-12Jun2012
    Public Shared BoolMustBePerfect As Boolean = True
    Public strProbeAgain As String = ""

I think what is happening is in the code below is a variable scope problem. I really need to read up on Public, Friend Sharing. The strProbeAgain variable is supposed to have the Probe File data stored in an array in the MainForm Class and because the variable is not appearing to be defined. I have had this happen several times before and had to change the scope and placement of a string, decimal, integer or array variable Below is the code from the ComPorts module:
Code: [Select]
'Try this - Send the program to MainForm to do more data. If this doesn't work will have to copy the MainForm
                ' procedure into ComPorts. BT-6Oct2012
                ' MainForm.strFile = "Give this a value"
                'MainForm.DripFeedCNCDataParts()
                '  Dim strProbeAgain As String = MainForm.DripFeedCNCDataParts(MainForm.strProbe(1, 1))
                strProbeAgain = MainForm.DripFeedCNCDataParts(MainForm.strProbe(1, 1))

                WriteToComPort(strProbeAgain)
                ' MessageBox.Show(strProbeAgain)
                '  WriteToComPort(MainForm.DripFeedCNCDataParts(MainForm.strProbe(1, 1)))
                '  MainForm.DripFeedCNCDataParts(MainForm.strProbe(1, 1)) 'Hardcoded for testing on Tues. 9Oct2012
                ' MessageBox.Show(MainForm.DripFeedCNCDataParts(MainForm.strProbe(1, 1)))

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 2844
    • Lakeview Research
Re: Simple VB.Net RS-232 program to capture serial port data to a text file
« Reply #53 on: October 19, 2012, 01:01:34 pm »
In my ComPortTerminal example, take a look at the AccessFormMarshal routine and associated code, which passes data to a form for display.

http://www.lvr.com/serport.htm#my_example_code


btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
Re: Simple VB.Net RS-232 program to capture serial port data to a text file
« Reply #54 on: October 19, 2012, 02:07:31 pm »
Thanks Jan, and I went out to the truck and grabbed my Murach's VB 2008 book on myBase and see some good info. As I said I have had this happen before, and often I will put messagebox.show statements in that just flat get ignored when I try to see what they say and the variables that I thought  :o should have the data I need, just aren't 'scoped' for it.  :)

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 2844
    • Lakeview Research
Re: Simple VB.Net RS-232 program to capture serial port data to a text file
« Reply #55 on: October 19, 2012, 02:18:14 pm »
Yes, it can take some research to get it right!

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
Re: Simple VB.Net RS-232 program to capture serial port data to a text file
« Reply #56 on: October 23, 2012, 11:53:37 am »
Below are the variables that I have defined in MainForm.  I have them outside any sub or function, because they may and are used throughout running the program, so their values have to persist.

Code: [Select]
'Analyze the REVCAM parts and put them into appropriate arrays.
    Public Shared strProbe(50, 5) As String
    Public Shared strRef(50, 5) As String
    Public Shared strMill(50, 5) As String
    Public Shared FixAtt(50, 20) As String
    Public Shared strPrgmUpToFirstRevCam As String = ""
    Public Shared strPrgmLastLines As String = ""
    Public Shared PockNoCnt As Integer = 1


 In the ComPorts module I have only tried to use the strProbe(50,5) array for testing. So instead of returning string data in the strProbe in the array, instead of sending data to the WritetoComPort function, the function isn't run and hence I don't get a Message for the strProbeAgain value in strProbe(1,1) is 'not defined' since the array itself isn't defined while in ComPorts module.

Code: [Select]
strProbeAgain = MainForm.DripFeedCNCDataParts(MainForm.strProbe(1, 1))
WriteToComPort(strProbeAgain)
MessageBox.Show(strProbeAgain)

I am having a hard time understanding the scope of the variables.

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
Re: Simple VB.Net RS-232 program to capture serial port data to a text file
« Reply #57 on: October 23, 2012, 03:33:45 pm »
Good news and bad news, and the bad news isn't really bad. But the bad news is I don't understand all there is about variable scoping and the inheritance of using MyBase, polymorphism, MyClass ...

But the good news is I finally figured out the problem. So the code I had:
Code: [Select]
strProbeAgain = MainForm.DripFeedCNCDataParts(MainForm.strProbe(1, 1))
WriteToComPort(strProbeAgain)
MessageBox.Show(strProbeAgain

did not need to have the DripFeedCNCDataParts at all. I think I had a problem a couple weeks ago with a variable scope at the time and fixed that problem but didn't update the above code. So when I ran the below code the variable was Public Shared whereas above ONLY the MainForm.strProbe(1,1) was Public Shared, and DripFeedCNCDataParts was just Public.

An easy solution. Now I can work with the arrays, that I spent time building directly!

Thanks Jan for your help on this. I still want to learn more about OOP and Inheritance, but I am going to go home happier.
P.S. And even happier after the election is over, whomever wins too, not to be political!


               
Code: [Select]
strProbeAgain = MainForm.strProbe(1, 1)
                WriteToComPort(strProbeAgain)
                MessageBox.Show(strProbeAgain)

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
Re: Simple VB.Net RS-232 program to capture serial port data to a text file
« Reply #58 on: October 23, 2012, 04:18:02 pm »
I still may need to use the MyBase on the procedure that filters the CNC data. I am going to test this in the next hour. As the data contains statements that are filtered in MainForm but I am calling it from ComPorts, and it is only Public. But it is just a procedure so I think I may get my hands dirty on MyBase anyway. Will post later if I can't get this to work today.

btcomp

  • Frequent Contributor
  • ****
  • Posts: 58
Re: Simple VB.Net RS-232 program to capture serial port data to a text file
« Reply #59 on: October 23, 2012, 05:47:30 pm »
Yep, I will have to do something. The array data is fine, but it has to be filtered and the MainForm class function DripFeedCNCDataParts takes the data that comes through each line and filters out lines that start with parenthesis ( or asterisk * ... and these lines are not sent to the CNC because they are not allowed in DNC mode.

I could just copy the procedure over to the ComPorts but that isn't the way to do it. So there is always another step.