I have a script that dumps data from a serial port to both a terminal and the harddrive. The output to the terminal looks fine, however the file write an ^M after each resulting in an extra newline for every other line.
The offending code:
# run and dump to file.
loop {
# output data to log file.
data = sp.read
data.delete!("\C-M") # Removes escape character.
if( data != "" )
puts data
File.open($log_开发者_运维技巧file, 'a') { |f| f.write( data ) }
end
}
Example output: On the terminal:
1
2
3
In the file
1
2
3
Edit: The solution is to run data.delete!("\C-M") after the read.
Try opening the data written to the file in ruby with read. I suspect the problem you have is with the carriage return characters that sometimes cause problems when transferring a file from windows to linux or when downloading files via some mail clients.
I don't know how your serial data looks like, but you can always do a chomp on data variable before writing. Try it and see how it goes.
Edit: If you want to remove the ^M, maybe you can try sp.read.tr("\r","")
精彩评论