Click here to Skip to main content
15,067,251 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear Developer,

first short what i'm doin':
I'm using an optical probe for serial communication. The probe has got a FTDI Chip (virtual ComPort) and my Device is sending the data with an optical diode. (Every 2 seconds for about 350 ms and after the 350 ms there is an break with 9V DC voltage until the next telegram is comein').

I'm using the SerialDataReceivedEvent to handle the data.
C#
private void getSerialData(object sender, SerialDataReceivedEventArgs e)


After the event starts i'm waitin 400ms
System.Threading.Thread.Sleep(400);
and read out all the data
data = serialPort.ReadExisting();

My problem is, that the Event starts every second. The first string is normal, the second is "" (nothing). I already terminate any form of light from my optical probe and messure the signal my device is sending and can't find anything excepted the normal 350ms telegram.

May someone of you already had the same problem and can tell my why every second string is "" and why the Event starts every second and not every 2nd second when the next telegram starts?
Posted

Have you properly checked COM Port name, baudrate and all that stuff relavant to serial communication ?
   
Sure i've done.

Here is the code i've init my serialPort

C#
private bool intiCom()
        {
            if (serialPort == null)
                serialPort = new SerialPort();
            serialPort.Handshake = Handshake.None;
            serialPort.PortName = comport;
            serialPort.BaudRate = baudrate;
            serialPort.DataBits = databits;

            // StopBits
            if (stopbits == 1)
                serialPort.StopBits = StopBits.One;
            else if (stopbits == 1.5)
                serialPort.StopBits = StopBits.OnePointFive;
            else if (stopbits == 2)
                serialPort.StopBits = StopBits.Two;
            else
                return false;

            // Parity
            if (parity == 1)
                serialPort.Parity = Parity.Even;
            else if (parity == 2)
                serialPort.Parity = Parity.Mark;
            else if (parity == 3)
                serialPort.Parity = Parity.None;
            else if (parity == 4)
                serialPort.Parity = Parity.Odd;
            else if (parity == 5)
                serialPort.Parity = Parity.Space;
            else
                return false;

            // RTS
            serialPort.RtsEnable = true;
            serialPort.Encoding = Encoding.ASCII;

            try
            {
                serialPort.Open();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }


And the settings came from my ini file :)
VB
portname=4
baudrate=9600
databits=8
stopbits=1
parity=3
showsettings=0
showdisplay=1
tray=1
topmost=0
excelinstalled=1
excellang=german
saveinginterval=3600
scalebars=1
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900