By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,610 Members | 1,989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,610 IT Pros & Developers. It's quick & easy.

How to read images using serial port in c#.net 2005

P: 1
Hi,

I am using a check scanner.It has functions which gives output like image ,checknumber. i am using serial port to get this output in my coding. My problem is i am not able to read the display image ( which i scanned using scanner ). I used serail port as well as MSCOMM control.but i couldn't get the output. Can anybody knows this and help me to sort out my problem -- it is very urgent

here i am using windows 2000 professional OS and i am using VS 2005 c# .Here is my code
MSCOMM Control Code

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Initialize the COM Port control
InitComPort();
try
{
axMSComm1.Output = new byte[] { 0, 0x41, (byte)'A', 255 };
}
catch (System.Exception se)
{
rtfTerminal.Text = se.Message;
}
}

private void OnComm(object sender,EventArgs e)//MSCommLib OnComm Event Handler
{
//*** The below code gives the check number
rtfTerminal.Text = axMSComm1.Input.ToString();

//***code below suppose to capture the check image and save as a jpg file, but it never works
//no error is thrown in this occssion.It swallows the error and does nothing

byte[] picture = (byte[])axMSComm1.Input;
MemoryStream ms = new MemoryStream(picture);
Bitmap bmp = new Bitmap(ms);
string saveTime = System.DateTime.Now.Millisecond.ToString() + ".jpg";
bmp.Save("C:\\Images" + saveTime);

}
private void InitComPort()
{

axMSComm1.CommPort = 1;
if (axMSComm1.PortOpen) axMSComm1.PortOpen = false;
axMSComm1.RThreshold = 1;
axMSComm1.Settings = "115200,n,8,1";
axMSComm1.DTREnable = true;
axMSComm1.Handshaking = MSCommLib.HandshakeConstants.comNone;
axMSComm1.InputMode = MSCommLib.InputModeConstants.comInputModeText;
axMSComm1.InputLen = 0;
axMSComm1.NullDiscard = false;
// Attach the event handler
axMSComm1.OnComm += new System.EventHandler(this.OnComm);
try
{
axMSComm1.PortOpen = true;
}
catch(System.Exception se)
{
rtfTerminal.Text = se.Message;
}

}
}

Serial Port

public partial class Form1
{
System.IO.Ports.SerialPort serialPort = new System.IO.Ports.SerialPort();
public Form1()
{
InitializeComponent();
serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(ser ialPort_DataReceived);
}

private void btnConnect_Click(System.Object sender, System.EventArgs e)
{
if (serialPort.IsOpen)
{
serialPort.Close();
}
try
{
System.IO.Ports.SerialPort with_1 = serialPort;
with_1.PortName = cbbCOMPorts.Text;
with_1.BaudRate = 115200;
with_1.Parity = System.IO.Ports.Parity.None;
with_1.DataBits = 8;
with_1.StopBits = System.IO.Ports.StopBits.One;
// .Encoding = System.Text.Encoding.Unicode
serialPort.Open();
lblMessage.Text = cbbCOMPorts.Text + " connected.";
btnConnect.Enabled = false;
//btnDisconnect.Enabled = True
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

private void Form1_Load(System.Object sender, System.EventArgs e)
{
for (int i = 0; i <= (new Microsoft.VisualBasic.Devices.Computer()).Ports.Se rialPortNames.Count - 1; i++)
{
cbbCOMPorts.Items.Add((new Microsoft.VisualBasic.Devices.Computer()).Ports.Se rialPortNames[i]);
}
}
private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{

txtDataReceived.Invoke(new myDelegate(updateTextBox), new object[] {});
}
public delegate void myDelegate();
public void updateTextBox ()
{
System.Windows.Forms.RichTextBox with_1 = txtDataReceived;
with_1.Font = new Font("Garamond", (float) (12.0), FontStyle.Bold);
with_1.SelectionColor = Color.Red;
with_1.AppendText(serialPort.ReadExisting());
with_1.ScrollToCaret();
}
}
Apr 21 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.