Click here to Skip to main content
15,915,867 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
GeneralRe: Memory utilization of MemoryStream Pin
Dave Kreskowiak20-Feb-14 13:11
mveDave Kreskowiak20-Feb-14 13:11 
GeneralRe: Memory utilization of MemoryStream Pin
elix54520-Feb-14 14:25
elix54520-Feb-14 14:25 
AnswerRe: Memory utilization of MemoryStream Pin
JonB26-Feb-14 5:19
JonB26-Feb-14 5:19 
QuestionGantt Chart Pin
Member 1057034317-Feb-14 0:39
Member 1057034317-Feb-14 0:39 
QuestionASP.NET Membership Pin
Meetmca16-Feb-14 18:17
professionalMeetmca16-Feb-14 18:17 
AnswerRe: ASP.NET Membership Pin
Richard MacCutchan16-Feb-14 22:26
mveRichard MacCutchan16-Feb-14 22:26 
QuestionQuestion about VB and Databases Pin
CaseLost14-Feb-14 17:05
CaseLost14-Feb-14 17:05 
AnswerRe: Question about VB and Databases Pin
Eddy Vluggen15-Feb-14 23:33
professionalEddy Vluggen15-Feb-14 23:33 
Questionsql query syntax in .net Pin
Member 1050621512-Feb-14 3:52
Member 1050621512-Feb-14 3:52 
AnswerRe: sql query syntax in .net Pin
Dave Kreskowiak12-Feb-14 4:30
mveDave Kreskowiak12-Feb-14 4:30 
AnswerRe: sql query syntax in .net Pin
thatraja12-Feb-14 4:31
professionalthatraja12-Feb-14 4:31 
AnswerRe: sql query syntax in .net Pin
Richard Deeming12-Feb-14 5:22
mveRichard Deeming12-Feb-14 5:22 
AnswerRe: sql query syntax in .net Pin
Eddy Vluggen12-Feb-14 6:56
professionalEddy Vluggen12-Feb-14 6:56 
GeneralRe: sql query syntax in .net Pin
Member 1050621512-Feb-14 22:01
Member 1050621512-Feb-14 22:01 
AnswerRe: sql query syntax in .net Pin
Eddy Vluggen12-Feb-14 22:28
professionalEddy Vluggen12-Feb-14 22:28 
GeneralRe: sql query syntax in .net Pin
Member 1050621512-Feb-14 22:35
Member 1050621512-Feb-14 22:35 
GeneralRe: sql query syntax in .net Pin
Eddy Vluggen13-Feb-14 0:31
professionalEddy Vluggen13-Feb-14 0:31 
GeneralRe: sql query syntax in .net Pin
Member 1050621513-Feb-14 1:39
Member 1050621513-Feb-14 1:39 
GeneralRe: sql query syntax in .net Pin
Eddy Vluggen13-Feb-14 2:59
professionalEddy Vluggen13-Feb-14 2:59 
GeneralRe: sql query syntax in .net Pin
Member 1050621513-Feb-14 3:48
Member 1050621513-Feb-14 3:48 
GeneralRe: sql query syntax in .net Pin
Eddy Vluggen13-Feb-14 4:59
professionalEddy Vluggen13-Feb-14 4:59 
QuestionUploading Data from MySql to MS Project using .net C# Pin
Badari Narayan11-Feb-14 21:22
Badari Narayan11-Feb-14 21:22 
AnswerRe: Uploading Data from MySql to MS Project using .net C# Pin
Richard MacCutchan11-Feb-14 21:25
mveRichard MacCutchan11-Feb-14 21:25 
Questionserialport timeout c# .net 4 client profile Pin
openLG11-Feb-14 1:40
openLG11-Feb-14 1:40 
Hello,

I want to read a packet from serial port,
I don't know the length of the packet,
so I have to wait a timeout for the first byte
and then I have to read all next bytes.

I supersed class
SerialPort

and I write the method
Receive

in ".NET Framework 4 Client Profile".

I try to use this code but
sometimes
Receive

works well, sometimes doesn't work


public class PortaCom : SerialPort
    {
        int timeout = 0;
        public PortaCom(string portName, int baudRate, int timeout)
            : base(portName, baudRate)
        {
            this.timeout = timeout;
            base.Open();
        }

        public byte[] Receive()
        {
            List<Byte> ByteList = new List<Byte>();
            DateTime Now = DateTime.Now;
            base.ReadTimeout = 1;//ms
            //wait 1st byte
            //in a timeout span
            int FirstByte = -1;
            while (FirstByte == -1)
            {
                if (DateTime.Now - Now > TimeSpan.FromMilliseconds(timeout))
                    throw new TimeoutException();
                try { FirstByte = base.ReadByte(); }
                catch (TimeoutException) { }
            }
            //read next bytes
            //if available
            int NextByte = FirstByte;
            while (NextByte != -1)
            {
                try
                {
                    Byte _NextByte = (Byte)NextByte;
                    ByteList.Add(_NextByte);
                    NextByte = base.ReadByte();
                }
                catch (TimeoutException)
                {
                    return ByteList.ToArray();
                }                
            }
            return ByteList.ToArray();
        }
}


Can someone help me ?
QuestionRe: serialport timeout c# .net 4 client profile Pin
Eddy Vluggen11-Feb-14 3:11
professionalEddy Vluggen11-Feb-14 3:11 

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.