একটি ডিএলএল প্রকল্পে "বন্দর খুলতে পারে না!"


-3

নিম্নলিখিত কোড সহ সিওএম বন্দরগুলির মধ্যে একটিতে অ্যাক্সেস করার জন্য আমি ভিজ্যুয়াল স্টুডিওতে একটি সি কনসোল অ্যাপ্লিকেশন তৈরি করেছি এবং সমস্ত কিছু ঠিকঠাক হয়েছে।

#include <windows.h>
#include <stdio.h>
#include <conio.h>


/*DWORD dwBytesWrite = 25;
WriteFile(hSerial, "LOOOOOL", n, &dwBytesWrite, NULL);*/
/*
int main(void)
{

int n = 25;
char szBuff[25 + 1] = { 0 };

HANDLE hSerial;
DCB dcbSerialParams = { 0 };
COMMTIMEOUTS timeouts = { 0 };
DWORD dwBytesRead = 25;

dcbSerialParams.DCBlength = sizeof(DCB);

hSerial = CreateFile("COM6",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);


if (hSerial == INVALID_HANDLE_VALUE)
{
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
puts("cannot open port!");
return;
}

puts("invalid handle value!");
return;
}

if (!GetCommState(hSerial, &dcbSerialParams))
{
puts("error getting state");
return;
}

dcbSerialParams.BaudRate = CBR_9600;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;

if (!SetCommState(hSerial, &dcbSerialParams))
{
puts("error setting port state");
return;
}

timeouts.ReadIntervalTimeout = 30;
timeouts.ReadTotalTimeoutMultiplier = 100;
timeouts.ReadTotalTimeoutConstant = 100;

if (!SetCommTimeouts(hSerial, &timeouts))
{
puts("timeouts setting fail!");
}

while (1){
if (!ReadFile(hSerial, szBuff, n, &dwBytesRead, NULL)){
puts("serial read error fail!");
return;
}

else
{
printf("%s\n", szBuff);
}
}


getchar();
return 0;

}

এখন আমার একটি ডিএলএল প্রকল্প করা দরকার এবং এটি আমাকে নিম্নলিখিত ত্রুটি দিয়েছে: "পোর্ট খুলতে পারে না!"

কোন ধারণা কেন? ধন্যবাদ


প্রোগ্রামিং এবং সফ্টওয়্যার বিকাশের জন্য নির্দিষ্ট ইস্যুগুলি বিষয়বস্তু বন্ধ, দেখুন আমি এখানে কোন বিষয় সম্পর্কে জিজ্ঞাসা করতে পারি? । স্ট্যাক ওভারফ্লো চেষ্টা করুন তবে দয়া করে প্রথমে পড়ুন আমি কীভাবে একটি ভাল প্রশ্ন জিজ্ঞাসা করব? ।
— ডেভিডপস্টিল

প্রথম সমস্যাটি হ'ল '
— ইন্ট

পাঠযোগ্যতা / স্পষ্টতার জন্য দয়া করে কোডটি ইনডেন্ট করুন
— ব্যবহারকারী 362929249

অনেক 'ফিরে'; মূল () ফাংশনে প্রতিটি বিবৃতি ভুল এবং মূল () প্রধান হিসাবে একটি
— ইনট্রে

উত্তর:


0

এখানে এমএসডিএন-এর একটি উদাহরণ ।

রেফারেন্সযুক্ত ওয়েব পৃষ্ঠায় মন্তব্যটি সি ++ নির্দেশ করে তবে এটি কোনও সি ++ বৈশিষ্ট্য ব্যবহার করছে না।

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

void PrintCommState(DCB dcb)
{
    //  Print some of the DCB structure values
    _tprintf( TEXT("\nBaudRate = %d, ByteSize = %d, Parity = %d, StopBits = %d\n"), 
              dcb.BaudRate, 
              dcb.ByteSize, 
              dcb.Parity,
              dcb.StopBits );
}


int _tmain( int argc, TCHAR *argv[] )
{
   DCB dcb;
   HANDLE hCom;
   BOOL fSuccess;
   TCHAR *pcCommPort = TEXT("COM1"); //  Most systems have a COM1 port

   //  Open a handle to the specified com port.
   hCom = CreateFile( pcCommPort,
                      GENERIC_READ | GENERIC_WRITE,
                      0,      //  must be opened with exclusive-access
                      NULL,   //  default security attributes
                      OPEN_EXISTING, //  must use OPEN_EXISTING
                      0,      //  not overlapped I/O
                      NULL ); //  hTemplate must be NULL for comm devices

   if (hCom == INVALID_HANDLE_VALUE) 
   {
       //  Handle the error.
       printf ("CreateFile failed with error %d.\n", GetLastError());
       return (1);
   }

   //  Initialize the DCB structure.
   SecureZeroMemory(&dcb, sizeof(DCB));
   dcb.DCBlength = sizeof(DCB);

   //  Build on the current configuration by first retrieving all current
   //  settings.
   fSuccess = GetCommState(hCom, &dcb);

   if (!fSuccess) 
   {
      //  Handle the error.
      printf ("GetCommState failed with error %d.\n", GetLastError());
      return (2);
   }

   PrintCommState(dcb);       //  Output to console

   //  Fill in some DCB values and set the com state: 
   //  57,600 bps, 8 data bits, no parity, and 1 stop bit.
   dcb.BaudRate = CBR_57600;     //  baud rate
   dcb.ByteSize = 8;             //  data size, xmit and rcv
   dcb.Parity   = NOPARITY;      //  parity bit
   dcb.StopBits = ONESTOPBIT;    //  stop bit

   fSuccess = SetCommState(hCom, &dcb);

   if (!fSuccess) 
   {
      //  Handle the error.
      printf ("SetCommState failed with error %d.\n", GetLastError());
      return (3);
   }

   //  Get the comm config again.
   fSuccess = GetCommState(hCom, &dcb);

   if (!fSuccess) 
   {
      //  Handle the error.
      printf ("GetCommState failed with error %d.\n", GetLastError());
      return (2);
   }

   PrintCommState(dcb);       //  Output to console

   _tprintf (TEXT("Serial port %s successfully reconfigured.\n"), pcCommPort);
   return (0);
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.