Click here to Skip to main content
15,888,351 members
Home / Discussions / C#
   

C#

 
GeneralRe: Read information from serial port in Mathlab format Pin
Victor Gonzalez 20245-Apr-24 12:26
Victor Gonzalez 20245-Apr-24 12:26 
AnswerRe: Read information from serial port in Mathlab format Pin
jschell5-Apr-24 12:20
jschell5-Apr-24 12:20 
GeneralRe: Read information from serial port in Mathlab format Pin
Victor Gonzalez 20245-Apr-24 12:28
Victor Gonzalez 20245-Apr-24 12:28 
GeneralRe: Read information from serial port in Mathlab format Pin
Richard MacCutchan5-Apr-24 22:44
mveRichard MacCutchan5-Apr-24 22:44 
GeneralRe: Read information from serial port in Mathlab format Pin
Victor Gonzalez 20245-Apr-24 23:22
Victor Gonzalez 20245-Apr-24 23:22 
GeneralRe: Read information from serial port in Mathlab format Pin
Richard MacCutchan5-Apr-24 23:41
mveRichard MacCutchan5-Apr-24 23:41 
GeneralRe: Read information from serial port in Mathlab format Pin
Luc Pattyn6-Apr-24 8:16
sitebuilderLuc Pattyn6-Apr-24 8:16 
GeneralRe: Read information from serial port in Mathlab format Pin
Victor Gonzalez 20247-Apr-24 1:12
Victor Gonzalez 20247-Apr-24 1:12 
Hello all,

Yes periodicaly the data sended is 21 byte, but some times send 63 or 42 bytes.
I think that I found the solution and now I can red the data in decimanl and later I transofrm to "X".

Now I have another problem. When I start the program with out break point for debug, when I read the information i write to the screen, but this write not found, but if I put a break point and debug the progam, yes is writing the messages in the screen.

this is my code.

using System;
using System.IO.Ports;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;

class PortDataReceived
{

    public static void Main()
    {

        SerialPort mySerialPort = new SerialPort("COM3");

        mySerialPort.BaudRate = 56000;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;
        mySerialPort.RtsEnable = true;

        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

        mySerialPort.Open();

        Console.WriteLine("Press any key to continue...");
        Console.WriteLine();
        Console.ReadKey();
        mySerialPort.Close();
    }

    private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {

        
        SerialPort sp = (SerialPort)sender;

        
        int count = sp.BytesToRead;
        int count2 = 0;
        byte[] data = new byte[count];
        sp.Read(data, 0, data.Length);

        switch (data.Length)
        {

            case 63:
                Console.WriteLine("inicio carrera o  pause off");
                if (data[7].ToString("X").Equals("0") && data[8].ToString("X").Equals("A6"))
                {
                    Console.WriteLine("pause off");

                }
                else
                {
                    if (data[7].ToString("X").Equals("3E") && data[8].ToString("X").Equals("A1"))
                    {
                        Console.WriteLine("carrera go!");
                    }
                }
                break;
            
            case 21:
               //Console.WriteLine("vuelta o pause on o stop carrera manual");
                if (data[7].ToString("X").Equals("1B") && data[8].ToString("X").Equals("A9"))
                {
                    Console.WriteLine("contador vuelta y tiempo");
                    
                }
                else
                {
                    if (data[7].ToString("X").Equals("0") && data[8].ToString("X").Equals("A5"))
                    {
                        Console.WriteLine("pause on");
                    }
                    else
                    {
                        if (data[7].ToString("X").Equals("0") && data[8].ToString("X").Equals("A7"))
                            Console.WriteLine("stop forzado");
                    }
                }
                break ;


            case 42:
                Console.WriteLine(" fin carril");
                break ;
             
        }
        sp.DiscardInBuffer();

        /*string[] tablehex = new string[data.Length];
        
        for (int i = 0; i < count; i++)
        {
            tablehex[i] = data[i].;
           //tablehex[i] = hexvalue;

        }
                 
            //   Console.WriteLine(hexvalue.ToString());

            // Console.WriteLine(tablehex[count2].ToString());

        leertabla(tablehex);*/


    }

GeneralRe: Read information from serial port in Mathlab format Pin
Luc Pattyn7-Apr-24 1:52
sitebuilderLuc Pattyn7-Apr-24 1:52 
GeneralRe: Read information from serial port in Mathlab format Pin
Victor Gonzalez 20247-Apr-24 6:04
Victor Gonzalez 20247-Apr-24 6:04 
GeneralRe: Read information from serial port in Mathlab format Pin
Luc Pattyn7-Apr-24 8:13
sitebuilderLuc Pattyn7-Apr-24 8:13 
GeneralRe: Read information from serial port in Mathlab format Pin
Victor Gonzalez 20247-Apr-24 9:53
Victor Gonzalez 20247-Apr-24 9:53 
GeneralRe: Read information from serial port in Mathlab format Pin
Luc Pattyn7-Apr-24 11:04
sitebuilderLuc Pattyn7-Apr-24 11:04 
GeneralRe: Read information from serial port in Mathlab format Pin
Victor Gonzalez 20247-Apr-24 22:42
Victor Gonzalez 20247-Apr-24 22:42 
GeneralRe: Read information from serial port in Mathlab format Pin
Luc Pattyn7-Apr-24 22:45
sitebuilderLuc Pattyn7-Apr-24 22:45 
QuestionDebugging the Sandbox Pin
Richard Andrew x645-Apr-24 3:43
professionalRichard Andrew x645-Apr-24 3:43 
QuestionIParsable? IFormattable? ICustomFormatter? IFormatProvider? TryParse! ToString! Pin
Tracy Dryden24-Mar-24 12:29
Tracy Dryden24-Mar-24 12:29 
AnswerRe: IParsable? IFormattable? ICustomFormatter? IFormatProvider? TryParse! ToString! Pin
Mycroft Holmes24-Mar-24 15:07
professionalMycroft Holmes24-Mar-24 15:07 
GeneralRe: IParsable? IFormattable? ICustomFormatter? IFormatProvider? TryParse! ToString! Pin
Tracy Dryden24-Mar-24 16:19
Tracy Dryden24-Mar-24 16:19 
AnswerRe: IParsable? IFormattable? ICustomFormatter? IFormatProvider? TryParse! ToString! Pin
Ralf Meier24-Mar-24 22:35
mveRalf Meier24-Mar-24 22:35 
GeneralRe: IParsable? IFormattable? ICustomFormatter? IFormatProvider? TryParse! ToString! Pin
Tracy Dryden25-Mar-24 7:51
Tracy Dryden25-Mar-24 7:51 
AnswerRe: IParsable? IFormattable? ICustomFormatter? IFormatProvider? TryParse! ToString! Pin
jschell26-Mar-24 12:00
jschell26-Mar-24 12:00 
Rant(Current) AI Rant. Pin
Gerry Schmitz17-Mar-24 12:29
mveGerry Schmitz17-Mar-24 12:29 
GeneralRe: (Current) AI Rant. Pin
Eddy Vluggen26-Mar-24 5:34
professionalEddy Vluggen26-Mar-24 5:34 
GeneralRe: (Current) AI Rant. Pin
lmoelleb1-Apr-24 21:44
lmoelleb1-Apr-24 21:44 

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.