Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read and Store Bytes from Serial Port

I am trying to create a RS232 application that reads incoming data and stores it in a buffer. I found the following code in an RS232 example but I am not sure how to use it

*RS232 Example port_DataReceived*

    private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        if (!comport.IsOpen) return;

        if (CurrentDataMode == DataMode.Text)
        {
            string data = comport.ReadExisting();

            LogIncoming(LogMsgType.Incoming, data + "\n");
        }
        else
        {
            int bytes = comport.BytesToRead;

            byte[] buffer = new byte[bytes];

            comport.Read(buffer, 0, bytes);

            LogIncoming(LogMsgType.Incoming, ByteArrayToHexString(buffer) + "\n");

        }
    }

I am trying to write another method that takes an incoming byte array and combines it with another array ... see below:

private void ReadStoreArray()
{
   //Read response and store in buffer
   int bytes = comport.BytesToRead;
   byte[] respBuffer = new byte[bytes];
   comport.Read(respBuffer, 0, bytes);   

   //I want to take what is in the buffer and combine it with another array
   byte AddOn = {0x01, 0x02}
   byte Combo = {AddOn[1], AddOn[2], respBuffer[0], ...};
}

I currently have both methods in my code as I am confused how to read and store the incoming bytes to the port. Can I use the "port_DataReceived" method in my "ReadStoreArray" method? Do I need to modify my "ReadStoreArray" method? Or should I just start over?

like image 846
Nevets Avatar asked Jan 24 '14 16:01

Nevets


People also ask

How do you read a serial port?

In Serial Port Reader go to the “Main menu”, choose “Session -> New session”. Alternately, you can click on the “New” icon on the main toolbar or press “Ctrl + N”. This invokes the “New monitoring session” screen. Terminal view – all received data is displayed in ASCII characters on a text console.

Can a serial port read and write at the same time?

It cannot transmit and receive data at the same time. If using multiple software serial ports, only one can receive data at a time.

How does the serial port transfer a byte?

To transmit a byte, the serial device driver program (running on the CPU) sends a byte to the serial port"s I/O address. This byte gets into a 1-byte "transmit shift register" in the serial port. From this shift register bits are taken from the byte one-by-one and sent out bit-by-bit on the serial line.

How many bytes of data are sent in serial port?

Most serial ports use between five and eight data bits. Binary data is typically transmitted as eight bits. Text-based data is transmitted as either seven bits or eight bits.


3 Answers

When you create your SerialPort:

SerialPort comport = new SerialPort("COM1");
comport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    // Shortened and error checking removed for brevity...
    if (!comport.IsOpen) return;
    int bytes = comport.BytesToRead;
    byte[] buffer = new byte[bytes];
    comport.Read(buffer, 0, bytes);
    HandleSerialData(buffer);
}

//private void ReadStoreArray(byte[] respBuffer)
private void HandleSerialData(byte[] respBuffer)
{
   //I want to take what is in the buffer and combine it with another array
   byte [] AddOn = {0x01, 0x02}
   byte [] Combo = {AddOn[1], AddOn[2], respBuffer[0], ...};
}
like image 130
Johnny Mopp Avatar answered Oct 04 '22 10:10

Johnny Mopp


Don't bother with using the DataRecieve handler, it's horribly inaccurate, you're better off to start a thread which constantly reads the serial port and grabs every byte which comes across it.

like image 37
Docmur Avatar answered Oct 04 '22 10:10

Docmur


You can't read the same data from the port twice. You'll need to read it once into a buffer, then either share the buffer (pass as a function parameter) or clone it to give each function its own copy.

like image 30
Ben Voigt Avatar answered Oct 04 '22 10:10

Ben Voigt