Sune Henriksen

Hi All.

I have a quick question. I have a file that needs to be sent over a serial connection. I am not sure if it is a file or the content of the file that needs to be sent, this would be subject to test. I am sendin from a terminal to a printer and the printer recieves and prints. The file is here called Test1.LBL and it contains this:

! 0 200 200 240 1
LABEL
CONTRAST 0
TONE 0
SPEED 5
PAGE-WIDTH 400
BAR-SENSE
;// PAGE 0000000004000240
BT 5 1 0
B I2OF5 2 1 30 23 115 999900001190507
BT OFF
T90 0 2 136 59 Hello
IL 124 14 280 14 50
LINE 71 90 334 90 1
FORM
PRINT

So I am looking for 2 ways of doing this. 1 to send the file over serial port and 2 to send the content of the file.

I have the serial port open. What comes next

Thanks in advance.

Sune



Re: .NET Base Class Library Sending content of file over serial connection.

nobugz

Read the file with System.IO.File.ReadAllLines(). That gives you an array of strings. For each string, call SerialPort.WriteLine() to send it to the printer.





Re: .NET Base Class Library Sending content of file over serial connection.

Sune Henriksen

I have a problem here, though, because when I add using System.IO I only have an option called ports. If I write what you suggested, I get an error. I can see that it iss supposed to work, according to MSDN F1 Help, but how do I fix this

Sune





Re: .NET Base Class Library Sending content of file over serial connection.

nobugz

That's very, very strange. System.IO.File lives in mscorlib.dll which is referenced by any .NET app. What is the error message





Re: .NET Base Class Library Sending content of file over serial connection.

Sune Henriksen

Error 1 'System.IO.File' does not contain a definition for 'ReadAllLines'

If I get the list of available items after File. I get of what is in F1 help. Readalllines is not there, neither readallbytes.

If I mark the error and F1 I get this: Compiler Error CS0117.

Hope you can help.

Sune





Re: .NET Base Class Library Sending content of file over serial connection.

nobugz

You must be using .NET 1.1. Here's a replacement for File.ReadAllLines():

public static string[] ReadAllLines(string path) {
System.Collections.ArrayList list = new System.Collections.ArrayList();
System.IO.StreamReader sr = new System.IO.StreamReader(path);
string txt;
while ((txt = sr.ReadLine()) != null) list.Add(txt);
sr.Close();
return (string[])list.ToArray(typeof(string));
}






Re: .NET Base Class Library Sending content of file over serial connection.

Sune Henriksen

OK. How about this aproach instead

SerialPort port = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);

port.Open();

using (Stream from = System.IO.File.Open("Test1.LBL", FileMode.Open))

using (Stream to = port)

{

int readCount;

byte[] buffer = new byte[1024];

while ((readCount = from.Read(buffer, 0, buffer.Length)) != 0)

{

to.Write(buffer, 0, readCount);

}

}

Here I have a problem with the Stream to a port. What do I need to put other then "port"

Hope somebody has the time to help..

Sune





Re: .NET Base Class Library Sending content of file over serial connection.

Christopher Fairbairn

Hi Sune,

The missing line could be replaced as follows:

using (Stream to = port.BaseStream)

The BaseStream property is documented in MSDN here - http://msdn2.microsoft.com/en-us/library/system.io.ports.serialport.basestream.aspx

Hope this helps,

Christopher Fairbairn