Process.Start() only works when line is misread

强颜欢笑 提交于 2019-12-12 02:24:00

问题


I have an Arduino sending a character 'c' through COM port 3.
I have a VS2013 C# console application listening on COM3 for character 'c'. The if statement when 'c' is read launches a .ahk file.

The problem is that when I build and install my application on another computer the file will only be opened if the line is written incorrectly on the console.

I noticed this after several attempts. So as to say, when data is received console prints a new line "data received", and a second line with the data.

Ex:

Data received:
c
Data received:
c
Data received:
c

Now this will not launch the application it's supposed to. But this will:

Data received:
cData received:
Data received:
c

Notice that on both examples I have received data three times, But only on the second example the lines are not parsed correctly and it does not print the character, or has line breaks either. In one of the three times it received data. Oddly enough it is then when it launches the application.

I am charging 2 USD to print photos. Each 2 USD triggers an ahk script that takes and prints the image. So every time 2 USD are insterted i receive a ´c´. Then, after the image is printed the ahk script quits and I would like it to be reopened again once another 2 USD are inserted.

I have read about Process.Start() in Microsoft pages. I'm still learning basics so it takes me a while to think in the right direction.

I have spent hours trying to figure it out through trial and error. Making modifications to the code with what my limited ability allows me to. link to an example on how to call a process from C#.

Anyhow, here is the code which (by the way) I copy pasted from a Google search on a Microsoft help site:

using System;
using System.IO.Ports;
using System.Diagnostics;
using System.Threading;

class PortDataReceived
{
    public static void Main()
    {
        SerialPort mySerialPort = new SerialPort("COM3");

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

        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

        mySerialPort.Open();

        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.WriteLine("Data Received:");
        Thread.Sleep(300);
        Console.Write(indata);
        Thread.Sleep(300);
        if (indata == "c")
        {
            Thread.Sleep(1000);
            //Process.Start(@"C:\Users\laptop\Desktop\print.ahk");
            Process process = new Process();
            // Configure the process using the StartInfo properties.
            process.StartInfo.FileName = "print.ahk";
            process.StartInfo.Arguments = "-n";
           //process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
            process.Start();
            process.WaitForExit();// Waits here for the process to exit.
        }
    }
}

As a bonus, I need to find a programmer that will help me implement a face detection/crop program I found here on StackOverflow. Actually found one written with python and another in C#. Have been on online sites to hire programmers but the offers seem audacious, and without any promise. Anyhow pm me if you need more info.


回答1:


It looks like your sender not only sends the 'c' character but also some whitespace. So basically you just need to relax the condition in your if statement a bit.

So instead of

if (indata == "c")

please try for example

if (indata.Contains("c"))


来源:https://stackoverflow.com/questions/31297195/process-start-only-works-when-line-is-misread

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!