.trim serialports and filestream questions code included , code not working =(

Jun 18 2008 7:07 PM

I jsut posted with a lot less information a problem I am having and did not get a reply. So now I will post all the information I have available and hope for some help here.

My problem involves trim methods serialports and file streams. my on recevied method reads the first line and should parse it out to give me a file name. In my case the first line has 2 extra characters i have to remove plus two more characters added in the transfer from the okuma lathe machines we use. So I strip the extra chars to make a good file name then add them back so the file is correct. I then read the rest of the port buffers data and add it to a string. Take the two strings write them to the file.writealltext method and I should have a file. The string.length is comeing back as being 61 chars long so I tried triming it a few different ways none have worked. Included I have an example .min file which is an instruction for the machine I am sending to, the out put I get from the console and my code. The output from the file.WriteallTExt method when viewed in notepad ++ reveals a slew of trailing nulnulnulnul chars which I dont know how to recreate here so. Here it is in a nutshell, any help will be greatly appreciated.

#region Using Calls for system namespaces

using System;

using System.IO;

using System.Text;

using System.IO.Ports;

#endregion

#region Namespace ComCom

namespace ComCom

{

public class ProgComm //serial class

{

private static SerialPort SERCOM = new SerialPort(); //serial class

public static void OnReceived(object sender, SerialDataReceivedEventArgs args)//Handle incoming data received method

{

string str = SERCOM.ReadTo("%");

//string vMin = SERCOM.ReadTo("%");

Console.WriteLine(str);

Console.WriteLine(str.Length);

str = str.Substring(3);

Console.WriteLine("sub " + str);

Console.WriteLine(str.Length);

str = str.TrimEnd(null);

Console.WriteLine(str);

str = str.Trim();

Console.WriteLine("post trim " + str.Length + " " + str);

//str = ("$" + str + "%");

//string compiled = (str + Environment.NewLine + vMin + Environment.NewLine + "%");

File.WriteAllText(@"\\MPI.COM\Production\EP\CNC\TEST.MIN", str);

//TextWriter tw = new StreamWriter(File.Open((@"\\MPI.COM\Production\EP\CNC\" + str.TrimEnd(' ')), FileMode.Create, FileAccess.ReadWrite,FileShare.ReadWrite));

//TextWriter tw = new StreamWriter(File.WriteAllText((@"\\MPI.COM\Production\EP\CNC\TEST.MIN"), FileMode.Create, FileAccess.Write, FileShare.Write));

//tw.Write(str);

//tw.Write(str);

//tw.Write("$" + str + "%");

//tw.Write(vMin);

//tw.Write("%");

}

private static void Main(string[] args) //array holds to input parameters [0] = file direction(Send or Receive) | [1] = file name to send | [2] = Com port to send to.

{

#region Port instantiation with settings hard-coded and a switch for port selection

if (SERCOM.IsOpen == true) SERCOM.Close(); //Check for open port

SERCOM.Handshake = Handshake.XOnXOff; //HandShake

SERCOM.BaudRate = 600; //BaudRate

SERCOM.DataBits = 7; //DataBits

SERCOM.StopBits = StopBits.One; // StopBits

SERCOM.PortName = args[2].ToString(); //LocalPort set from array [2]

SERCOM.Parity = Parity.Even; //Parity

SERCOM.DtrEnable = true; //DtrEnable

SERCOM.RtsEnable = true; //RtsEnable

SERCOM.DataReceived += new SerialDataReceivedEventHandler(OnReceived); //Data received event handler

SERCOM.Encoding = Encoding.ASCII; //Encoding format for transmission

SERCOM.Open(); //Open the port

#endregion

#region Handle Direction (send vs receive)

if (Convert.ToInt16(args[0]) == 0)

{

TextReader lotr = new StreamReader(args[1]); //interpret the .min file and assign a reader

string lotrS = lotr.ReadToEnd(); // read the file to a string

lotr.Close(); // close the text reader

Console.WriteLine(lotrS);

SERCOM.WriteLine(lotrS); //Write Data to the port buffer for send to lathe

SERCOM.Close();

return;

};

if (Convert.ToInt16(args[0]) == 1)

{

Console.WriteLine("Waiting to receive file from Lathe");

Console.WriteLine("Press any key to quit, without transfering");

Console.ReadLine();

}

Console.WriteLine("end");

Console.ReadLine();

#endregion

}//End Main Loop

}//End public class ProgComm

}//End Name Space ComCom

#endregion

#endregion

 

____________ console out put

C:\WINDOWS>O:\PC\"Com Com Deploy.exe" 1 0 COM9
Waiting to receive file from Lathe
Press any key to quit, without transfering
? $M-15637.MIN
64
sub M-15637.MIN
61
M-15637.MIN
post trim 61 M-15637.MIN

end

_this correlates to my on received which is in a testing faze to figure out why it wasint working I was testing lengths and you can see how trim and trimend both keep the same string.length _

__________ example file *correct looking*

$M-16136.MIN%
ZZ=-.XXX
TH=.140   (USE .005 OR LESS ID TOOL RADIUS)
CO=.XXX
FP=00
FS=.0000
SP=00
SS=.0000
TP=00
TS=.0000
DP=00
DS=.0000
CA=[TH+CO+.005]
CS=[TH+CO+.010]
G50 S2500
NSTRT
VZSHZ=0
VZSHX=0
V20=0
V1=3
/IF[VDIN[24]EQ 1]NBAR
/CALL OFEED
/GOTO N01  
/NBAR CALL ONBAR
N01 G00 X.850 Z.100 TXXXX S2000 M3  (FACE TOOL)
N02 G01 Z.005 F.010
N03 G01 X-.100
N05 G00 X20 Z1.0
N06 G00 X.559 Z.050 TXXXX  M08 (PLUNGE)
N07 G01 Z0 F.006
N08 G76 G01 X.383 L-.025
N09 G01 Z-.105 F.004
N10 G91 G01 X+.015
N11 G90 G01 X.383
N12 G91 G01 X-.015
N13 G90 G01 X.383 F.006
N14 G76 G01 Z-.008 L-.025
N15 G01 X.208
N16 G00 Z.1
N17 G00 X20 Z1.0
N18 G00 X.230 Z.050 TXXXX   (ID ROUGHING)
N19 G01 Z-CA F.005
N20 G00 Z.1
N21 G00 X.282 Z.050 TXXXXXX   (ID)
N22 G41 G01 Z-.008 F.005
N23 G01 X.258 Z-.028 F.004
N24 G01 X.267 Z-.078
N25 G01 X.276
N26 G01 X.282 Z-.105
N27 G01 Z-TH+.017
N28 G02 X.316 Z-TH L.017
N29 G40 G01 Z-CA F.007
N30 G01 X.230 F.010
N31 G00 Z.1
N32 G00 X20 Z1.0  M09
N33 G00 X.485 Z.050 TXXXXXX   (OD)
N34 G42 G01 Z0 F.005
N00 G03 X.509 Z-.0525 L.120 F.004
N35 G03 X.485 Z-.105 L.120
N36 G01 Z-TH+.017
N37 G03 X.451 Z-TH L.017
N38 G40 G01 Z-CA F.005
N39 G01 X.559 F.010
N40 G01 Z-CA-.020 A-45
N41 G00 Z.1
N42 G00 X20 Z1.0
N43 G00 X.559 Z.100 TXXXX   (CO)
N44 G00 Z-TH-CO
N45 G01 X.228 F.004
N46 G01 X.559 F.010
N00 G01 Z.100
N47 G00 X20 Z1.0
N48 /VZSHZ=VZSHZ-CS
V20=V20+1
IF[VZSHZ-CS+ZZ LE VNVLZ-VZOFZ] N100
IF[V20 EQ FP]N200
IF[V20 EQ SP]N210
IF[V20 EQ TP]N220
IF[V20 EQ DP]N230
GOTO N01
N200
VZSHX=VZSHX+FS
GOTO N01
N210
VZSHX=VZSHX+SS
GOTO N01
N220
VZSHX=VZSHX+TS
GOTO N01
N230
VZSHX=VZSHX+DS
GOTO N01
N100 VZSHZ=0
G00 X30 Z30
M05 TXXXX S700
GOTO NSTRT
M30
%

 


Answers (1)