Re: Please help with this.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Bill Thompson (billt61_at_rgv.rr.com)
Date: 02/19/05


Date: Sat, 19 Feb 2005 02:11:49 -0600


"Ed" <Ed@whyshouldItell.me> wrote in message
news:RTwRd.71075$QS5.16867@trndny06...
> Hi,
>
> I still cannot get the receive working.
> This is the last thing I have tried and all I get is garbage back.
> All I seem to get is 0x01 even when I am supposed to get text.
> Please take a look and feel free to suggest. I really need binary to work.
>
> void CPSMDlg::OnOnCommMscomm1()
> {
> Char ed[2048];
> FILE *in;
> COleVariant Value;
>
> if (m_comm.GetCommEvent() == 2)
> {
> Value = m_comm.GetInput();
> CString Buffer = V_BSTR(&Value);
> ed[current]=Buffer[0];
> ed[current+1]=0x00;
> in = fopen("c:\\psm\\a1.dat", "wb");
> fwrite(ed, current, 1, in);
> fclose(in);
> current++;
> AfxMessageBox(Buffer);
> }
> }
>
> current is a count thats reset before I request any data.
>
> Thanks in advance,
> Ed
> --------------------------------------------------------------------------

---
>
The only difference I see between your code and mine is that I assigned
m_comm.GetInput() directly to a CString, while you're assigning it to a
COleVariant.  According to the CommCtrl Wrapper Class I have, GetInput
returns a CString.  So perhaps something is getting mucked up in all the
casting.
You can also take a look at GetInputLength(), allocate an array of chars
(TCHARS? unicode support? up to you I guess), and simply copy them all in.
I don't think CString knows or cares if there is 'binary' data inside it...
AFAIK there is no translation going on.
Looking at your code, it also seems possible you will drop characters by
only taking one character out.  I would use GetInputLength() to copy all
received characters into your buffer.
Good Luck
>
> "Bill Thompson" <billt61@rgv.rr.com> wrote in message
> news:uvGB4vXFFHA.3888@TK2MSFTNGP12.phx.gbl...
> > "Ed" <Ed@whyshouldItell.me> wrote in message
> > news:K28Rd.23829$ya6.12297@trndny01...
> >> Please someone help me out with this.
> >> I preferr not to change horses in mid race with this by switching
serial
> >> routines.
> >>
> >> I know I am doing something wrong. My transmit works just fine. I am
set
> > to
> >> binary mode as I need to do transfer in binary. I cannot get any
received
> >> data. Here is parts of my code that apply.
> >>
> >> bool CPSMDlg::sendMicroProgram()
> >> {
> >> int a, b;
> >> unsigned char ede[20000];
> >> FILE *in;
> >>
> >> a=0;
> >> b=0;
> >> lop:
> >> Sleep(10);
> >>
> >> if (rx1 == 0){
> >> a++;
> >> if (a <= 100)
> >> goto lop;
> >> }
> >> if (rx1 == 1){
> >> AfxMessageBox("rx1 = 1");
> >> GetChar();
> >> if (get == 0xee){
> >> AfxMessageBox("Got EE");
> >> return true;
> >> }
> >> rx1=0;
> >> ede[b] = get;
> >> a++;
> >> b++;
> >> if (a <= 100)
> >> goto lop;
> >> in = fopen("c:\\psm\\a1.dat", "wb");
> >> fwrite (ede, b, 1, in);
> >> fclose(in);
> >> AfxMessageBox("Did Not Get EE");
> >> }
> >> AfxMessageBox("Did Not Get EE");
> >> return;
> >> }
> >>
> >> bool CPSMDlg::SendChar() // this routine works perfectly
> >> {
> >> CByteArray btArray;
> >>
> >> btArray.Add (DataByte);
> >> COleVariant var(btArray);
> >> m_comm.SetOutput(var);
> >> return true;
> >> }
> >>
> >> bool CPSMDlg::GetChar() // I know I am doing something wrong here
> >> {
> >> COleVariant myVar;
> >> CString sBuffer;
> >> myVar.Attach (m_comm.GetInput());
> >> sBuffer = myVar.bstrVal;
> >> get=sBuffer[0];
> >> return true;
> >> }
> >>
> >> void CPSMDlg::OnOnCommMscomm1()
> >> {
> >> // TODO: Add your control notification handler code here
> >> rx1=1;
> >> }
> >>
> >>
> >> I need to send a program to my monitor within my microcontroller and
that
> >> works just fine and the program starts just fine so i know data is
being
> >> sent ok.
> >> Please modify my code so I can receive single bytes to be evaluated .
> >> Thanks in advance.
> >>
> >>
> >
> > Since nobody has replied, I'll take a shot.
> >
> > I'm assuming that m_comm is an instance of the CCommCtrl class.  If not,
> > read no further.
> >
> > Obviously you need something like an interrupt service routine to
receive
> > characters in a timely fashion.  I have cut and paste this from code
that
> > I
> > have laying around that I have not used in years (but was working);
> > hopefully it may get you on the right track.
> >
> > // Add an eventsink map to hook the OnComm event:
> >
> > BEGIN_EVENTSINK_MAP(MyDialog, CDialog)
> > ON_EVENT(MyDialog, ID_COMMCTRL, 1 /* OnComm */, OnCommEvent, VTS_NONE)
> > END_EVENTSINK_MAP()
> >
> > // sample OnCommEvent handler:
> >
> > void MyDialog::OnCommEvent()
> > {
> > switch(m_comm.GetCommEvent())
> > {
> > case 2: // vbMSCommEvReceive:
> >   ProcessData(m_commctrl.GetInput());
> > }
> >
> > void MyDialog::ProcessData(LPCTSTR pchData)
> > {
> > // do something with the data
> > }
> >
> >
> > //this goes in the .h file for the CPSMDlg:
> > class CPSMDlg : public CDialog
> > {
> > ...
> > protected:
> > afx_msg void OnCommEvent();
> > DECLARE_EVENTSINK_MAP()
> > ...
> > }
> >
> > // You probably already have code that looks like this somewhere:
> >
> > BOOL MyDialog::OnInitDialog()
> > {
> > ...
> > // Create the MSCOMM32 OLE Control.
> > if (!m_commctrl.Create(NULL,0,CRect(0,0,0,0),this,ID_COMMCTRL))
> > {
> >  TRACE0("Failed to create OLE Communications Control\n");
> >  return -1;      // fail to create
> > }
> >
> > // Set RThreshold property to 1. The default RThreshold value (0)
> > // causes the OnComm event to NOT fire when a character is detected
> > // in the serial port.
> > m_commctrl.SetRThreshold(1);
> > ...
> > }
> >
> >
> >
> >
>
>


Relevant Pages

  • Re: Serial Port Class ?
    ... OnComm event when characters are received. ... > Being someone who reads these vb and API groups regularly, I seem to> remember someone mentioning that they have a serial port class for VB> (for replacing the MSCOMM control) on their website, but for the life of> me, I couldn't find any references to it. ... All I'm doing is sending a few> characters to it to change to a different channel, ...
    (microsoft.public.vb.enterprise)
  • Re: Serial Port Class ?
    ... OnComm event when characters are received. ... > Being someone who reads these vb and API groups regularly, I seem to> remember someone mentioning that they have a serial port class for VB> (for replacing the MSCOMM control) on their website, but for the life of> me, I couldn't find any references to it. ... All I'm doing is sending a few> characters to it to change to a different channel, ...
    (microsoft.public.vb.winapi)
  • Re: replacing unsigned char * with CString
    ... Or unsigned char *. ... Note that CString does not "automatically expand" anything in the example you gave. ... 16-bit characters at the drop of a preprocessor symbol. ... imageFile.Read(tempImage, radarMapSize); ...
    (microsoft.public.vc.mfc)
  • Re: C++ Standard Library
    ... Surprisingly, not only does my customer base not want STL, they don't usually want ... 8-bit and 16-bit characters under std::string, and how to move them in and out of CString. ...
    (microsoft.public.vc.mfc)
  • Re: CString and UTF-8
    ... CString will try to understand the multibyte characters as its ... CString treats the UTF-8 characters as ...
    (microsoft.public.vc.mfc)