Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

serial communication, read the 9th bit

I have an application which connects with an external protocol using serial communication.

I need know if the wakeup bit is set on each packet it sends to me (the 9 bit), and as communication rates must be below 40ms, and response must be sent under 20 ms. The framework, encapsulates the bits read from the port, and only send back the 8 bits of data to me. Also, I cannot wait for the parity error event, because of timing issues.

I need to know how can I read the 9 bit, or if there is a free alternative to http://www.wcscnet.com/CdrvLBro.htm

like image 673
miguel.hpsgaming Avatar asked Nov 24 '11 11:11

miguel.hpsgaming


1 Answers

Did you try to put your serial read function right in the parity error event handler? Depending on the driver, this might be fast enough.

This wouldn't happen to be for a certain slot machine protocol, would it? I did this for fun for you. Maybe it will work?

{
    public Form1()
    {
        InitializeComponent();
    }

    SerialPort sp;
    private void Form1_Load(object sender, EventArgs e)
    {
        sp = new SerialPort("COM1", 19200, Parity.Space, 8, StopBits.One);
        sp.ParityReplace = 0;
        sp.ErrorReceived += new SerialErrorReceivedEventHandler(sp_SerialErrorReceivedEventHandler);          
        sp.ReadTimeout = 5;
        sp.ReadBufferSize = 256;
        sp.Open();
    }

    object msgsLock = new object();
    Queue<byte[]> msgs = new Queue<byte[]>();
    public void sp_SerialErrorReceivedEventHandler(Object sender, SerialErrorReceivedEventArgs e)
    {
        if (e.EventType == SerialError.RXParity)
        {
           byte[] buffer = new byte[256];            
           try
           {                   
               int cnt = sp.Read(buffer, 0, 256);
               byte[] msg = new byte[cnt];
               Array.Copy(buffer, msg, cnt);
               if (cnt > 0)
               {
                   lock (msgsLock)
                   {
                       msgs.Enqueue(msg);
                   }
               }
           }
           catch
           {
           }              
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (msgs.Count > 0)
        {
            lock (msgsLock)
            {
                listBox1.Items.Insert(0, BitConverter.ToString(msgs.Dequeue()));
            }
        }
    }
}

}

Anyways, for more control over the serial port I suggest using the win32 calls to get what you want.

http://msdn.microsoft.com/en-us/magazine/cc301786.aspx

like image 193
rare Avatar answered Oct 12 '22 02:10

rare