Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing to file and recognizing a CR/LF!!!

Status
Not open for further replies.

czarj

Technical User
Apr 22, 2004
130
US
HI all,

I am designing a small software application to receive data through a serial port, display it on the screen and write it to a text file. Currently the application gets the data and writes it to the screen and text file, but doesn’t do any formatting. The next step is to format the data in a useful way. The data is a series of numbers or dashes separated by commas with 20 sets per interval. At the end of each line there is supposed to be a carriage return and line feed. However, the CR/LF doesn’t seem to work, all I get is a continuous stream. For example, the data steams in like this:
--, --, --, --, 80, 93, -- , -- , --, 160, 120, 91, --, --, --, --, --, --, -- , -- ?? --, --, --, --, --, 90, -- , -- , --, 160, 120, 91, --, --, --, --, --, --, -- , -- ??

I need to format that data so that after each series is done it returns to another line as shown here (as it is supposed to):

--, --, --, --, 80, 93, -- , -- , --, 160, 120, 91, --, --, --, --, --, --, -- , --
--, --, --, --, --, 90, -- , -- , --, 160, 120, 91, --, --, --, --, --, --, -- , --


Below is the sub I am calling to write the data to a file. How do I get VB to recognize the CR/LF?


Call WriteFile("c:\test.txt", strInput)
Code:
Public Sub WriteFile(FileName As String, strInput As String)

   Dim i As Byte
   i = FreeFile
   Open FileName For Append As #i
   Print #i, strInput;
   Close #i

End Sub

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Remove the semicolon in your Print statement.

You might want to open your file and leave it open until all records are written to it.
 
Here is some text from the VB help file about the Write # statement:

Unlike the Print # statement, the Write # statement inserts commas between items and quotation marks around strings as they are written to the file. You don't have to put explicit delimiters in the list. Write # inserts a newline character, that is, a carriage return–linefeed (Chr(13) + Chr(10)), after it has written the final character in outputlist to the file.

I am not suggesting that you use Write instead of Print because it has some other differeneces as well. Perhaps you should just append a Cr and Lf like this:

Print #i, strInput & vbCrLf
 
Well, I've worked on this for a while, but still no luck. After looking at the output file with a hex editor I found that the CR & LF codes were seperated by null charecters (i.e. 000A00000D0000). So I removed all the null 0's in hopes that would work. Now when I look at the output file with a hex editor I see the 0A0D, but it is still not being recognized as a CR/LF. Any suggestions?

He is the write sub I am calling:

Code:
Public Sub WriteFile(FileName As String, strInput As String)

   'prepare and open the file for data input
   Dim i As Byte
   i = FreeFile
   Open FileName For Append As #i
   
   strInput = Replace(strInput, Chr(0), "") 'remove null characters
   
   Print #i, strInput;
   Close #i

End Sub

Thanks all,

CzarJ

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Maybe it is in UniCode where it uses 2 bytes per character.

If this is the case then the high byte will be empty and therefore appear as null.
 
You can use the StrConv function to convert from unicode.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I tried converting from Unicode to VB, but its not Unicode. While I was at it I tried all the other StrConv modalities and none of them helped the situation.

Thanks,

czarJ


--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
The Visual Basic shortcut code for CR and LF together is vbCrLf, or you can search for chr(10) & chr(13).

-B
 
I've tried numerous methods to get VB to recognize the CrLf with no luck. Is there a way to tell VB when it sees the hexadecimal value "0A0D" to vbCRLF?


Thanks



--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Code:
if instr(strTemp, vbcrlf) > 0 then
    'Do whatever
end if
-B
 
Well um.

Its backwards.

13 is the carriage return

10 is the line feed.

You should be seeing 0D0A



 
If your data is indeed LF CR instead of CR LF try checking for vbLf & vbCr instead of vbCrLf. You might also try using vbNewLine and see if that is helpful. Good Luck!

Have a great day!

j2consulting@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top