Write column values to serial port on vb

2021-03-27T14:50:13.97+00:00

I'm new with programming, and i would like help to write all the column values from a datagrid view to serial comport

Developer technologies | VB
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,381 Reputation points
    2021-03-29T02:53:54.917+00:00

    Hi @Francisco Javier Rodriguez Castro ,
    Here's a document you can refer to: Accessing the Computer's Ports (Visual Basic)

    i would like help to write all the column values from a datagrid view to serial comport

    The following example is to send a specific column in datagrid view to serial comport.

        Dim _serialPort As SerialPort = New SerialPort("COM1", 19200, Parity.None, 8, StopBits.One)  
        Private isOpened As Boolean = False  
      
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
            ' Send Data.  
            Dim data As String = String.Empty  
            If _serialPort.IsOpen Then  
                For Each row As DataGridViewRow In DataGridView1.Rows  
                    data += row.Cells(yourColumnIndex).Value.ToString() + " | "  
                Next  
                _serialPort.WriteLine(data)  
            Else  
                MessageBox.Show("cannot open Serial Port")  
            End If  
        End Sub  
      
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click  
            If Not isOpened Then  
                Try  
                    _serialPort.Open() ' Open Serial Port  
                    Button1.Text = "Close Serial Port"  
                    isOpened = True  
                    AddHandler _serialPort.DataReceived, AddressOf serialPort_DataReceived  
                Catch  
                    MessageBox.Show("Fail to open Serial Port!")  
                End Try  
            Else  
      
                Try  
                    _serialPort.Close()  
                    Button1.Text = "Open Serial Port"  
                    isOpened = False  
                Catch  
                    MessageBox.Show("Fail to close Serial Port!")  
                End Try  
            End If  
        End Sub  
        Private Sub serialPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)  
            Dim Result As String = _serialPort.ReadLine  
            MessageBox.Show(result)  
        End Sub  
    

    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.