简体   繁体   中英

C# Visual Studio console serial interface

I am starting from the ground up and trying to write a simple console to interface with a serial port on a windows 7 computer.

I am using:

Code:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

 using System.IO.Ports;

 namespace ConsoleApplication1
 {
    class Program
    {
        public static void Main()
        {
            SerialPort mySerialPort = new SerialPort("COM5");


        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;

        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

        mySerialPort.Open();

        mySerialPort.Write("This is a test");

        Console.WriteLine("Press any key to continue...");
        Console.WriteLine();
        Console.ReadKey();
        mySerialPort.Close();
    }

    private static void DataReceivedHandler(
                        object sender,
                        SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
        Console.Write(indata);
    }
  }
}

So far i have ran this code and interfaced with a xbee module connected to my computer. That xbee module sent the serial data to another xbee connected to a msp430. The msp430 is programmed to take whatever it receives and echo it back. This works with the code I have. In my console I will get "This is a test" echoed back onto the console window.

The problem I am having is when I use a virtual serial connection to a putty window. I am using this to try to ease development and not have to use hardware all of the time. I will use HHD Free Virtual Serial Ports to create a bridged connection between two serial ports. I will connect one to the putty terminal and the other will be for my console program. When running the program I recieve the error.

"A first chance exception of type 'System.TimeoutException' occurred in System.dll"

on the line

mySerialPort.Write("This is a test");

But the "This is a test" will appear on the Putty terminal. If I remove the "mySerialPort.Write("This is a test");" line and attempt to send data from the Putty window to the console window, nothing appears.

Again this works with my hardware solution just fine.

Please help and i will try to clarify any questions. Thank you again.

I guess the problem is in virtual utility you are using. It seems it sets pin states incorrect. If I use 2 putty instances and connect to bridged ports I see infinite sending of symbol I entered. So I think your code is fine.

When I was working on such tasks I used a special cable for connecting 2 hardware com ports (com1 and com2, if you don't have them you can try usb-to-com converters) and it worked fine.

I'm has the very same problem with HHD Free Virtual Serial Ports, but this work great with asynchronous write operation.

Also you can replace

mySerialPort.Write("This is a test");

with (for example)

var buffer = Encoding.ASCII.GetBytes("This is a test");
mySerialPort.BaseStream.BeginWrite(buffer, 0, buffer.Length, ar => mySerialPort.BaseStream.EndWrite(ar), new object());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM