Click here to Skip to main content
15,887,267 members
Home / Discussions / C#
   

C#

 
GeneralRe: Serial Port Won't Send While Receiving Pin
C-P-User-328-Dec-12 5:59
C-P-User-328-Dec-12 5:59 
GeneralRe: Serial Port Won't Send While Receiving Pin
Richard MacCutchan28-Dec-12 6:40
mveRichard MacCutchan28-Dec-12 6:40 
AnswerRe: Serial Port Won't Send While Receiving Pin
GrooverFromHolland28-Dec-12 7:04
GrooverFromHolland28-Dec-12 7:04 
AnswerRe: Serial Port Won't Send While Receiving Pin
jschell28-Dec-12 9:53
jschell28-Dec-12 9:53 
GeneralRe: Serial Port Won't Send While Receiving Pin
C-P-User-331-Dec-12 10:54
C-P-User-331-Dec-12 10:54 
GeneralRe: Serial Port Won't Send While Receiving Pin
C-P-User-31-Jan-13 13:35
C-P-User-31-Jan-13 13:35 
GeneralRe: Serial Port Won't Send While Receiving Pin
C-P-User-31-Jan-13 13:36
C-P-User-31-Jan-13 13:36 
GeneralRe: Serial Port Won't Send While Receiving Pin
GrooverFromHolland1-Jan-13 23:41
GrooverFromHolland1-Jan-13 23:41 
Hi,

A serial port cannot send and receive at the same time.
There must be some kind of protocol e.g. hardware handshaking, software handshaking or some kind of software protocol.
if You don't know what protocol is used, it is impossible to to make it work.

Here is an example of a protocol that my device uses.
My port is listening and waiting for for bytes to receive.
The device sends three bytes(two address bytes and one data byte and expects to have the data byte as answer immediately.
If the device receives the data byte and checks if this was the byte sent it sends the next three bytes, until there is no more to send.

In the code snippet You can see that the port is sending in the data_received event with no problem,
that is because I know the device is not sending until I answer correct.

This is the code I use to do this.
It only works with the protocol of my device, but it shows how to implement such a protocol:

C#
System.Timers.Timer tmrPortInt = new System.Timers.Timer(800);
 comportInt.DataReceived += new SerialDataReceivedEventHandler(portInt_DataReceived);
 tmrPortInt.Elapsed += new System.Timers.ElapsedEventHandler(tmrPortInt_Elapsed);

 private void portInt_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (tmrPortInt.Enabled) tmrPortInt.Stop();//this is a timer to check if no more data is received,
                                                      //While data is received within 800 m Sec port keeps listening.
                                                      // In my case he data is processed in the timer elapsed event(~6000 bytes).
            int tempAddress;
            int bytes = comportInt.BytesToRead;
            byte[] buffer = new byte[3];  //I want to read only three bytes.

            if (bytes > 2)
            {
                buffer = new byte[bytes]; //Empty my buffer before filling with received bytes.
                comportInt.Read(buffer, 0, 3);
               
                comportInt.Write(buffer, 2, 1);//You can see i am writing within the port_received event.
                                               // I send the third byte received.
                if (receiveByteList == null) receiveByteList = new List<byte>();
               
                tempAddress = (256 * buffer[0] + buffer[1]); // decimal value of two address bytes
                receiveBytes[tempAddress] = buffer[2];
                receiveByteList.Add(receiveBytes[1]);// add data byte to list
               
                tmrPortInt.Start();// Prevents the timer to elapse until no more bytes are received.
            }
        }
         void tmrPortInt_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            tmrPortInt.Stop();
            this.Invoke(new EventHandler(Process_MachineData));// This is where I do something With the data received.
        }


Regards,

Groover
0200 A9 23
0202 8D 01 80
0205 00

GeneralRe: Serial Port Won't Send While Receiving Pin
C-P-User-32-Jan-13 4:25
C-P-User-32-Jan-13 4:25 
GeneralRe: Serial Port Won't Send While Receiving Pin
C-P-User-37-Jan-13 9:21
C-P-User-37-Jan-13 9:21 
QuestionQuestion about split Pin
S Douglas27-Dec-12 6:32
professionalS Douglas27-Dec-12 6:32 
AnswerRe: Question about split Pin
PIEBALDconsult27-Dec-12 7:20
mvePIEBALDconsult27-Dec-12 7:20 
GeneralRe: Question about split Pin
S Douglas27-Dec-12 8:04
professionalS Douglas27-Dec-12 8:04 
GeneralRe: Question about split Pin
SledgeHammer0127-Dec-12 10:38
SledgeHammer0127-Dec-12 10:38 
GeneralRe: Question about split Pin
PIEBALDconsult27-Dec-12 11:03
mvePIEBALDconsult27-Dec-12 11:03 
GeneralRe: Question about split Pin
S Douglas27-Dec-12 11:29
professionalS Douglas27-Dec-12 11:29 
GeneralRe: Question about split Pin
SledgeHammer0127-Dec-12 13:34
SledgeHammer0127-Dec-12 13:34 
GeneralRe: Question about split Pin
PIEBALDconsult27-Dec-12 13:59
mvePIEBALDconsult27-Dec-12 13:59 
GeneralRe: Question about split Pin
SledgeHammer0127-Dec-12 14:29
SledgeHammer0127-Dec-12 14:29 
GeneralRe: Question about split Pin
PIEBALDconsult27-Dec-12 15:10
mvePIEBALDconsult27-Dec-12 15:10 
GeneralRe: Question about split Pin
PIEBALDconsult27-Dec-12 16:14
mvePIEBALDconsult27-Dec-12 16:14 
GeneralRe: Question about split Pin
SledgeHammer0127-Dec-12 17:24
SledgeHammer0127-Dec-12 17:24 
GeneralRe: Question about split Pin
April Fans27-Dec-12 14:14
April Fans27-Dec-12 14:14 
QuestionDebugging C# application using WinDbg 6.12.0002.633 Pin
Sharath C V27-Dec-12 0:20
professionalSharath C V27-Dec-12 0:20 
QuestionHOW TO ADD LINK TAG IN CODE OF ASPX.CS PAGE ALONG WITH TAB TEXT TO OPEN LINK. Pin
saurabh pintu goel26-Dec-12 19:51
saurabh pintu goel26-Dec-12 19:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.